Andrew Boyson / motorhome

Dependencies:   net lpc1768 crypto clock web fram log

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lcd.c Source File

lcd.c

00001 #include <stdint.h>
00002 #include <stdlib.h>
00003 
00004 #include "i2c1.h"
00005 #include "mstimer.h"
00006 #include "clk.h"
00007 #include "tm.h"
00008 
00009 // commands
00010 #define LCD_CLEARDISPLAY 0x01
00011 #define LCD_DISPLAYCONTROL 0x08
00012 #define LCD_FUNCTIONSET 0x20
00013 
00014 // flags for display on/off control
00015 #define LCD_DISPLAYON 0x04
00016 #define LCD_DISPLAYOFF 0x00
00017 
00018 // flag for entry mode
00019 #define LCD_ENTRYLEFT 0x02
00020 
00021 // flags for function set
00022 #define LCD_8BITMODE 0x10
00023 #define LCD_2LINE 0x08
00024 #define LCD_5x10DOTS 0x04
00025 
00026 static uint8_t data[2]; //This must be static as it is used in the main routine
00027 static int _result = I2C1_RESULT_NONE;
00028 
00029 static void send(int length)
00030 {
00031     I2C1Send(0x3e, length, data, &_result);
00032 }
00033 
00034 static void command(uint8_t cmd)
00035 {
00036     data[0] = 0x80;
00037     data[1] = cmd;
00038     send(2);
00039 }
00040 void write(uint8_t value)
00041 {
00042     data[0] = 0x40;
00043     data[1] = value;
00044     send(2);
00045 }
00046 
00047 #define TODO_NONE           0
00048 #define TODO_INIT_START     1
00049 #define TODO_INIT_FUNCTION  2
00050 #define TODO_INIT_CONTROL   3
00051 #define TODO_INIT_CLEAR     4
00052 #define TODO_READY          5
00053 #define TODO_SEND_START     6
00054 #define TODO_SEND_CURSOR0   7
00055 #define TODO_SEND_TEXT0     8
00056 #define TODO_SEND_CURSOR1   9
00057 #define TODO_SEND_TEXT1    10
00058 
00059 static int _todo = TODO_NONE;
00060 
00061 static uint32_t _MsTimerWaitAfter = 0;
00062 static int      _waitAfterMs      = 0;
00063 
00064 static const char* _pText0 = 0;
00065 static const char* _pText1 = 0;
00066 static int i = 0;
00067 
00068 static void LcdSendText(char* pText0, char* pText1)
00069 {
00070     _pText0 = pText0;
00071     _pText1 = pText1;
00072     _todo = TODO_SEND_START;
00073 }
00074 
00075 void LcdInit()
00076 {
00077     _todo = TODO_INIT_START;
00078 }
00079 
00080 static void handleDoIt()
00081 {
00082     if (I2C1Busy) return;
00083     if (!MsTimerRelative(_MsTimerWaitAfter, _waitAfterMs)) return;
00084     _waitAfterMs = 0; //Don't wait unless specified below
00085     _MsTimerWaitAfter = MsTimerCount;
00086     switch (_todo)
00087     {
00088         case TODO_INIT_START:
00089             _todo = TODO_INIT_FUNCTION;
00090             _waitAfterMs = 15;
00091             break;
00092         case TODO_INIT_FUNCTION:
00093             command(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x10DOTS); //2 line
00094             _todo = TODO_INIT_CONTROL;
00095             _waitAfterMs = 2;
00096             break;
00097         case TODO_INIT_CONTROL:
00098             command(LCD_DISPLAYCONTROL | LCD_DISPLAYON);
00099             _todo = TODO_INIT_CLEAR;
00100             _waitAfterMs = 2;
00101             break;
00102         case TODO_INIT_CLEAR:
00103             command(LCD_CLEARDISPLAY);
00104             _todo = TODO_READY;
00105             _waitAfterMs = 15;
00106             break;
00107         case TODO_READY:
00108             break;
00109         case TODO_SEND_START:
00110             _todo = TODO_SEND_CURSOR0;
00111             break;
00112         case TODO_SEND_CURSOR0:
00113             command(0x80);
00114             _todo = TODO_SEND_TEXT0;
00115             i = 0;
00116             _waitAfterMs = 10;
00117             break;
00118         case TODO_SEND_TEXT0:
00119             if (_pText0 && _pText0[i]) write(_pText0[i++]);
00120             else                       _todo = TODO_SEND_CURSOR1;
00121             _waitAfterMs = 5;
00122             break;
00123         case TODO_SEND_CURSOR1:
00124             command(0xc0);
00125             _todo = TODO_SEND_TEXT1;
00126             i = 0;
00127             _waitAfterMs = 10;
00128             break;
00129         case TODO_SEND_TEXT1:
00130             if (_pText1 && _pText1[i]) write(_pText1[i++]);
00131             else                       _todo = TODO_READY;
00132             _waitAfterMs = 5;
00133             break;
00134     }
00135 }
00136 
00137 void LcdMain()
00138 {
00139     handleDoIt();
00140     static uint32_t msTimerTick = 0;
00141     static char pTime0[16];
00142     static char pTime1[16];
00143     if (MsTimerRepetitive(&msTimerTick, 1000) && _todo == TODO_READY)
00144     {
00145         struct tm tm;
00146         ClkNowTmUtc(&tm);
00147         strftime(pTime0, sizeof(pTime0), "%a, %d %b %Y", &tm);
00148         strftime(pTime1, sizeof(pTime1), "%H:%M:%S GMT", &tm);
00149         LcdSendText(pTime0, pTime1);
00150     }
00151 }