| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | //***************************************************************************** |
| 2 | // File Name : spyglasstest.c |
||
| 3 | // |
||
| 4 | // Title : example usage of the Spyglass I2C-controller user interface |
||
| 5 | // Revision : 1.0 |
||
| 6 | // Notes : |
||
| 7 | // Target MCU : Atmel AVR series |
||
| 8 | // Editor Tabs : 4 |
||
| 9 | // |
||
| 10 | // Revision History: |
||
| 11 | // When Who Description of change |
||
| 12 | // ----------- ----------- ----------------------- |
||
| 13 | // 28-Aug-2005 pstang Created the program |
||
| 14 | //***************************************************************************** |
||
| 15 | |||
| 16 | #include <avr/io.h> |
||
| 17 | |||
| 18 | #include "global.h" |
||
| 19 | #include "timer.h" |
||
| 20 | #include "uart.h" |
||
| 21 | #include "rprintf.h" |
||
| 22 | #include "vt100.h" |
||
| 23 | #include "lcd.h" |
||
| 24 | #include "debug.h" |
||
| 25 | #include "i2c.h" |
||
| 26 | #include "spyglass.h" |
||
| 27 | |||
| 28 | // defines |
||
| 29 | #define TIMER_PRESCALE 1024 |
||
| 30 | #define TIMER_INTERVAL (F_CPU/TIMER_PRESCALE/100) |
||
| 31 | |||
| 32 | // global variables |
||
| 33 | static volatile unsigned long UptimeMs; |
||
| 34 | u08 Contrast=0; |
||
| 35 | |||
| 36 | // prototypes |
||
| 37 | void spyglassTest(void); |
||
| 38 | void systickHandler(void); |
||
| 39 | void i2cDeviceSearch(void); |
||
| 40 | void serviceLocal(void); |
||
| 41 | |||
| 42 | int main(void) |
||
| 43 | { |
||
| 44 | timerInit(); // initializing timers |
||
| 45 | uartInit(); // initializing serial port |
||
| 46 | uartSetBaudRate(115200); // set serial port baud rate |
||
| 47 | rprintfInit(uartSendByte); // direct rprintf() output to go to serial port |
||
| 48 | timerPause(100); // wait for a moment |
||
| 49 | |||
| 50 | // print welcome message |
||
| 51 | rprintf("\r\n\n\nWelcome to the Spyglass UI test.\r\n"); |
||
| 52 | |||
| 53 | // begin test application |
||
| 54 | spyglassTest(); |
||
| 55 | |||
| 56 | return 0; |
||
| 57 | } |
||
| 58 | |||
| 59 | void spyglassTest(void) |
||
| 60 | { |
||
| 61 | // initializing Spyglass interface and I2C bus |
||
| 62 | rprintf("Initializing Spyglass communication..."); |
||
| 63 | spyglassInit(); |
||
| 64 | spyglassLcdInit(); |
||
| 65 | rprintf("Done!\r\n"); |
||
| 66 | rprintf("Printing 'Hello World!' message to spyglass LCD.\r\n"); |
||
| 67 | |||
| 68 | rprintfInit(spyglassLcdWriteChar); |
||
| 69 | spyglassLcdGotoXY(0,0); |
||
| 70 | rprintfProgStrM("Hello World!"); |
||
| 71 | rprintfInit(uartSendByte); |
||
| 72 | |||
| 73 | timerPause(1000); |
||
| 74 | |||
| 75 | // initialize systick timer |
||
| 76 | rprintf("Initializing Periodic Timer\r\n"); |
||
| 77 | timer2SetPrescaler(TIMERRTC_CLK_DIV1024); |
||
| 78 | // attach the 'systickHandler' so that it gets called every time Timer2 overflows |
||
| 79 | timerAttach(TIMER2OVERFLOW_INT, systickHandler); |
||
| 80 | |||
| 81 | rprintf("Starting local command prompt. Type '?' to get help.\r\n"); |
||
| 82 | rprintf("cmd>"); |
||
| 83 | |||
| 84 | while(1) |
||
| 85 | { |
||
| 86 | serviceLocal(); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | void systickHandler(void) |
||
| 91 | { |
||
| 92 | u16 tms; |
||
| 93 | u08 ts,tm,th; |
||
| 94 | u08 pb; |
||
| 95 | |||
| 96 | // set timer for 10ms interval |
||
| 97 | TCNT2 = (unsigned char)-TIMER_INTERVAL; |
||
| 98 | // count up on uptime |
||
| 99 | UptimeMs += 10; |
||
| 100 | |||
| 101 | // if we at a 100ths millisecond interval, update the display |
||
| 102 | if(!(UptimeMs % 100)) |
||
| 103 | { |
||
| 104 | // redirect rprintf() output to spyglass LCD |
||
| 105 | rprintfInit(spyglassLcdWriteChar); |
||
| 106 | |||
| 107 | // print banner message |
||
| 108 | spyglassLcdGotoXY(0,0); |
||
| 109 | rprintfProgStrM("**** SpyglassUI ****"); |
||
| 110 | |||
| 111 | // read pushbuttons and take appropriate action |
||
| 112 | pb = spyglassGetPushbuttons(); |
||
| 113 | spyglassLcdGotoXY(0,1); |
||
| 114 | rprintf("Buttons: "); |
||
| 115 | rprintfNum(2,8,FALSE,'0',pb); |
||
| 116 | |||
| 117 | if((pb & 0x01) && (Contrast < 255)) |
||
| 118 | Contrast++; |
||
| 119 | if((pb & 0x02) && (Contrast > 0)) |
||
| 120 | Contrast--; |
||
| 121 | if(pb & 0x08) |
||
| 122 | spyglassSetLeds(0x01); |
||
| 123 | if(pb & 0x10) |
||
| 124 | spyglassSetLeds(0x02); |
||
| 125 | |||
| 126 | // show display contrast |
||
| 127 | spyglassLcdGotoXY(0,2); |
||
| 128 | rprintf("LCD Contrast: "); |
||
| 129 | rprintfNum(10,3,FALSE,' ',Contrast); |
||
| 130 | spyglassSetLcdContrast(Contrast); |
||
| 131 | |||
| 132 | // show time |
||
| 133 | tms = (UptimeMs)%1000; |
||
| 134 | ts = (UptimeMs/1000)%60; |
||
| 135 | tm = (UptimeMs/60000l)%60; |
||
| 136 | th = (UptimeMs/3600000l); |
||
| 137 | spyglassLcdGotoXY(0,3); |
||
| 138 | rprintf("Time:"); |
||
| 139 | rprintfNum(10,3,FALSE,' ',th); |
||
| 140 | rprintfChar(':'); |
||
| 141 | rprintfNum(10,2,FALSE,'0',tm); |
||
| 142 | rprintfChar(':'); |
||
| 143 | rprintfNum(10,2,FALSE,'0',ts); |
||
| 144 | rprintfChar('.'); |
||
| 145 | rprintfNum(10,3,FALSE,'0',tms); |
||
| 146 | rprintf("ms"); |
||
| 147 | |||
| 148 | // return rprintf() output to serial port |
||
| 149 | rprintfInit(uartSendByte); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | void serviceLocal(void) |
||
| 154 | { |
||
| 155 | int c; |
||
| 156 | |||
| 157 | // a little command-prompt utility to play with the spyglass UI |
||
| 158 | // all commands are single characters |
||
| 159 | |||
| 160 | if( (c = uartGetByte()) != -1) |
||
| 161 | { |
||
| 162 | // echo command to terminal |
||
| 163 | uartSendByte(c); |
||
| 164 | // process command |
||
| 165 | switch(c) |
||
| 166 | { |
||
| 167 | case 'i': spyglassLcdInit(); break; |
||
| 168 | case 'h': |
||
| 169 | rprintfInit(spyglassLcdWriteChar); |
||
| 170 | spyglassLcdGotoXY(0,0); |
||
| 171 | rprintf("*** HELLO WORLD ***"); |
||
| 172 | rprintfInit(uartSendByte); |
||
| 173 | break; |
||
| 174 | case 'p': |
||
| 175 | rprintf("Pushbutton State: 0x%x\r\n", spyglassGetPushbuttons()); |
||
| 176 | break; |
||
| 177 | case '+': if(Contrast<255) Contrast++; rprintf("\r\nLCD Contrast: %d\r\n", Contrast); spyglassSetLcdContrast(Contrast); break; |
||
| 178 | case '-': if(Contrast>0) Contrast--; rprintf("\r\nLCD Contrast: %d\r\n", Contrast); spyglassSetLcdContrast(Contrast); break; |
||
| 179 | case 'l': spyglassSetLeds(0x00); break; |
||
| 180 | case 'L': spyglassSetLeds(0xFF); break; |
||
| 181 | case 'b': spyglassSetBeeper(0); break; |
||
| 182 | case 'B': spyglassSetBeeper(1); break; |
||
| 183 | case 'x': |
||
| 184 | i2cDeviceSearch(); |
||
| 185 | break; |
||
| 186 | case '?': |
||
| 187 | rprintfProgStrM("\r\n"); |
||
| 188 | rprintfProgStrM("--- Spyglass Commands: ---\r\n"); |
||
| 189 | rprintfProgStrM("(i) Initialize Spyglass LCD\r\n"); |
||
| 190 | rprintfProgStrM("(h) Print 'Hello World' message to Spyglass LCD\r\n"); |
||
| 191 | rprintfProgStrM("(p) Get Spyglass pushbutton state\r\n"); |
||
| 192 | rprintfProgStrM("(+) Increase contrast number (lightens contrast)\r\n"); |
||
| 193 | rprintfProgStrM("(-) Decrease contrast number (darkens contrast)\r\n"); |
||
| 194 | rprintfProgStrM("(l) Set Spyglass User LEDs to OFF\r\n"); |
||
| 195 | rprintfProgStrM("(L) Set Spyglass User LEDs to ON\r\n"); |
||
| 196 | rprintfProgStrM("(b) Set Spyglass beeper to OFF\r\n"); |
||
| 197 | rprintfProgStrM("(B) Set Spyglass beeper to ON\r\n"); |
||
| 198 | rprintfProgStrM("--- General Commands: ---\r\n"); |
||
| 199 | rprintfProgStrM("(x) Search for I2C devices on the bus\r\n"); |
||
| 200 | rprintfProgStrM("(?) Help\r\n"); |
||
| 201 | break; |
||
| 202 | case '\r': |
||
| 203 | default: |
||
| 204 | break; |
||
| 205 | } |
||
| 206 | // print new prompt |
||
| 207 | rprintfProgStrM("\r\ncmd>"); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | void i2cDeviceSearch(void) |
||
| 212 | { |
||
| 213 | u08 i2cAddr; |
||
| 214 | u08 i2cStat; |
||
| 215 | |||
| 216 | // this function searches all device addresses on the I2C bus |
||
| 217 | // and returns addresses that are live (have a device) |
||
| 218 | |||
| 219 | rprintf("\r\nSearching for I2c devices on bus\r\n"); |
||
| 220 | |||
| 221 | for(i2cAddr = 0; i2cAddr<0x80; i2cAddr+=2) |
||
| 222 | { |
||
| 223 | i2cStat = i2cMasterSendNI(i2cAddr, 0, 0); |
||
| 224 | if(i2cStat == I2C_OK) |
||
| 225 | rprintf("Device present at address 0x%x\r\n", i2cAddr); |
||
| 226 | } |
||
| 227 | rprintf("Search complete.\r\n"); |
||
| 228 | } |
Powered by WebSVN v2.8.3