Demo Application for the Celeritous Breakout Board

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //Celeritous Technical Services, Corp.  2012
00002 //Mattehw R. Gattis
00003 
00004 #include "math.h"
00005 #include "ST7565R.h"
00006 #include "QEI.h"
00007 #include "L25AA02EA48.h"
00008 #include "EthernetNetIf.h"
00009 #include "mbed.h"
00010 #include "defines.h"
00011 #include "HTTPServer.h"
00012 #include "RPCFunction.h"
00013 #include "RPCVariable.h"
00014 
00015 ST7565R lcd(p12,p29,p7,p5, p30 , true);
00016 L25AA02EA48 mac_pins(P0_9,P0_8,P0_7,P0_18);        //pins to MAC address chip on breakout board
00017 QEI OE(p14,p28,NC,1,QEI::X4_ENCODING);
00018 
00019 //Timer keyttime;
00020 Ticker net_interrupt;
00021 volatile int inputnav;
00022 volatile char *macAddress;
00023 volatile unsigned int ethresult=(~0);
00024 IpAddr ip;
00025 float myObj;
00026 RPCVariable<float> MyVar(&myObj, "MyVar");
00027 
00028 EthernetNetIf eth;
00029 HTTPServer svr;
00030 LocalFileSystem fs("webfs");
00031 
00032 void print_menu(char **);
00033 void print_sensors(char line);
00034 void main_menu();
00035 void key_handle();
00036 void OE_menu();
00037 void eth_menu();
00038 void gfx_demo();
00039 void net_poll();
00040 void http_setup();
00041 
00042 extern "C" void mbed_mac_address(char *mac) {    //calling my own mbed_mac_address function to use the Breakout board's MAC chip
00043     for (int i=0; i<6; i++) {mac[i] = macAddress[i];}
00044 }
00045 
00046 int main()
00047 {
00048     MyVar.write(3.14159);
00049     NAV_UP_int.fall(&key_handle);
00050     NAV_LEFT_int.fall(&key_handle);
00051     NAV_DOWN_int.fall(&key_handle);
00052     NAV_RIGHT_int.fall(&key_handle);
00053     NAV_PRESS_int.fall(&key_handle);
00054     //keyttime.start();
00055     main_menu();
00056 }
00057 
00058 void print_menu(char **buffer) {
00059     char i;
00060     for (i=0;i<4;i++) {
00061         lcd.moveto(2,i);
00062         lcd.printf("%s",buffer[i]);
00063     }
00064 }
00065 
00066 void print_sensors(signed char line) {
00067     AnalogIn vtemp(p15);
00068     AnalogIn vrh(p16);
00069     AnalogIn vlight(p17);
00070     float temp;
00071     
00072     temp=0.1*(vtemp.read()*3300)-60;
00073     if (line<=1) {lcd.moveto(9,1-line);lcd.printf("%0.2f C",temp);}
00074     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));}
00075     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()))));}
00076     return;
00077 }
00078 
00079 void main_menu() {
00080     char *buffer[]={"Web Server","Temp :","Humid:","Light:","Optical Encoder","Graphics Demo"};
00081     signed char cursor=0,line=0;
00082     
00083     while (1) {
00084         lcd.clearBuffer();
00085         if (cursor>line+2) {line=cursor-2;}
00086         if (cursor<=line) {line=cursor-1;}
00087         if (line<0) {line=0;}
00088         if (line>2) {line=2;}
00089         print_menu(&buffer[line]);
00090         print_sensors(line);
00091         lcd.moveto(0,cursor-line);
00092         lcd.printf("->");
00093         lcd.swapBuffers();
00094         if (inputnav&NAV_UP && cursor>0) {cursor--;}
00095         if (inputnav&NAV_DOWN && cursor<5) {cursor++;}
00096         if (inputnav==NAV_PRESS && cursor==4) {OE_menu();}
00097         if (inputnav==NAV_PRESS && cursor==0) {eth_menu();}
00098         if (inputnav==NAV_PRESS && cursor==5) {gfx_demo();}
00099         inputnav=0;
00100         wait_ms(20);
00101     }
00102 }
00103 
00104 void key_handle() {
00105     wait_ms(20);
00106     /*if (keyttime.read_ms()>100) {*/inputnav=(~NAVIn.read())&0x1F;//keyttime.reset();}
00107 }
00108 
00109 void OE_menu() {
00110     PwmOut led(LED1);
00111     char *buffer[]={"","","OE Pulse:","Back"};
00112     char i,j,cursor=2;
00113     int last_pulse=0;
00114     int pulse=0;
00115     float output=0.0;
00116     
00117     OE.reset();
00118     while (1) {
00119         lcd.clearBuffer();
00120 
00121         print_menu(buffer);
00122         lcd.moveto(0,cursor);
00123         lcd.printf("->");
00124 
00125         lcd.moveto(12,2);
00126         lcd.printf("%d",pulse);
00127         output=-((float)pulse)/128;
00128         if (output>1.0f) {output=1.0f;}
00129         if (output<0.0f) {output=0.0f;}
00130         led=output;
00131         for (j=0;j<(int)(output*128);j++) {
00132             for (i=0;i<7;i++) {
00133                 lcd.clearpixel(j,i);
00134             }
00135         }
00136 
00137         lcd.swapBuffers();
00138 
00139         inputnav=0;
00140         while (!inputnav && pulse==last_pulse) {pulse=OE.getPulses();}
00141         if (inputnav&NAV_UP && cursor>2) {cursor--;}
00142         if (inputnav&NAV_DOWN && cursor<3) {cursor++;}
00143         if (inputnav==NAV_PRESS && cursor==3) {
00144             wait_ms(200);
00145             led=0.0f;
00146             inputnav=0;
00147             return;
00148         }
00149         
00150         last_pulse=pulse;
00151     }
00152 }
00153 
00154 void gfx_demo() {  
00155     unsigned int iteration=0;
00156     unsigned int pixel=0;    
00157     
00158     unsigned int x=0;
00159     unsigned int y=0;
00160     
00161     inputnav=0;
00162     while(inputnav!=NAV_PRESS) 
00163     {
00164     
00165         lcd.clearBuffer();
00166         wait_ms(30);
00167         for(int i=0; i<32; i++)
00168         {
00169             for (int j=0; j<128; j++)
00170             {
00171                 pixel = (i+iteration+y)*(j+iteration+x);
00172                 pixel &= 15;
00173                                                    
00174                 if (pixel == 0) lcd.clearpixel(j,i);
00175             }
00176         }
00177 
00178         iteration++;
00179         lcd.swapBuffers();
00180     }
00181     wait_ms(200);
00182     inputnav=0;
00183 }
00184 
00185 void eth_menu() {
00186     lcd.clearBuffer();
00187     if (ethresult==(~0)) {
00188         lcd.moveto(0,0);lcd.printf("Initializing...");
00189         lcd.swapBuffers();
00190         lcd.clearBuffer();
00191         macAddress=mac_pins.getMacAddress();
00192     }
00193     lcd.moveto(0,0);
00194     lcd.printf("Mac:%02X:%02X:%02X:%02X:%02X:%02X", macAddress[0], macAddress[1], macAddress[2], macAddress[3], macAddress[4], macAddress[5]);
00195     lcd.moveto(0,1);
00196     if (ethresult==(~0)) {ethresult=eth.setup();}
00197     if (ethresult==ETH_OK) {
00198         ip=eth.getIp();
00199         lcd.printf("IP :%d.%d.%d.%d",ip[0],ip[1],ip[2],ip[3]);
00200         http_setup();
00201         net_interrupt.attach_us(&net_poll,2000);
00202     }
00203     else {
00204         lcd.printf("ERROR on startup!");
00205         lcd.moveto(0,2);
00206         lcd.printf("Reset and try again.");
00207     }
00208     lcd.moveto(0,3);
00209     lcd.printf("->Back");
00210     lcd.swapBuffers();
00211     inputnav=0;
00212     //simply wait for the user to press the middle nav key
00213     while (inputnav!=NAV_PRESS) {wait_ms(20);}
00214     wait_ms(200);
00215     inputnav=0;
00216     return;
00217 }
00218 
00219 void net_poll() {
00220     Net::poll();
00221 }
00222 
00223 void http_setup() {
00224     Base::add_rpc_class<AnalogIn>();
00225 
00226     FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
00227 
00228     svr.addHandler<RPCHandler>("/rpc");
00229     svr.addHandler<FSHandler>("/"); //Default handler
00230 
00231     svr.bind(80);
00232 }