SerialLCD Class | |
Header
#define SerialLCDl_h #include <inttypes.h> class SerialLCD { public: SerialLCD(uint8_t); void begin(); void setCursor(uint8_t, uint8_t); void print(char*); void print(char); void printStr(const char*); void printStr(const char*, uint8_t,uint8_t col = 0); void printPgm(const char*,uint8_t,uint8_t col = 0); void write(uint8_t); void clear(); void home(); void print(int,uint8_t dploc = 0); private: void send(uint8_t, uint8_t); };
| |
C++
// John Saunders 7/15/2012 #include "Arduino.h" #include "SerialLCD.h" #define clockPin 3 #define dataPin 4 #define lcdInitLen 8 #define RSInst 0 #define RSData 1 const byte lcdInitSeq[] = {0x30,0x30,0x30,0x38,0x0C,0x01,0x14,0x80}; const byte rowAddr[] = {0x80,0xC0,0x94,0xD4}; // rows 1-4 as displayed uint8_t _enablePin; SerialLCD::SerialLCD(uint8_t p) { _enablePin = p; } void SerialLCD::begin() { pinMode(dataPin,OUTPUT); pinMode(clockPin,OUTPUT); pinMode(_enablePin,OUTPUT); delay(50); pinMode (clockPin,OUTPUT); pinMode(dataPin,OUTPUT); pinMode(_enablePin,OUTPUT); digitalWrite(clockPin,LOW); for(int i; i < lcdInitLen;i++) { send(lcdInitSeq[i],RSInst); delay(5); } } void SerialLCD::send(uint8_t serin,uint8_t RS) { shiftOut(dataPin,clockPin,MSBFIRST,serin); digitalWrite(dataPin,RS); delayMicroseconds(1); digitalWrite(_enablePin,HIGH); delayMicroseconds(1); digitalWrite(_enablePin,LOW); delayMicroseconds(45); } void SerialLCD::setCursor(byte col, byte row) { int r = constrain(row,1,4); int c = constrain(col,0,19); send((rowAddr[r-1]+c),RSInst); } void SerialLCD::print(char* words) { do { send(*words++,RSData); } while(*words!= NULL); } void SerialLCD::printStr(const char* words) { do { send(*words++,RSData); } while(*words!= NULL); } void SerialLCD::printStr(const char* words, uint8_t row, uint8_t col) { int r = constrain(row,1,4); int c = constrain(col,0,19); send((rowAddr[r-1]+c),RSInst); do { send(*words++,RSData); } while(*words!= NULL); } void SerialLCD::printPgm(const char* words,uint8_t row,uint8_t col) { int r = constrain(row,1,4); int c = constrain(col,0,19); int i=0; char ch; if((row == 0) && (col == 0)) { send(0x01,RSInst); delay(5); } else { send((rowAddr[r-1]+c),RSInst); } while(true) { ch = pgm_read_byte(words+i++); if(ch == NULL) { break; } send(ch,RSData); } } void SerialLCD::print(char c) { send(char(c),RSData); } void SerialLCD::write(uint8_t i) { send(i,RSData); } void SerialLCD::clear() { send(0x01,RSInst); delay(5); } void SerialLCD::home() { send(0x02,RSInst); delay(2); } void SerialLCD::print(int k,uint8_t dploc) { int div; int rem; int mod = 10000; uint8_t leading = 0; uint8_t i = 4; dploc=constrain(dploc,0,3); if(k < 0) { send(char('-'),RSData); k = -k; } while(mod >= 1) { div = k/mod; rem = k % mod; if(leading == 1 || div > 0) { send(char(div + '0'),RSData); leading = 1; } if(leading == 0 && div == 0 && i <= dploc) { send(char(div + '0'),RSData); } if(dploc > 0 && i == dploc) { send(char('.'),RSData); } k = rem; mod = mod/10; i--; } } |