Skip to content

Instantly share code, notes, and snippets.

@ckxng
Last active January 11, 2016 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckxng/a23b79b53629760bceb3 to your computer and use it in GitHub Desktop.
Save ckxng/a23b79b53629760bceb3 to your computer and use it in GitHub Desktop.
// set clock to 1MHz; 1 microsecond = 1 clock cycle
#define F_CPU 1000000L
#define DEBUG_ENABLED 0
#define DEBUG(x) if(DEBUG_ENABLED){Serial.print(x);}
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#define LCD_SIZE_X 16
#define LCD_SIZE_Y 2
#define LCD_RS_PIN 12
#define LCD_ENABLE_PIN 11
#define LCD_D4_PIN 5
#define LCD_D5_PIN 4
#define LCD_D6_PIN 3
#define LCD_D7_PIN 2
/* additionally...
* ground to LCD R/W pin
* 10K potentiometer to LCD VO pin
*/
LiquidCrystal lcd(LCD_RS_PIN, LCD_ENABLE_PIN, LCD_D4_PIN, LCD_D5_PIN, LCD_D6_PIN, LCD_D7_PIN);
typedef struct cursor_t {
unsigned char x = 0; // 0-indexed column where the next char will write
unsigned char y = 0; // 0-indexed row where the next char will write
void reset();
void advance();
void setLine(unsigned char);
} cursor_t;
static cursor_t lcd_cursor; // track the cursor manually
#define BTBUFFER_SZ 32
#define BT_TX_PIN 6
#define PT_RX_PIN 7
SoftwareSerial bluetooth(BT_TX_PIN, PT_RX_PIN);
typedef struct btbuffer_t {
unsigned char len = 0;
char * buf = new char[BTBUFFER_SZ];
char last = ' ';
} btbuffer_t;
static btbuffer_t btbuffer;
void setup() {
Serial.begin(9600);
DEBUG(F("Initialize LCD.\n"));
lcd.begin(LCD_SIZE_X, LCD_SIZE_Y);
DEBUG(F("Change BlueSMiRF to 9600 baud, does not persist reboot.\n"));
bluetooth.begin(115200); // BlueSMiRF defaults to 115200bps
bluetooth.print("$"); // Enter CMD mode
bluetooth.print("$");
bluetooth.print("$");
delay(100);
while (bluetooth.available() > 0) { bluetooth.read(); } // "CMD"
bluetooth.println("U,9600,N");
bluetooth.begin(9600);
delay(100);
DEBUG(F("Print OK! on LCD.\n"));
lcd.clear();
lcd.print(F("OK!"));
delay(500);
DEBUG(F("Clear LCD for program start.\n"));
lcd.clear();
}
void loop()
{
if (read_bluetooth()) {
DEBUG(F("Writing bluetooth buffer to LCD.\n"));
lcd_reset();
print_btbuffer();
}
delay(100);
}
/**
* print the current contents of the btbuffer
*/
void print_btbuffer() {
for(unsigned char i = 0; i < btbuffer.len; i++) {
lcd_putChar(btbuffer.buf[i]);
}
btbuffer.len = 0;
}
/**
* clear the LCD and set the cursor to 0,0
*/
void lcd_reset() {
lcd.clear();
lcd_cursor.reset();
}
/**
* set the cursor to 0,0
*/
void cursor_t::reset() {
this->setLine(0);
}
/**
* set the cursor to the beginning of the specified line
* \param line 0-indexed line number
*/
void cursor_t::setLine(unsigned char line) {
this->x=0;
this->y=line;
}
/**
* set the LCD cursor to match our local cursor_t.
* This only needs to be done just before writing out to the screen.
*/
void lcd_cursorSync() {
lcd.setCursor(lcd_cursor.x, lcd_cursor.y);
}
/**
* advance the cursor, wrapping according to LCD_SIZE_X and LCD_SIZE_Y
*/
void cursor_t::advance() {
this->x++;
if(this->x >= LCD_SIZE_X) {
this->x=0;
this->y++;
}
if(this->y >= LCD_SIZE_Y) {
this->y=0;
}
}
/**
* sync the cursor, then write a character, and advance the cursor
* \param c a charater to write to the LCD
*/
void lcd_putChar(char c) {
lcd_cursorSync();
lcd.write(c);
lcd_cursor.advance();
}
/**
* read from bluetooth into btbuffer; appropriate for calling in the main loop.
* If content is available on the bluetooth serial, add it to the buffer.
* Characters are inserted at btbuffer.buf[btbuffer.len-1], and len will not grow
* beyond BTBUFFER_SZ. This means that the last character of the buffer keeps
* getting replaced once the buffer is at max size.
*
* '\r' and '\n' are not added to the buffer. A sequence of multiple '\r' or '\n'
* characters in a row are ignored. Buffer reading stops once a '\r' or '\n' is
* encountered.
*
* \return true if a '\r' or '\n' was encountered
*/
bool read_bluetooth() {
while(bluetooth.available()) {
DEBUG(F("Found content on bluetooth: ["));
char c = bluetooth.read();
DEBUG(c);
DEBUG(F("]\n"));
if(c == '\n' || c == '\r') {
if(btbuffer.last == '\n' || btbuffer.last == '\r') {
DEBUG(F("Duplicate newline found\n"));
btbuffer.last = c;
return false;
}
DEBUG(F("Newline found!\n"));
btbuffer.last = c;
return true;
}
if(btbuffer.len < BTBUFFER_SZ) {
btbuffer.len++;
}
btbuffer.buf[btbuffer.len-1] = btbuffer.last = c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment