CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Sun Nov 04 18:44:51 2018 -0700
Revision:
0:aa13e1c335cd
Child:
1:622a6f7ed3e6
create new repo for show2me

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Charles David Young 0:aa13e1c335cd 1 #include "mbed.h"
Charles David Young 0:aa13e1c335cd 2 #include "SDFileSystem.h"
Charles David Young 0:aa13e1c335cd 3 #include "ST7565_LCD.h"
Charles David Young 0:aa13e1c335cd 4 #include "QEI.h"
Charles David Young 0:aa13e1c335cd 5
Charles David Young 0:aa13e1c335cd 6 #define BAT_GAIN 6.80
Charles David Young 0:aa13e1c335cd 7 #define BAT_OFFS 0.0
Charles David Young 0:aa13e1c335cd 8
Charles David Young 0:aa13e1c335cd 9 PwmOut BEEP (D2); // Buzzer/speaker (PWM output)
Charles David Young 0:aa13e1c335cd 10 PwmOut BKL (D3); // LCD backlight control (PMW output)
Charles David Young 0:aa13e1c335cd 11 DigitalOut KAL (D0); // Keep-Alive/turn-off
Charles David Young 0:aa13e1c335cd 12 DigitalOut BTC (D2); // Aux BT module control
Charles David Young 0:aa13e1c335cd 13 DigitalIn Button (D4); // Pushbutton (digital input)
Charles David Young 0:aa13e1c335cd 14 AnalogIn BATT (A0); // Battery monitor
Charles David Young 0:aa13e1c335cd 15 AnalogIn ALS (A1); // Ambient Light sensor
Charles David Young 0:aa13e1c335cd 16
Charles David Young 0:aa13e1c335cd 17 SDFileSystem sd(PB_15, PB_14, PB_13, PB_1, "sd"); // MOSI, MISO, SCK, CS
Charles David Young 0:aa13e1c335cd 18
Charles David Young 0:aa13e1c335cd 19 // Quadrature encoder
Charles David Young 0:aa13e1c335cd 20 QEI Wheel(D5, D6, NC, 16);
Charles David Young 0:aa13e1c335cd 21
Charles David Young 0:aa13e1c335cd 22 // Tickers
Charles David Young 0:aa13e1c335cd 23 Ticker Sec_Beat; // Timer ticker
Charles David Young 0:aa13e1c335cd 24 Ticker Display_Refresh; // Display refresh ticker
Charles David Young 0:aa13e1c335cd 25
Charles David Young 0:aa13e1c335cd 26 //Serial ports
Charles David Young 0:aa13e1c335cd 27 Serial PC(USBTX, USBRX); // Virtual COM via USB
Charles David Young 0:aa13e1c335cd 28 //Serial BT_module(D1, D0); // BlueTooth module
Charles David Young 0:aa13e1c335cd 29
Charles David Young 0:aa13e1c335cd 30
Charles David Young 0:aa13e1c335cd 31 extern unsigned int buffer[128*64/8]; // RAM buffer used by LCD
Charles David Young 0:aa13e1c335cd 32 time_t seconds; // timestamp
Charles David Young 0:aa13e1c335cd 33 char Text[40]=""; // Text string used by LCD
Charles David Young 0:aa13e1c335cd 34 float Vbatt, ALLevel; // battery voltage and ambient light level
Charles David Young 0:aa13e1c335cd 35
Charles David Young 0:aa13e1c335cd 36 // ------------------- Prototypes -----------------------
Charles David Young 0:aa13e1c335cd 37 void Timer_tick(void);
Charles David Young 0:aa13e1c335cd 38 void Update_Display(void);
Charles David Young 0:aa13e1c335cd 39 void Set_Time(void);
Charles David Young 0:aa13e1c335cd 40 void Read_Voltages(void);
Charles David Young 0:aa13e1c335cd 41 void PowerOff(void);
Charles David Young 0:aa13e1c335cd 42
Charles David Young 0:aa13e1c335cd 43 int main()
Charles David Young 0:aa13e1c335cd 44 {
Charles David Young 0:aa13e1c335cd 45
Charles David Young 0:aa13e1c335cd 46 KAL = 1; // ensure self-sustained power
Charles David Young 0:aa13e1c335cd 47 //Button.mode(PullUp); // enable pushbutton pull-up
Charles David Young 0:aa13e1c335cd 48 BKL.period_ms(3); // set LCD backlight PWM
Charles David Young 0:aa13e1c335cd 49 BKL.write(1.0);
Charles David Young 0:aa13e1c335cd 50 BEEP.period_us(2300); // set initial buzzer period and duty-cycle
Charles David Young 0:aa13e1c335cd 51 BEEP.write(0.2);
Charles David Young 0:aa13e1c335cd 52 Wheel.reset(); // clear encoder
Charles David Young 0:aa13e1c335cd 53 LCD_reset();
Charles David Young 0:aa13e1c335cd 54
Charles David Young 0:aa13e1c335cd 55 // splash screen with date and time
Charles David Young 0:aa13e1c335cd 56 sprintf(Text,__DATE__);
Charles David Young 0:aa13e1c335cd 57 LCD_drawstring(buffer, 60, 5, Text);
Charles David Young 0:aa13e1c335cd 58 sprintf(Text,__TIME__);
Charles David Young 0:aa13e1c335cd 59 LCD_drawstring(buffer, 78, 6, Text);
Charles David Young 0:aa13e1c335cd 60 LCD_write_buffer(buffer);
Charles David Young 0:aa13e1c335cd 61
Charles David Young 0:aa13e1c335cd 62
Charles David Young 0:aa13e1c335cd 63 // enable LCD refresh ticker
Charles David Young 0:aa13e1c335cd 64 Display_Refresh.attach(&Update_Display, 0.3);
Charles David Young 0:aa13e1c335cd 65
Charles David Young 0:aa13e1c335cd 66 if(Button) // if pushbutton is pressed
Charles David Young 0:aa13e1c335cd 67 Set_Time(); // set RTC time and date
Charles David Young 0:aa13e1c335cd 68
Charles David Young 0:aa13e1c335cd 69 wait(2);
Charles David Young 0:aa13e1c335cd 70 Clear_buffer(buffer);
Charles David Young 0:aa13e1c335cd 71 BKL.write(0.5);
Charles David Young 0:aa13e1c335cd 72
Charles David Young 0:aa13e1c335cd 73 //PowerOff(); // Power-off test
Charles David Young 0:aa13e1c335cd 74
Charles David Young 0:aa13e1c335cd 75
Charles David Young 0:aa13e1c335cd 76 // enable sec-beat ticker
Charles David Young 0:aa13e1c335cd 77 Sec_Beat.attach(&Timer_tick, 1);
Charles David Young 0:aa13e1c335cd 78
Charles David Young 0:aa13e1c335cd 79
Charles David Young 0:aa13e1c335cd 80
Charles David Young 0:aa13e1c335cd 81 printf("Hello World!\n");
Charles David Young 0:aa13e1c335cd 82 mkdir("/sd/system", 0777);
Charles David Young 0:aa13e1c335cd 83 FILE *fp = fopen("/sd/system/sdlog.txt", "w");
Charles David Young 0:aa13e1c335cd 84 if(fp == NULL) {
Charles David Young 0:aa13e1c335cd 85 error("Could not open file for write\n");
Charles David Young 0:aa13e1c335cd 86 }
Charles David Young 0:aa13e1c335cd 87 fprintf(fp, "Hello fun SD Card World!");
Charles David Young 0:aa13e1c335cd 88 fclose(fp);
Charles David Young 0:aa13e1c335cd 89 printf("Goodbye World!\n");
Charles David Young 0:aa13e1c335cd 90
Charles David Young 0:aa13e1c335cd 91 while(1)
Charles David Young 0:aa13e1c335cd 92 {
Charles David Young 0:aa13e1c335cd 93 // dance
Charles David Young 0:aa13e1c335cd 94 }
Charles David Young 0:aa13e1c335cd 95
Charles David Young 0:aa13e1c335cd 96
Charles David Young 0:aa13e1c335cd 97 }
Charles David Young 0:aa13e1c335cd 98
Charles David Young 0:aa13e1c335cd 99
Charles David Young 0:aa13e1c335cd 100 //===========================================================================
Charles David Young 0:aa13e1c335cd 101
Charles David Young 0:aa13e1c335cd 102 // ------------- Called every second ----------------------
Charles David Young 0:aa13e1c335cd 103
Charles David Young 0:aa13e1c335cd 104 void Timer_tick()
Charles David Young 0:aa13e1c335cd 105 {
Charles David Young 0:aa13e1c335cd 106 seconds = time(NULL);
Charles David Young 0:aa13e1c335cd 107 strftime(Text, 50, "%d-%b-%Y %H:%M:%S", localtime(&seconds));
Charles David Young 0:aa13e1c335cd 108 LCD_drawstring(buffer, 0, 0, Text);
Charles David Young 0:aa13e1c335cd 109
Charles David Young 0:aa13e1c335cd 110 // read voltages
Charles David Young 0:aa13e1c335cd 111 Read_Voltages();
Charles David Young 0:aa13e1c335cd 112
Charles David Young 0:aa13e1c335cd 113 // write values to buffer
Charles David Young 0:aa13e1c335cd 114 sprintf(Text,"VBATT= %4.2f", Vbatt);
Charles David Young 0:aa13e1c335cd 115 LCD_drawstring(buffer, 0, 2, Text);
Charles David Young 0:aa13e1c335cd 116 sprintf(Text,"ALL= %4.2f", ALLevel);
Charles David Young 0:aa13e1c335cd 117 LCD_drawstring(buffer, 0, 3, Text);
Charles David Young 0:aa13e1c335cd 118
Charles David Young 0:aa13e1c335cd 119 // Send data to COM port
Charles David Young 0:aa13e1c335cd 120 //PC.printf("Tset= %3.0f, Tpit= %3.0f, Tmeat= %3.0f, PWM= %3.0f\n",Tset, Tpit, Tmeat, FanSpeed);
Charles David Young 0:aa13e1c335cd 121
Charles David Young 0:aa13e1c335cd 122 //LCD_write_buffer(buffer); // LCD update
Charles David Young 0:aa13e1c335cd 123
Charles David Young 0:aa13e1c335cd 124 return;
Charles David Young 0:aa13e1c335cd 125 }
Charles David Young 0:aa13e1c335cd 126
Charles David Young 0:aa13e1c335cd 127
Charles David Young 0:aa13e1c335cd 128 //---------------------------------------------------------------------------
Charles David Young 0:aa13e1c335cd 129 void Update_Display(void)
Charles David Young 0:aa13e1c335cd 130 {
Charles David Young 0:aa13e1c335cd 131 LCD_write_buffer(buffer); // LCD update
Charles David Young 0:aa13e1c335cd 132 return;
Charles David Young 0:aa13e1c335cd 133 }
Charles David Young 0:aa13e1c335cd 134
Charles David Young 0:aa13e1c335cd 135 //---------------------------------------------------------------------------
Charles David Young 0:aa13e1c335cd 136 void Set_Time(void)
Charles David Young 0:aa13e1c335cd 137 {
Charles David Young 0:aa13e1c335cd 138 uint8_t Year=0, Month=0, Day=0, Hours=0, Mins=0, Secs=0;
Charles David Young 0:aa13e1c335cd 139 time_t seconds;
Charles David Young 0:aa13e1c335cd 140 struct tm t;
Charles David Young 0:aa13e1c335cd 141
Charles David Young 0:aa13e1c335cd 142 sprintf(Text,"TIME & DATE SETTING");
Charles David Young 0:aa13e1c335cd 143 LCD_drawstring(buffer, 0, 0, Text);
Charles David Young 0:aa13e1c335cd 144
Charles David Young 0:aa13e1c335cd 145 // Set year
Charles David Young 0:aa13e1c335cd 146 while(Button);
Charles David Young 0:aa13e1c335cd 147 wait_ms(50);
Charles David Young 0:aa13e1c335cd 148
Charles David Young 0:aa13e1c335cd 149 while(!Button)
Charles David Young 0:aa13e1c335cd 150 {
Charles David Young 0:aa13e1c335cd 151 if(int(Wheel.getPulses())<0)
Charles David Young 0:aa13e1c335cd 152 Wheel.reset();
Charles David Young 0:aa13e1c335cd 153 Year = (uint8_t)(Wheel.getPulses());
Charles David Young 0:aa13e1c335cd 154
Charles David Young 0:aa13e1c335cd 155 if(Year>99)
Charles David Young 0:aa13e1c335cd 156 Wheel.reset();
Charles David Young 0:aa13e1c335cd 157
Charles David Young 0:aa13e1c335cd 158 sprintf(Text, "Year: %2d", Year);
Charles David Young 0:aa13e1c335cd 159 LCD_drawstring(buffer, 0, 2, Text);
Charles David Young 0:aa13e1c335cd 160
Charles David Young 0:aa13e1c335cd 161 }
Charles David Young 0:aa13e1c335cd 162
Charles David Young 0:aa13e1c335cd 163 // Set month
Charles David Young 0:aa13e1c335cd 164 while(Button);
Charles David Young 0:aa13e1c335cd 165 wait_ms(50);
Charles David Young 0:aa13e1c335cd 166 Wheel.reset();
Charles David Young 0:aa13e1c335cd 167 while(!Button)
Charles David Young 0:aa13e1c335cd 168 {
Charles David Young 0:aa13e1c335cd 169 if(int(Wheel.getPulses())<0)
Charles David Young 0:aa13e1c335cd 170 Wheel.reset();
Charles David Young 0:aa13e1c335cd 171 Month = (uint8_t)(Wheel.getPulses()/2);
Charles David Young 0:aa13e1c335cd 172
Charles David Young 0:aa13e1c335cd 173 if(Month>11)
Charles David Young 0:aa13e1c335cd 174 Wheel.reset();
Charles David Young 0:aa13e1c335cd 175
Charles David Young 0:aa13e1c335cd 176 sprintf(Text, "Month: %2d", Month+1);
Charles David Young 0:aa13e1c335cd 177 LCD_drawstring(buffer, 0, 3, Text);
Charles David Young 0:aa13e1c335cd 178
Charles David Young 0:aa13e1c335cd 179 }
Charles David Young 0:aa13e1c335cd 180
Charles David Young 0:aa13e1c335cd 181
Charles David Young 0:aa13e1c335cd 182 // Set day
Charles David Young 0:aa13e1c335cd 183 while(Button);
Charles David Young 0:aa13e1c335cd 184 wait_ms(50);
Charles David Young 0:aa13e1c335cd 185 Wheel.reset();
Charles David Young 0:aa13e1c335cd 186 while(!Button)
Charles David Young 0:aa13e1c335cd 187 {
Charles David Young 0:aa13e1c335cd 188 if(int(Wheel.getPulses())<0)
Charles David Young 0:aa13e1c335cd 189 Wheel.reset();
Charles David Young 0:aa13e1c335cd 190 Day = (uint8_t)(Wheel.getPulses()/2);
Charles David Young 0:aa13e1c335cd 191
Charles David Young 0:aa13e1c335cd 192 if(Day>30)
Charles David Young 0:aa13e1c335cd 193 Wheel.reset();
Charles David Young 0:aa13e1c335cd 194
Charles David Young 0:aa13e1c335cd 195 sprintf(Text, "Day: %2d", Day+1);
Charles David Young 0:aa13e1c335cd 196 LCD_drawstring(buffer, 0, 4, Text);
Charles David Young 0:aa13e1c335cd 197
Charles David Young 0:aa13e1c335cd 198 }
Charles David Young 0:aa13e1c335cd 199
Charles David Young 0:aa13e1c335cd 200 // Set hours
Charles David Young 0:aa13e1c335cd 201 while(Button);
Charles David Young 0:aa13e1c335cd 202 wait_ms(50);
Charles David Young 0:aa13e1c335cd 203 Wheel.reset();
Charles David Young 0:aa13e1c335cd 204 while(!Button)
Charles David Young 0:aa13e1c335cd 205 {
Charles David Young 0:aa13e1c335cd 206 if(int(Wheel.getPulses())<0)
Charles David Young 0:aa13e1c335cd 207 Wheel.reset();
Charles David Young 0:aa13e1c335cd 208 Hours = (uint8_t)(Wheel.getPulses()/2);
Charles David Young 0:aa13e1c335cd 209
Charles David Young 0:aa13e1c335cd 210 if(Hours>22)
Charles David Young 0:aa13e1c335cd 211 Wheel.reset();
Charles David Young 0:aa13e1c335cd 212
Charles David Young 0:aa13e1c335cd 213 sprintf(Text, "Hours: %2d", Hours+1);
Charles David Young 0:aa13e1c335cd 214 LCD_drawstring(buffer, 0, 5, Text);
Charles David Young 0:aa13e1c335cd 215
Charles David Young 0:aa13e1c335cd 216 }
Charles David Young 0:aa13e1c335cd 217 Hours++;
Charles David Young 0:aa13e1c335cd 218
Charles David Young 0:aa13e1c335cd 219 // Set minutes
Charles David Young 0:aa13e1c335cd 220 while(Button);
Charles David Young 0:aa13e1c335cd 221 wait_ms(50);
Charles David Young 0:aa13e1c335cd 222 Wheel.reset();
Charles David Young 0:aa13e1c335cd 223 while(!Button)
Charles David Young 0:aa13e1c335cd 224 {
Charles David Young 0:aa13e1c335cd 225 if(int(Wheel.getPulses())<0)
Charles David Young 0:aa13e1c335cd 226 Wheel.reset();
Charles David Young 0:aa13e1c335cd 227 Mins = (uint8_t)(Wheel.getPulses()/2);
Charles David Young 0:aa13e1c335cd 228
Charles David Young 0:aa13e1c335cd 229 if(Mins>59)
Charles David Young 0:aa13e1c335cd 230 Wheel.reset();
Charles David Young 0:aa13e1c335cd 231
Charles David Young 0:aa13e1c335cd 232 sprintf(Text, "Minutes: %2d", Mins);
Charles David Young 0:aa13e1c335cd 233 LCD_drawstring(buffer, 0, 6, Text);
Charles David Young 0:aa13e1c335cd 234
Charles David Young 0:aa13e1c335cd 235 }
Charles David Young 0:aa13e1c335cd 236
Charles David Young 0:aa13e1c335cd 237 t.tm_year = Year + 100;
Charles David Young 0:aa13e1c335cd 238 t.tm_mon = Month;
Charles David Young 0:aa13e1c335cd 239 t.tm_mday = Day + 1;
Charles David Young 0:aa13e1c335cd 240 t.tm_hour = Hours;
Charles David Young 0:aa13e1c335cd 241 t.tm_min = Mins;
Charles David Young 0:aa13e1c335cd 242 t.tm_sec = Secs;
Charles David Young 0:aa13e1c335cd 243
Charles David Young 0:aa13e1c335cd 244 seconds = mktime(&t);
Charles David Young 0:aa13e1c335cd 245 set_time(seconds);
Charles David Young 0:aa13e1c335cd 246
Charles David Young 0:aa13e1c335cd 247 return;
Charles David Young 0:aa13e1c335cd 248 }
Charles David Young 0:aa13e1c335cd 249
Charles David Young 0:aa13e1c335cd 250
Charles David Young 0:aa13e1c335cd 251 //---------------------------------------------------------------------------
Charles David Young 0:aa13e1c335cd 252 void Read_Voltages(void)
Charles David Young 0:aa13e1c335cd 253 {
Charles David Young 0:aa13e1c335cd 254
Charles David Young 0:aa13e1c335cd 255 double ADC_value;
Charles David Young 0:aa13e1c335cd 256 uint8_t smooth = 10; // Number of samples to smooth
Charles David Young 0:aa13e1c335cd 257 uint8_t i;
Charles David Young 0:aa13e1c335cd 258
Charles David Young 0:aa13e1c335cd 259 // Read battery voltage
Charles David Young 0:aa13e1c335cd 260
Charles David Young 0:aa13e1c335cd 261 ADC_value = BATT.read(); // cleanup
Charles David Young 0:aa13e1c335cd 262 wait_ms(50);
Charles David Young 0:aa13e1c335cd 263 ADC_value = 0;
Charles David Young 0:aa13e1c335cd 264 for(i=0;i<smooth;i++)
Charles David Young 0:aa13e1c335cd 265 ADC_value += BATT.read();
Charles David Young 0:aa13e1c335cd 266
Charles David Young 0:aa13e1c335cd 267 ADC_value = ADC_value/smooth;
Charles David Young 0:aa13e1c335cd 268 Vbatt = (float)(ADC_value*BAT_GAIN)+BAT_OFFS;
Charles David Young 0:aa13e1c335cd 269
Charles David Young 0:aa13e1c335cd 270
Charles David Young 0:aa13e1c335cd 271 // Read Ambient Light Level
Charles David Young 0:aa13e1c335cd 272
Charles David Young 0:aa13e1c335cd 273 ADC_value = ALS.read(); // cleanup
Charles David Young 0:aa13e1c335cd 274 wait_ms(50);
Charles David Young 0:aa13e1c335cd 275 ADC_value = 0;
Charles David Young 0:aa13e1c335cd 276 for(i=0;i<smooth;i++)
Charles David Young 0:aa13e1c335cd 277 ADC_value += ALS.read();
Charles David Young 0:aa13e1c335cd 278
Charles David Young 0:aa13e1c335cd 279 ADC_value = ADC_value/smooth;
Charles David Young 0:aa13e1c335cd 280 ALLevel = (float)(ADC_value);
Charles David Young 0:aa13e1c335cd 281
Charles David Young 0:aa13e1c335cd 282 return;
Charles David Young 0:aa13e1c335cd 283 }
Charles David Young 0:aa13e1c335cd 284
Charles David Young 0:aa13e1c335cd 285
Charles David Young 0:aa13e1c335cd 286 //---------------------------------------------------------------------------
Charles David Young 0:aa13e1c335cd 287 void PowerOff(void)
Charles David Young 0:aa13e1c335cd 288 {
Charles David Young 0:aa13e1c335cd 289 BKL.write(1);
Charles David Young 0:aa13e1c335cd 290 Clear_buffer(buffer);
Charles David Young 0:aa13e1c335cd 291 sprintf(Text,"POWERING OFF");
Charles David Young 0:aa13e1c335cd 292 LCD_drawstring(buffer, 20, 3, Text);
Charles David Young 0:aa13e1c335cd 293 LCD_write_buffer(buffer);
Charles David Young 0:aa13e1c335cd 294 wait(2);
Charles David Young 0:aa13e1c335cd 295
Charles David Young 0:aa13e1c335cd 296 KAL = 0;
Charles David Young 0:aa13e1c335cd 297 }