Show2Me control FW, initial shared version

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Fork of 000_GEO_SHOW2ME_OK_F411RE by Walter Trovo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SDFileSystem.h"
00003 #include "ST7565_LCD.h"
00004 #include "QEI.h"
00005 
00006 #define BAT_GAIN    6.80
00007 #define BAT_OFFS    0.0
00008 
00009 PwmOut      BEEP        (D2);      // Buzzer/speaker (PWM output)
00010 PwmOut      BKL         (D3);      // LCD backlight control (PMW output)
00011 DigitalOut  KAL         (D0);      // Keep-Alive/turn-off
00012 DigitalOut  BTC         (D2);      // Aux BT module control
00013 DigitalIn   Button      (D4);      // Pushbutton (digital input)
00014 AnalogIn    BATT        (A0);      // Battery monitor
00015 AnalogIn    ALS         (A1);      // Ambient Light sensor
00016  
00017 SDFileSystem sd(PB_15, PB_14, PB_13, PB_1, "sd"); // MOSI, MISO, SCK, CS
00018 
00019 // Quadrature encoder
00020 QEI Wheel(D5, D6, NC, 16);
00021 
00022 // Tickers
00023 Ticker Sec_Beat;            // Timer ticker
00024 Ticker Display_Refresh;     // Display refresh ticker
00025 
00026 //Serial ports
00027 Serial PC(USBTX, USBRX);    // Virtual COM via USB
00028 //Serial BT_module(D1, D0);    // BlueTooth module 
00029 
00030 
00031 extern unsigned int buffer[128*64/8];   // RAM buffer used by LCD
00032 time_t seconds;     // timestamp
00033 char Text[40]="";   // Text string used by LCD 
00034 float Vbatt, ALLevel;   // battery votage and ambient light level
00035 
00036 // ------------------- Prototypes ----------------------- 
00037 void Timer_tick(void);
00038 void Update_Display(void);
00039 void Set_Time(void);
00040 void Read_Voltages(void);
00041 void PowerOff(void);
00042 
00043 int main() 
00044 {
00045 
00046     KAL = 1;      // ensure self-sustained power
00047     //Button.mode(PullUp);      // enable pushbutton pull-up
00048     BKL.period_ms(3);         // set LCD backlight PWM
00049     BKL.write(1.0);  
00050     BEEP.period_us(2300);      // set initial buzzer period and duty-cycle
00051     BEEP.write(0.2);
00052     Wheel.reset();        // clear encoder
00053     LCD_reset();   
00054 
00055     // splash screen with date and time
00056     sprintf(Text,__DATE__);
00057     LCD_drawstring(buffer, 60, 5, Text);
00058     sprintf(Text,__TIME__);
00059     LCD_drawstring(buffer, 78, 6, Text);  
00060     LCD_write_buffer(buffer);
00061 
00062 
00063     // enable LCD refresh ticker        
00064     Display_Refresh.attach(&Update_Display, 0.3);
00065 
00066     if(Button)   // if pushbutton is pressed
00067         Set_Time();  // set RTC time and date  
00068 
00069     wait(2);
00070     Clear_buffer(buffer);
00071     BKL.write(0.5);
00072     
00073     //PowerOff();   // Power-off test
00074 
00075 
00076     // enable sec-beat ticker  
00077     Sec_Beat.attach(&Timer_tick, 1);
00078       
00079 
00080 
00081     printf("Hello World!\n");    
00082     mkdir("/sd/system", 0777);
00083     FILE *fp = fopen("/sd/system/sdlog.txt", "w");
00084     if(fp == NULL) {
00085         error("Could not open file for write\n");
00086     }
00087     fprintf(fp, "Hello fun SD Card World!");
00088     fclose(fp); 
00089     printf("Goodbye World!\n");
00090     
00091     while(1) 
00092     {
00093        // dance 
00094     }    
00095     
00096     
00097 }
00098 
00099 
00100 //===========================================================================
00101 
00102 // ------------- Called every second ----------------------
00103 
00104 void Timer_tick() 
00105 { 
00106     seconds = time(NULL); 
00107     strftime(Text, 50, "%d-%b-%Y  %H:%M:%S", localtime(&seconds));
00108     LCD_drawstring(buffer, 0, 0, Text);
00109 
00110     // read voltages 
00111     Read_Voltages();
00112     
00113     // write values to buffer
00114     sprintf(Text,"VBATT= %4.2f", Vbatt);
00115     LCD_drawstring(buffer, 0, 2, Text);    
00116     sprintf(Text,"ALL= %4.2f", ALLevel);
00117     LCD_drawstring(buffer, 0, 3, Text); 
00118                               
00119     // Send data to COM port
00120     //PC.printf("Tset= %3.0f, Tpit= %3.0f, Tmeat= %3.0f, PWM= %3.0f\n",Tset, Tpit, Tmeat, FanSpeed); 
00121 
00122     //LCD_write_buffer(buffer);   // LCD update
00123                          
00124     return;
00125 }
00126 
00127 
00128 //---------------------------------------------------------------------------
00129 void Update_Display(void)
00130 {
00131     LCD_write_buffer(buffer);   // LCD update 
00132     return;  
00133 }
00134 
00135 //---------------------------------------------------------------------------
00136 void Set_Time(void)
00137 {
00138     uint8_t Year=0, Month=0, Day=0, Hours=0, Mins=0, Secs=0;
00139     time_t seconds;
00140     struct tm t;
00141 
00142     sprintf(Text,"TIME & DATE SETTING");
00143     LCD_drawstring(buffer, 0, 0, Text); 
00144     
00145     // Set year
00146     while(Button);
00147     wait_ms(50);  
00148 
00149     while(!Button)
00150     { 
00151         if(int(Wheel.getPulses())<0)
00152             Wheel.reset();
00153         Year = (uint8_t)(Wheel.getPulses());
00154         
00155         if(Year>99) 
00156             Wheel.reset();
00157              
00158         sprintf(Text, "Year: %2d", Year);
00159         LCD_drawstring(buffer, 0, 2, Text);
00160    
00161     }
00162 
00163     // Set month
00164     while(Button);
00165     wait_ms(50);  
00166     Wheel.reset();
00167     while(!Button)
00168     { 
00169         if(int(Wheel.getPulses())<0)
00170             Wheel.reset();
00171         Month = (uint8_t)(Wheel.getPulses()/2);
00172         
00173         if(Month>11) 
00174             Wheel.reset();
00175             
00176         sprintf(Text, "Month: %2d", Month+1);
00177         LCD_drawstring(buffer, 0, 3, Text);
00178    
00179     }
00180 
00181 
00182     // Set day
00183     while(Button);
00184     wait_ms(50);  
00185     Wheel.reset();
00186     while(!Button)
00187     { 
00188         if(int(Wheel.getPulses())<0)
00189             Wheel.reset();
00190         Day = (uint8_t)(Wheel.getPulses()/2);
00191         
00192         if(Day>30) 
00193             Wheel.reset();
00194             
00195         sprintf(Text, "Day: %2d", Day+1);
00196         LCD_drawstring(buffer, 0, 4, Text);
00197    
00198     }
00199 
00200     // Set hours
00201     while(Button);
00202     wait_ms(50);  
00203     Wheel.reset();
00204     while(!Button)
00205     { 
00206         if(int(Wheel.getPulses())<0)
00207             Wheel.reset();
00208         Hours = (uint8_t)(Wheel.getPulses()/2);
00209         
00210         if(Hours>22) 
00211             Wheel.reset();
00212             
00213         sprintf(Text, "Hours: %2d", Hours+1);
00214         LCD_drawstring(buffer, 0, 5, Text);
00215    
00216     }
00217     Hours++;
00218 
00219     // Set minutes
00220     while(Button);
00221     wait_ms(50);  
00222     Wheel.reset();
00223     while(!Button)
00224     { 
00225         if(int(Wheel.getPulses())<0)
00226             Wheel.reset();
00227         Mins = (uint8_t)(Wheel.getPulses()/2);
00228         
00229         if(Mins>59) 
00230             Wheel.reset();
00231             
00232         sprintf(Text, "Minutes: %2d", Mins);
00233         LCD_drawstring(buffer, 0, 6, Text);
00234    
00235     }
00236 
00237     t.tm_year = Year + 100;
00238     t.tm_mon  = Month;
00239     t.tm_mday = Day + 1;
00240     t.tm_hour = Hours;
00241     t.tm_min  = Mins;
00242     t.tm_sec  = Secs;
00243 
00244     seconds = mktime(&t);
00245     set_time(seconds);
00246     
00247     return;
00248 }    
00249 
00250 
00251 //---------------------------------------------------------------------------
00252 void Read_Voltages(void)
00253 {
00254 
00255     double ADC_value;
00256     uint8_t smooth = 10; // Number of samples to smooth
00257     uint8_t i;
00258     
00259     // Read battery voltage
00260     
00261     ADC_value = BATT.read();    // cleanup
00262     wait_ms(50);
00263     ADC_value = 0;    
00264     for(i=0;i<smooth;i++) 
00265         ADC_value += BATT.read();       
00266 
00267     ADC_value = ADC_value/smooth;
00268     Vbatt = (float)(ADC_value*BAT_GAIN)+BAT_OFFS;
00269     
00270     
00271     // Read Ambient Light Level
00272     
00273     ADC_value = ALS.read();    // cleanup
00274     wait_ms(50);
00275     ADC_value = 0;    
00276     for(i=0;i<smooth;i++) 
00277         ADC_value += ALS.read();       
00278 
00279     ADC_value = ADC_value/smooth;
00280     ALLevel = (float)(ADC_value);      
00281    
00282 return;
00283 }
00284 
00285 
00286 //--------------------------------------------------------------------------- 
00287 void PowerOff(void) 
00288 {
00289     BKL.write(1);
00290     Clear_buffer(buffer);
00291     sprintf(Text,"POWERING OFF");
00292     LCD_drawstring(buffer, 20, 3, Text); 
00293     LCD_write_buffer(buffer);
00294     wait(2);
00295     
00296     KAL = 0;
00297 }