Allen Litton
/
BreakoutBoardDemo
Demo Application for the Celeritous Breakout Board
main.cpp@0:1a3da73fe36a, 2012-05-18 (annotated)
- Committer:
- celeritous
- Date:
- Fri May 18 03:55:10 2012 +0000
- Revision:
- 0:1a3da73fe36a
Celeritous_BreakoutBoardDemo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
celeritous | 0:1a3da73fe36a | 1 | //Celeritous Technical Services, Corp. 2012 |
celeritous | 0:1a3da73fe36a | 2 | //Mattehw R. Gattis |
celeritous | 0:1a3da73fe36a | 3 | |
celeritous | 0:1a3da73fe36a | 4 | #include "math.h" |
celeritous | 0:1a3da73fe36a | 5 | #include "ST7565R.h" |
celeritous | 0:1a3da73fe36a | 6 | #include "QEI.h" |
celeritous | 0:1a3da73fe36a | 7 | #include "L25AA02EA48.h" |
celeritous | 0:1a3da73fe36a | 8 | #include "EthernetNetIf.h" |
celeritous | 0:1a3da73fe36a | 9 | #include "mbed.h" |
celeritous | 0:1a3da73fe36a | 10 | #include "defines.h" |
celeritous | 0:1a3da73fe36a | 11 | #include "HTTPServer.h" |
celeritous | 0:1a3da73fe36a | 12 | #include "RPCFunction.h" |
celeritous | 0:1a3da73fe36a | 13 | #include "RPCVariable.h" |
celeritous | 0:1a3da73fe36a | 14 | |
celeritous | 0:1a3da73fe36a | 15 | ST7565R lcd(p12,p29,p7,p5, p30 , true); |
celeritous | 0:1a3da73fe36a | 16 | L25AA02EA48 mac_pins(P0_9,P0_8,P0_7,P0_18); //pins to MAC address chip on breakout board |
celeritous | 0:1a3da73fe36a | 17 | QEI OE(p14,p28,NC,1,QEI::X4_ENCODING); |
celeritous | 0:1a3da73fe36a | 18 | |
celeritous | 0:1a3da73fe36a | 19 | //Timer keyttime; |
celeritous | 0:1a3da73fe36a | 20 | Ticker net_interrupt; |
celeritous | 0:1a3da73fe36a | 21 | volatile int inputnav; |
celeritous | 0:1a3da73fe36a | 22 | volatile char *macAddress; |
celeritous | 0:1a3da73fe36a | 23 | volatile unsigned int ethresult=(~0); |
celeritous | 0:1a3da73fe36a | 24 | IpAddr ip; |
celeritous | 0:1a3da73fe36a | 25 | float myObj; |
celeritous | 0:1a3da73fe36a | 26 | RPCVariable<float> MyVar(&myObj, "MyVar"); |
celeritous | 0:1a3da73fe36a | 27 | |
celeritous | 0:1a3da73fe36a | 28 | EthernetNetIf eth; |
celeritous | 0:1a3da73fe36a | 29 | HTTPServer svr; |
celeritous | 0:1a3da73fe36a | 30 | LocalFileSystem fs("webfs"); |
celeritous | 0:1a3da73fe36a | 31 | |
celeritous | 0:1a3da73fe36a | 32 | void print_menu(char **); |
celeritous | 0:1a3da73fe36a | 33 | void print_sensors(char line); |
celeritous | 0:1a3da73fe36a | 34 | void main_menu(); |
celeritous | 0:1a3da73fe36a | 35 | void key_handle(); |
celeritous | 0:1a3da73fe36a | 36 | void OE_menu(); |
celeritous | 0:1a3da73fe36a | 37 | void eth_menu(); |
celeritous | 0:1a3da73fe36a | 38 | void gfx_demo(); |
celeritous | 0:1a3da73fe36a | 39 | void net_poll(); |
celeritous | 0:1a3da73fe36a | 40 | void http_setup(); |
celeritous | 0:1a3da73fe36a | 41 | |
celeritous | 0:1a3da73fe36a | 42 | extern "C" void mbed_mac_address(char *mac) { //calling my own mbed_mac_address function to use the Breakout board's MAC chip |
celeritous | 0:1a3da73fe36a | 43 | for (int i=0; i<6; i++) {mac[i] = macAddress[i];} |
celeritous | 0:1a3da73fe36a | 44 | } |
celeritous | 0:1a3da73fe36a | 45 | |
celeritous | 0:1a3da73fe36a | 46 | int main() |
celeritous | 0:1a3da73fe36a | 47 | { |
celeritous | 0:1a3da73fe36a | 48 | MyVar.write(3.14159); |
celeritous | 0:1a3da73fe36a | 49 | NAV_UP_int.fall(&key_handle); |
celeritous | 0:1a3da73fe36a | 50 | NAV_LEFT_int.fall(&key_handle); |
celeritous | 0:1a3da73fe36a | 51 | NAV_DOWN_int.fall(&key_handle); |
celeritous | 0:1a3da73fe36a | 52 | NAV_RIGHT_int.fall(&key_handle); |
celeritous | 0:1a3da73fe36a | 53 | NAV_PRESS_int.fall(&key_handle); |
celeritous | 0:1a3da73fe36a | 54 | //keyttime.start(); |
celeritous | 0:1a3da73fe36a | 55 | main_menu(); |
celeritous | 0:1a3da73fe36a | 56 | } |
celeritous | 0:1a3da73fe36a | 57 | |
celeritous | 0:1a3da73fe36a | 58 | void print_menu(char **buffer) { |
celeritous | 0:1a3da73fe36a | 59 | char i; |
celeritous | 0:1a3da73fe36a | 60 | for (i=0;i<4;i++) { |
celeritous | 0:1a3da73fe36a | 61 | lcd.moveto(2,i); |
celeritous | 0:1a3da73fe36a | 62 | lcd.printf("%s",buffer[i]); |
celeritous | 0:1a3da73fe36a | 63 | } |
celeritous | 0:1a3da73fe36a | 64 | } |
celeritous | 0:1a3da73fe36a | 65 | |
celeritous | 0:1a3da73fe36a | 66 | void print_sensors(signed char line) { |
celeritous | 0:1a3da73fe36a | 67 | AnalogIn vtemp(p15); |
celeritous | 0:1a3da73fe36a | 68 | AnalogIn vrh(p16); |
celeritous | 0:1a3da73fe36a | 69 | AnalogIn vlight(p17); |
celeritous | 0:1a3da73fe36a | 70 | float temp; |
celeritous | 0:1a3da73fe36a | 71 | |
celeritous | 0:1a3da73fe36a | 72 | temp=0.1*(vtemp.read()*3300)-60; |
celeritous | 0:1a3da73fe36a | 73 | if (line<=1) {lcd.moveto(9,1-line);lcd.printf("%0.2f C",temp);} |
celeritous | 0:1a3da73fe36a | 74 | if (line<=2) {lcd.moveto(9,2-line);lcd.printf("%0.2f %%",((25*(20000*(3.3*vrh.read())-9999))/10494)/(1.0546-0.0026*temp));} |
celeritous | 0:1a3da73fe36a | 75 | if (line<=3) {lcd.moveto(9,3-line);lcd.printf("%0.2f lux",0.0003f*pow(2.71828183f,9.2f*(1.6f-(3.3f*vlight.read()))));} |
celeritous | 0:1a3da73fe36a | 76 | return; |
celeritous | 0:1a3da73fe36a | 77 | } |
celeritous | 0:1a3da73fe36a | 78 | |
celeritous | 0:1a3da73fe36a | 79 | void main_menu() { |
celeritous | 0:1a3da73fe36a | 80 | char *buffer[]={"Web Server","Temp :","Humid:","Light:","Optical Encoder","Graphics Demo"}; |
celeritous | 0:1a3da73fe36a | 81 | signed char cursor=0,line=0; |
celeritous | 0:1a3da73fe36a | 82 | |
celeritous | 0:1a3da73fe36a | 83 | while (1) { |
celeritous | 0:1a3da73fe36a | 84 | lcd.clearBuffer(); |
celeritous | 0:1a3da73fe36a | 85 | if (cursor>line+2) {line=cursor-2;} |
celeritous | 0:1a3da73fe36a | 86 | if (cursor<=line) {line=cursor-1;} |
celeritous | 0:1a3da73fe36a | 87 | if (line<0) {line=0;} |
celeritous | 0:1a3da73fe36a | 88 | if (line>2) {line=2;} |
celeritous | 0:1a3da73fe36a | 89 | print_menu(&buffer[line]); |
celeritous | 0:1a3da73fe36a | 90 | print_sensors(line); |
celeritous | 0:1a3da73fe36a | 91 | lcd.moveto(0,cursor-line); |
celeritous | 0:1a3da73fe36a | 92 | lcd.printf("->"); |
celeritous | 0:1a3da73fe36a | 93 | lcd.swapBuffers(); |
celeritous | 0:1a3da73fe36a | 94 | if (inputnav&NAV_UP && cursor>0) {cursor--;} |
celeritous | 0:1a3da73fe36a | 95 | if (inputnav&NAV_DOWN && cursor<5) {cursor++;} |
celeritous | 0:1a3da73fe36a | 96 | if (inputnav==NAV_PRESS && cursor==4) {OE_menu();} |
celeritous | 0:1a3da73fe36a | 97 | if (inputnav==NAV_PRESS && cursor==0) {eth_menu();} |
celeritous | 0:1a3da73fe36a | 98 | if (inputnav==NAV_PRESS && cursor==5) {gfx_demo();} |
celeritous | 0:1a3da73fe36a | 99 | inputnav=0; |
celeritous | 0:1a3da73fe36a | 100 | wait_ms(20); |
celeritous | 0:1a3da73fe36a | 101 | } |
celeritous | 0:1a3da73fe36a | 102 | } |
celeritous | 0:1a3da73fe36a | 103 | |
celeritous | 0:1a3da73fe36a | 104 | void key_handle() { |
celeritous | 0:1a3da73fe36a | 105 | wait_ms(20); |
celeritous | 0:1a3da73fe36a | 106 | /*if (keyttime.read_ms()>100) {*/inputnav=(~NAVIn.read())&0x1F;//keyttime.reset();} |
celeritous | 0:1a3da73fe36a | 107 | } |
celeritous | 0:1a3da73fe36a | 108 | |
celeritous | 0:1a3da73fe36a | 109 | void OE_menu() { |
celeritous | 0:1a3da73fe36a | 110 | PwmOut led(LED1); |
celeritous | 0:1a3da73fe36a | 111 | char *buffer[]={"","","OE Pulse:","Back"}; |
celeritous | 0:1a3da73fe36a | 112 | char i,j,cursor=2; |
celeritous | 0:1a3da73fe36a | 113 | int last_pulse=0; |
celeritous | 0:1a3da73fe36a | 114 | int pulse=0; |
celeritous | 0:1a3da73fe36a | 115 | float output=0.0; |
celeritous | 0:1a3da73fe36a | 116 | |
celeritous | 0:1a3da73fe36a | 117 | OE.reset(); |
celeritous | 0:1a3da73fe36a | 118 | while (1) { |
celeritous | 0:1a3da73fe36a | 119 | lcd.clearBuffer(); |
celeritous | 0:1a3da73fe36a | 120 | |
celeritous | 0:1a3da73fe36a | 121 | print_menu(buffer); |
celeritous | 0:1a3da73fe36a | 122 | lcd.moveto(0,cursor); |
celeritous | 0:1a3da73fe36a | 123 | lcd.printf("->"); |
celeritous | 0:1a3da73fe36a | 124 | |
celeritous | 0:1a3da73fe36a | 125 | lcd.moveto(12,2); |
celeritous | 0:1a3da73fe36a | 126 | lcd.printf("%d",pulse); |
celeritous | 0:1a3da73fe36a | 127 | output=-((float)pulse)/128; |
celeritous | 0:1a3da73fe36a | 128 | if (output>1.0f) {output=1.0f;} |
celeritous | 0:1a3da73fe36a | 129 | if (output<0.0f) {output=0.0f;} |
celeritous | 0:1a3da73fe36a | 130 | led=output; |
celeritous | 0:1a3da73fe36a | 131 | for (j=0;j<(int)(output*128);j++) { |
celeritous | 0:1a3da73fe36a | 132 | for (i=0;i<7;i++) { |
celeritous | 0:1a3da73fe36a | 133 | lcd.clearpixel(j,i); |
celeritous | 0:1a3da73fe36a | 134 | } |
celeritous | 0:1a3da73fe36a | 135 | } |
celeritous | 0:1a3da73fe36a | 136 | |
celeritous | 0:1a3da73fe36a | 137 | lcd.swapBuffers(); |
celeritous | 0:1a3da73fe36a | 138 | |
celeritous | 0:1a3da73fe36a | 139 | inputnav=0; |
celeritous | 0:1a3da73fe36a | 140 | while (!inputnav && pulse==last_pulse) {pulse=OE.getPulses();} |
celeritous | 0:1a3da73fe36a | 141 | if (inputnav&NAV_UP && cursor>2) {cursor--;} |
celeritous | 0:1a3da73fe36a | 142 | if (inputnav&NAV_DOWN && cursor<3) {cursor++;} |
celeritous | 0:1a3da73fe36a | 143 | if (inputnav==NAV_PRESS && cursor==3) { |
celeritous | 0:1a3da73fe36a | 144 | wait_ms(200); |
celeritous | 0:1a3da73fe36a | 145 | led=0.0f; |
celeritous | 0:1a3da73fe36a | 146 | inputnav=0; |
celeritous | 0:1a3da73fe36a | 147 | return; |
celeritous | 0:1a3da73fe36a | 148 | } |
celeritous | 0:1a3da73fe36a | 149 | |
celeritous | 0:1a3da73fe36a | 150 | last_pulse=pulse; |
celeritous | 0:1a3da73fe36a | 151 | } |
celeritous | 0:1a3da73fe36a | 152 | } |
celeritous | 0:1a3da73fe36a | 153 | |
celeritous | 0:1a3da73fe36a | 154 | void gfx_demo() { |
celeritous | 0:1a3da73fe36a | 155 | unsigned int iteration=0; |
celeritous | 0:1a3da73fe36a | 156 | unsigned int pixel=0; |
celeritous | 0:1a3da73fe36a | 157 | |
celeritous | 0:1a3da73fe36a | 158 | unsigned int x=0; |
celeritous | 0:1a3da73fe36a | 159 | unsigned int y=0; |
celeritous | 0:1a3da73fe36a | 160 | |
celeritous | 0:1a3da73fe36a | 161 | inputnav=0; |
celeritous | 0:1a3da73fe36a | 162 | while(inputnav!=NAV_PRESS) |
celeritous | 0:1a3da73fe36a | 163 | { |
celeritous | 0:1a3da73fe36a | 164 | |
celeritous | 0:1a3da73fe36a | 165 | lcd.clearBuffer(); |
celeritous | 0:1a3da73fe36a | 166 | wait_ms(30); |
celeritous | 0:1a3da73fe36a | 167 | for(int i=0; i<32; i++) |
celeritous | 0:1a3da73fe36a | 168 | { |
celeritous | 0:1a3da73fe36a | 169 | for (int j=0; j<128; j++) |
celeritous | 0:1a3da73fe36a | 170 | { |
celeritous | 0:1a3da73fe36a | 171 | pixel = (i+iteration+y)*(j+iteration+x); |
celeritous | 0:1a3da73fe36a | 172 | pixel &= 15; |
celeritous | 0:1a3da73fe36a | 173 | |
celeritous | 0:1a3da73fe36a | 174 | if (pixel == 0) lcd.clearpixel(j,i); |
celeritous | 0:1a3da73fe36a | 175 | } |
celeritous | 0:1a3da73fe36a | 176 | } |
celeritous | 0:1a3da73fe36a | 177 | |
celeritous | 0:1a3da73fe36a | 178 | iteration++; |
celeritous | 0:1a3da73fe36a | 179 | lcd.swapBuffers(); |
celeritous | 0:1a3da73fe36a | 180 | } |
celeritous | 0:1a3da73fe36a | 181 | wait_ms(200); |
celeritous | 0:1a3da73fe36a | 182 | inputnav=0; |
celeritous | 0:1a3da73fe36a | 183 | } |
celeritous | 0:1a3da73fe36a | 184 | |
celeritous | 0:1a3da73fe36a | 185 | void eth_menu() { |
celeritous | 0:1a3da73fe36a | 186 | lcd.clearBuffer(); |
celeritous | 0:1a3da73fe36a | 187 | if (ethresult==(~0)) { |
celeritous | 0:1a3da73fe36a | 188 | lcd.moveto(0,0);lcd.printf("Initializing..."); |
celeritous | 0:1a3da73fe36a | 189 | lcd.swapBuffers(); |
celeritous | 0:1a3da73fe36a | 190 | lcd.clearBuffer(); |
celeritous | 0:1a3da73fe36a | 191 | macAddress=mac_pins.getMacAddress(); |
celeritous | 0:1a3da73fe36a | 192 | } |
celeritous | 0:1a3da73fe36a | 193 | lcd.moveto(0,0); |
celeritous | 0:1a3da73fe36a | 194 | lcd.printf("Mac:%02X:%02X:%02X:%02X:%02X:%02X", macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]); |
celeritous | 0:1a3da73fe36a | 195 | lcd.moveto(0,1); |
celeritous | 0:1a3da73fe36a | 196 | if (ethresult==(~0)) {ethresult=eth.setup();} |
celeritous | 0:1a3da73fe36a | 197 | if (ethresult==ETH_OK) { |
celeritous | 0:1a3da73fe36a | 198 | ip=eth.getIp(); |
celeritous | 0:1a3da73fe36a | 199 | lcd.printf("IP :%d.%d.%d.%d",ip[0],ip[1],ip[2],ip[3]); |
celeritous | 0:1a3da73fe36a | 200 | http_setup(); |
celeritous | 0:1a3da73fe36a | 201 | net_interrupt.attach_us(&net_poll,2000); |
celeritous | 0:1a3da73fe36a | 202 | } |
celeritous | 0:1a3da73fe36a | 203 | else { |
celeritous | 0:1a3da73fe36a | 204 | lcd.printf("ERROR on startup!"); |
celeritous | 0:1a3da73fe36a | 205 | lcd.moveto(0,2); |
celeritous | 0:1a3da73fe36a | 206 | lcd.printf("Reset and try again."); |
celeritous | 0:1a3da73fe36a | 207 | } |
celeritous | 0:1a3da73fe36a | 208 | lcd.moveto(0,3); |
celeritous | 0:1a3da73fe36a | 209 | lcd.printf("->Back"); |
celeritous | 0:1a3da73fe36a | 210 | lcd.swapBuffers(); |
celeritous | 0:1a3da73fe36a | 211 | inputnav=0; |
celeritous | 0:1a3da73fe36a | 212 | //simply wait for the user to press the middle nav key |
celeritous | 0:1a3da73fe36a | 213 | while (inputnav!=NAV_PRESS) {wait_ms(20);} |
celeritous | 0:1a3da73fe36a | 214 | wait_ms(200); |
celeritous | 0:1a3da73fe36a | 215 | inputnav=0; |
celeritous | 0:1a3da73fe36a | 216 | return; |
celeritous | 0:1a3da73fe36a | 217 | } |
celeritous | 0:1a3da73fe36a | 218 | |
celeritous | 0:1a3da73fe36a | 219 | void net_poll() { |
celeritous | 0:1a3da73fe36a | 220 | Net::poll(); |
celeritous | 0:1a3da73fe36a | 221 | } |
celeritous | 0:1a3da73fe36a | 222 | |
celeritous | 0:1a3da73fe36a | 223 | void http_setup() { |
celeritous | 0:1a3da73fe36a | 224 | Base::add_rpc_class<AnalogIn>(); |
celeritous | 0:1a3da73fe36a | 225 | |
celeritous | 0:1a3da73fe36a | 226 | FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path |
celeritous | 0:1a3da73fe36a | 227 | |
celeritous | 0:1a3da73fe36a | 228 | svr.addHandler<RPCHandler>("/rpc"); |
celeritous | 0:1a3da73fe36a | 229 | svr.addHandler<FSHandler>("/"); //Default handler |
celeritous | 0:1a3da73fe36a | 230 | |
celeritous | 0:1a3da73fe36a | 231 | svr.bind(80); |
celeritous | 0:1a3da73fe36a | 232 | } |