root/twiLCD/twiLCD.cpp

Revision 9, 8.0 KB (checked in by mlalondesvn, 15 months ago)

DS1337:

FIXED - All callback functions now work properly
FIXED - Oscillator startup so that it resets the status register
FIXED - All alarm mode bitmasks
MODIF - clockStart() from macro to function
MODIF - clockSave() to reset the clock so that the status flags are reset

twiLCD:

MODIF - setClrscreenCallback() renamed clrsSetCallback()

Line 
1extern "C" {
2    #include <Wire/Wire.h>
3}
4
5#include "twiLCD.h"
6
7#ifdef TWI_LCD_DPOT_CTRL
8#include "../ds1803/ds1803.h"
9#endif
10
11twiLCD::twiLCD()
12{
13#if defined(__AVR_ATmega168__) && !defined(TWI_LCD_SMALL)
14    #if defined(TWI_LCD_CTRL) && !defined(TWI_LCD_BL_PWM)
15        backlightStatus = 0;
16    #endif
17   
18    #ifdef TWI_LCD_BL_PWM
19        pinMode(TWI_LCD_BACKLIGHT, HIGH);
20    #endif
21   
22    displayStatus = false;
23#endif
24
25#ifdef WIRE_LIB_SCAN_MOD
26    /*if (!Wire.isStarted())*/ Wire.begin();
27#else
28    Wire.begin();
29#endif
30}
31
32twiLCD LCD = twiLCD();
33
34void twiLCD::Init() {
35#ifdef TWI_LCD_CTRL
36    backlightOff();
37#ifdef TWI_LCD_DPOT_CTRL
38    setBrightness(0);
39#endif
40#endif
41    delay(20);
42/////////// 4 pin initialization
43    writeCommand(0x03); // function set: 4 pin initialization
44    delay(5);
45    writeCommand(0x03); // function set: 4 pin initialization
46    delay(1);
47    writeCommand(0x03); // function set: 4 pin initialization
48    delay(1);
49    writeCommand(0x02); // function set: 4 pin initialization
50/////////// end of 4 pin initialization
51   
52   
53    writeCommand(LCD_FUNCTION_SET); // function set: 4-bit interface, 1 display lines, 5x7 font
54    writeCommand(LCD_ENTRY_MODE);
55    writeCommand(LCD_CURSOR);
56    clearScreen();
57   
58    writeCommand(LCD_CLOSE_INIT);
59   
60#if defined(__AVR_ATmega168__) && !defined(TWI_LCD_SMALL)
61#ifdef TWI_LCD_CTRL
62    #if defined(TWI_LCD_BL_PWM) || defined(TWI_LCD_DPOT_CTRL)
63        #ifdef TWI_LCD_DPOT_CTRL
64            setBrightness();
65        #endif
66        backlightOn(TWI_LCD_BL_LEVEL);
67    #else
68        backlightOn();
69    #endif
70#endif
71
72    displayStatus = true;
73#endif
74}
75
76boolean twiLCD::checkLCDBusy(void)
77{
78    Wire.requestFrom(PCF8574_RADDR, 1);
79   
80    while (!Wire.available());
81   
82    return (Wire.receive() & TWI_LCD_BUSY ? true : false);
83}
84
85void twiLCD::writeCommand(uint8_t value)
86{
87    int value1  = 0;
88    int control = 0; // stores RS and RW
89       
90#ifdef TWI_LCD_USE_TIMEOUT
91    previousMillis  = millis();
92#endif
93   
94    control = value >> 8;   // get the control signals RS and RW
95    control >>= 5;          // shift the control signals to the left
96 
97    Wire.beginTransmission(PCF8574_WADDR);
98   
99    value1  = value;
100    cbi(value1, TWI_LCD_DATAL); // Turn off LByte
101   
102    cbi(value1, TWI_LCD_RS);
103    cbi(value1, TWI_LCD_RW);
104   
105    sendPulse(value1);
106   
107    delay(1);
108   
109    cbi(value, TWI_LCD_DATAH);  // Turn off HByte
110    value <<= 4;
111   
112    cbi(value, TWI_LCD_RS);
113    cbi(value, TWI_LCD_RW);
114   
115    sendPulse(value);
116   
117    Wire.endTransmission();
118   
119    delay(1);
120}
121
122void twiLCD::writeData(uint8_t value)
123{
124    int value1  = 0;
125    int control = 0; // stores RS and RW
126   
127    // Wait for the LCD to be ready
128   
129#ifdef TWI_LCD_USE_TIMEOUT
130    previousMillis  = millis();
131#endif
132
133    control = value >> 8;   // get the control signals RS and RW
134    control >>= 5;          // shift the control signals to the left
135   
136    value1  = value;
137    cbi(value1, TWI_LCD_DATAL); // Turn off LByte
138    sbi(value1, TWI_LCD_RS);
139    cbi(value1, TWI_LCD_RW);
140   
141    while (checkLCDBusy());
142   
143    Wire.beginTransmission(PCF8574_WADDR);
144    sendPulse(value1);
145   
146    delay(1);
147   
148    cbi(value, TWI_LCD_DATAH);  // Turn off HByte
149    value   <<= 4;
150   
151    sbi(value, TWI_LCD_RS);
152    cbi(value, TWI_LCD_RW);
153   
154    sendPulse(value);
155    Wire.endTransmission();
156}
157
158void twiLCD::sendPulse(int value)
159{
160    cbi(value, TWI_LCD_ENABLE);
161    writeToPCF(value);
162   
163    sbi(value, TWI_LCD_ENABLE);
164    writeToPCF(value);
165   
166    cbi(value, TWI_LCD_ENABLE);
167    writeToPCF(value);
168}
169
170void twiLCD::writeToPCF(int value)
171{
172#if defined(TWI_LCD_CTRL) && !defined(TWI_LCD_BL_PWM)
173    if (backlightStatus)
174        sbi(value, TWI_LCD_BACKLIGHT);
175    else
176        cbi(value, TWI_LCD_BACKLIGHT);
177   
178    Wire.send(value);
179#else
180    Wire.send(value);
181#endif
182}
183
184void twiLCD::clearScreen(void)
185{
186    writeCommand(LCD_CLEAR);
187    delay(100);
188   
189#if defined(__AVR_ATmega168__) && !defined(TWI_LCD_SMALL)
190    currentLine = 1;
191#endif
192   
193#ifdef LCD_USE_CLRSCREEN_CALLBACK
194    if (twiLCDcallbackFunc[0]) twiLCDcallbackFunc[0]();
195#endif
196}
197
198void twiLCD::printString_P(const char *data)
199{
200    int ch;
201   
202    for (;;) {
203        ch = pgm_read_byte( data++ );
204        if ( !ch ) return;
205        writeData(ch);
206    }
207}
208
209#if defined(__AVR_ATmega168__) && !defined(TWI_LCD_SMALL)
210#ifdef TWI_LCD_USE_TIMEOUT
211boolean twiLCD::checkTimeout(void)
212{
213    if (displayStatus && millis() - previousMillis > TWI_LCD_TIMEOUT) {
214        turnOff();
215        return true;
216    }
217
218    return false;
219}
220#endif
221
222#ifdef TWI_LCD_CTRL
223#ifdef TWI_LCD_BL_PWM
224void twiLCD::backlightOn(void)
225{
226    digitalWrite(TWI_LCD_BACKLIGHT, HIGH);
227}
228
229void twiLCD::backlightOn(uint8_t value)
230{
231    analogWrite(TWI_LCD_BACKLIGHT, value);
232}
233
234void twiLCD::backlightOff(void)
235{
236    digitalWrite(TWI_LCD_BACKLIGHT, LOW);
237}
238#else
239#ifdef TWI_LCD_DPOT_CTRL
240void twiLCD::backlightOn(void)
241{
242    DPOT.setWiper2(TWI_LCD_BL_LEVEL);
243}
244
245void twiLCD::backlightOn(uint8_t value)
246{
247    DPOT.setWiper2(value);
248}
249
250void twiLCD::backlightOff(void)
251{
252    DPOT.setWiper2(0);
253}
254
255void twiLCD::setBrightness(void)
256{
257    DPOT.setWiper1(TWI_LCD_DB_LEVEL);
258}
259
260void twiLCD::setBrightness(uint8_t value)
261{
262    DPOT.setWiper1(value);
263}
264
265#else
266void twiLCD::backlightOn(void)
267{
268    int val = 0;
269    backlightStatus = 1;
270    sbi(val, TWI_LCD_BACKLIGHT);
271   
272    Wire.beginTransmission(PCF8574_WADDR);
273    Wire.send(val);
274    Wire.endTransmission();
275}
276
277void twiLCD::backlightOff(void)
278{
279    int val = 0;
280    backlightStatus = 0;
281    cbi(val, TWI_LCD_BACKLIGHT);
282   
283    Wire.beginTransmission(PCF8574_WADDR);
284    Wire.send(val);
285    Wire.endTransmission();
286}
287#endif
288#endif
289#endif
290
291void twiLCD::turnOff(void)
292{
293    writeCommand(LCD_TURN_OFF);
294#if defined(TWI_LCD_CTRL)
295    backlightOff();
296#ifdef TWI_LCD_DPOT_CTRL
297    setBrightness(0);
298#endif
299#endif
300    displayStatus = false;
301}
302
303/**
304 * This function needs a rewrite
305**/
306void twiLCD::setCursor(int index)
307{
308    //0-79, index for one line display, 8 bit mode
309    //0-39 and 64-103 for lines one and two of two line display, not implemented yet
310    int cmd = 128+index;
311    writeCommand(cmd);
312}
313
314/**
315 * This function needs a rewrite
316**/
317void twiLCD::moveToXY(uint8_t row, uint8_t column)
318{
319    int position;
320   
321    //  Determine the new position
322    position = (row * 20) + column;
323   
324    //  Send the correct commands to the command register of the LCD
325    if(position < 20)
326        writeCommand(0x80 | position);
327    else if(position >= 20 && position < 40)
328        writeCommand(0x80 | (position % 20 + 0x40));
329    else if(position >= 41 && position < 60)
330        writeCommand(0x80 | (position % 40 + 0x14));
331    else if(position >= 20 && position < 40)
332        writeCommand(0x80 | (position % 60 + 0x54));
333}
334
335uint8_t twiLCD::getCurrentLine(void)
336{
337    return currentLine;
338}
339
340void twiLCD::goToLine(uint8_t line)
341{
342    if (line > LCD_ROWS) return;
343    moveToXY(line - 1, 0);
344    currentLine = line;
345}
346
347void twiLCD::goToNextLine(void)
348{
349    if (currentLine+1 > LCD_ROWS) {
350        goToLine(1);
351    } else {
352        goToLine(currentLine+1);
353    }
354}
355
356void twiLCD::goHome(void)
357{
358    writeCommand(LCD_GO_HOME);   // set cursor position to zero
359    currentLine = 1;
360}
361
362void twiLCD::clearLine(void)
363{
364    int ii;
365    moveToXY(currentLine-1, 0);
366   
367    for (ii=0;ii<LCD_COLUMNS;ii++)
368    {
369        printString(" ");
370    }
371   
372    moveToXY(currentLine-1, 0);
373}
374
375void twiLCD::shiftDisplayLeft(void)
376{ 
377    writeCommand(LCD_SHIFT_LEFT);
378}
379
380void twiLCD::shiftDisplayRight(void)
381{
382    writeCommand(LCD_SHIFT_RIGHT);
383}
384
385bool twiLCD::getCurrentStatus(void)
386{
387    return displayStatus;
388}
389
390void twiLCD::printInteger(uint16_t integer, bool leadingZero)
391{   
392    uint8_t thousands = integer / 1000;
393    uint8_t hundreds = (integer - thousands*1000) / 100;
394    uint8_t tens = (integer - thousands*1000 - hundreds*100 ) / 10;
395    writeData(tens + 0x30);
396   
397    uint8_t ones = (integer - thousands*1000 - hundreds*100 - tens*10);
398    writeData(ones + 0x30);
399}
400
401void twiLCD::printInteger(int16_t integer)
402{
403    if (integer < 0)  {
404        printString("-");
405        integer = 0 - integer;
406    }
407   
408    //  Break down the original number into the thousands, hundreds, tens,
409    //  and ones places and then immediately write that value to the LCD
410    uint8_t thousands = integer / 1000;
411    if (thousands > 0) writeData(thousands + 0x30);
412   
413    uint8_t hundreds = (integer - thousands*1000) / 100;
414    if (hundreds > 0 || thousands > 0)
415        writeData(hundreds + 0x30);
416   
417    uint8_t tens = (integer - thousands*1000 - hundreds*100 ) / 10;
418    if (tens || hundreds > 0 || thousands > 0)
419        writeData(tens + 0x30);
420   
421    uint8_t ones = (integer - thousands*1000 - hundreds*100 - tens*10);
422    writeData(ones + 0x30);
423}
424
425#endif
426
427#ifdef LCD_USE_CLRSCREEN_CALLBACK
428void twiLCD::clrsSetCallback(void (*userFunc)(void))
429{
430    twiLCDcallbackFunc[0] = userFunc;
431}
432#endif
Note: See TracBrowser for help on using the browser.