PES 4 - Smart Medication Dispenser / PES4_ProgrammeforDesignReview2

Dependencies:   SDFileSystem mbed

Fork of PES4_Programme by PES 4 - Smart Medication Dispenser

Committer:
aeschsim
Date:
Fri Apr 06 06:47:53 2018 +0000
Revision:
70:cc7ed2325202
Parent:
69:ec1cfc0cd161
Child:
71:3d0a4dbff536
clean up

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cittecla 3:94026b29e994 1 #include "main.h"
aeschsim 59:1867088695f3 2 #define FADESPEED 5
aeschsim 59:1867088695f3 3 #define DEMOTIME 5
cittecla 2:72886e996098 4
cittecla 52:701d0c2f47d7 5 int state = 44;
itslinear 17:bbafd216e059 6
cittecla 41:f054a83f9556 7 int oldState;
cittecla 2:72886e996098 8
cittecla 2:72886e996098 9 Serial pc(USBTX, USBRX); // tx, rx
aeschsim 40:b5e533e1e033 10
aeschsim 60:7554e4272b3b 11 DigitalIn lichtschranke(PC_5);
aeschsim 60:7554e4272b3b 12 InterruptIn userButton1(PB_5);
aeschsim 60:7554e4272b3b 13 InterruptIn userButton2(PB_4);
aeschsim 60:7554e4272b3b 14 DigitalIn glasDetection(PA_10);
aeschsim 60:7554e4272b3b 15 PwmOut summer(PC_8);
itslinear 63:be13158bb4dd 16
itslinear 63:be13158bb4dd 17 // Circular buffers for serial TX and RX data - used by interrupt routines
itslinear 63:be13158bb4dd 18 const int buffer_size = 255;
itslinear 63:be13158bb4dd 19 // might need to increase buffer size for high baud rates
itslinear 63:be13158bb4dd 20 char tx_buffer[buffer_size+1];
itslinear 63:be13158bb4dd 21 char rx_buffer[buffer_size+1];
itslinear 63:be13158bb4dd 22 // Circular buffer pointers
itslinear 63:be13158bb4dd 23 // volatile makes read-modify-write atomic
itslinear 63:be13158bb4dd 24 volatile int tx_in=0;
itslinear 63:be13158bb4dd 25 volatile int tx_out=0;
itslinear 63:be13158bb4dd 26 volatile int rx_in=0;
itslinear 63:be13158bb4dd 27 volatile int rx_out=0;
itslinear 63:be13158bb4dd 28 // Line buffers for sprintf and sscanf
itslinear 63:be13158bb4dd 29 char tx_line[80];
itslinear 63:be13158bb4dd 30 char rx_line[80];
itslinear 63:be13158bb4dd 31
itslinear 63:be13158bb4dd 32 int i=0;
itslinear 63:be13158bb4dd 33 int rx_i=0;
aeschsim 70:cc7ed2325202 34
itslinear 63:be13158bb4dd 35 // Setup a serial interrupt function to receive data
itslinear 63:be13158bb4dd 36 pc.attach(&Rx_interrupt, Serial::RxIrq);
itslinear 63:be13158bb4dd 37 // Setup a serial interrupt function to transmit data
itslinear 63:be13158bb4dd 38 pc.attach(&Tx_interrupt, Serial::TxIrq);
aeschsim 70:cc7ed2325202 39
itslinear 63:be13158bb4dd 40
itslinear 63:be13158bb4dd 41
itslinear 63:be13158bb4dd 42
aeschsim 40:b5e533e1e033 43 /* PWM Test Tool */
aeschsim 40:b5e533e1e033 44 int channel;
aeschsim 40:b5e533e1e033 45 int dutyCycle;
cittecla 55:bdab541f434d 46 int day;
cittecla 55:bdab541f434d 47 int moment;
cittecla 55:bdab541f434d 48 s_user user[2];
cittecla 2:72886e996098 49
aeschsim 59:1867088695f3 50 /* Demo Tool */
aeschsim 59:1867088695f3 51 uint32_t color = 0;
aeschsim 59:1867088695f3 52 int colorTime = 0;
aeschsim 59:1867088695f3 53 int r,b,g;
aeschsim 59:1867088695f3 54
aeschsim 60:7554e4272b3b 55 void summ(void)
aeschsim 60:7554e4272b3b 56 {
aeschsim 60:7554e4272b3b 57 if(summer.read()>0) {
aeschsim 60:7554e4272b3b 58 summer.write(0.0f);
aeschsim 60:7554e4272b3b 59 } else {
aeschsim 60:7554e4272b3b 60 summer.write(0.5f);
aeschsim 60:7554e4272b3b 61 }
aeschsim 60:7554e4272b3b 62 }
aeschsim 60:7554e4272b3b 63
cittecla 2:72886e996098 64
cittecla 2:72886e996098 65 int main()
cittecla 2:72886e996098 66 {
cittecla 2:72886e996098 67 pc.baud(460800);
cittecla 2:72886e996098 68 printf("start...\r\n");
cittecla 2:72886e996098 69
aeschsim 34:9d6dd3a12641 70 while(1) {
cittecla 41:f054a83f9556 71 if(state != oldState) {
cittecla 58:cda5298c9b7f 72 printf("\r\nstate: %d\r\n",state);
cittecla 41:f054a83f9556 73 oldState = state;
aeschsim 34:9d6dd3a12641 74 }
aeschsim 34:9d6dd3a12641 75 switch (state) {
aeschsim 34:9d6dd3a12641 76 case 0:
aeschsim 34:9d6dd3a12641 77 printf("Hello World");
aeschsim 40:b5e533e1e033 78 state = 47;
aeschsim 34:9d6dd3a12641 79 break;
aeschsim 34:9d6dd3a12641 80 case 1:
aeschsim 34:9d6dd3a12641 81 break;
aeschsim 34:9d6dd3a12641 82 case 2:
aeschsim 34:9d6dd3a12641 83 break;
aeschsim 34:9d6dd3a12641 84 case 3:
aeschsim 34:9d6dd3a12641 85 break;
aeschsim 70:cc7ed2325202 86
aeschsim 34:9d6dd3a12641 87 case 10:
itslinear 63:be13158bb4dd 88 // Formatted IO test using send and receive serial interrupts
itslinear 63:be13158bb4dd 89 // with sprintf and sscanf
itslinear 63:be13158bb4dd 90 while (1) {
itslinear 63:be13158bb4dd 91 // Loop to generate different test values - send value in hex, decimal, and octal and then read back
itslinear 63:be13158bb4dd 92 for (i=0; i<0xFFFF; i++) {
itslinear 63:be13158bb4dd 93 // Print ASCII number to tx line buffer in hex
itslinear 63:be13158bb4dd 94 sprintf(tx_line,"%x\r\n",i);
itslinear 63:be13158bb4dd 95 // Copy tx line buffer to large tx buffer for tx interrupt routine
itslinear 63:be13158bb4dd 96 send_line();
itslinear 63:be13158bb4dd 97 // Print ASCII number to tx line buffer in decimal
itslinear 63:be13158bb4dd 98 sprintf(tx_line,"%d\r\n",i);
itslinear 63:be13158bb4dd 99 // Copy tx line buffer to large tx buffer for tx interrupt routine
itslinear 63:be13158bb4dd 100 send_line();
itslinear 63:be13158bb4dd 101 // Print ASCII number to tx line buffer in octal
itslinear 63:be13158bb4dd 102 sprintf(tx_line,"%o\r\n",i);
itslinear 63:be13158bb4dd 103 // Copy tx line buffer to large tx buffer for tx interrupt routine
itslinear 63:be13158bb4dd 104 send_line();
itslinear 63:be13158bb4dd 105
itslinear 63:be13158bb4dd 106 // Read a line from the large rx buffer from rx interrupt routine
itslinear 63:be13158bb4dd 107 read_line();
itslinear 63:be13158bb4dd 108 // Read ASCII number from rx line buffer
itslinear 63:be13158bb4dd 109 sscanf(rx_line,"%x",&rx_i);
itslinear 63:be13158bb4dd 110 // Read a line from the large rx buffer from rx interrupt routine
itslinear 63:be13158bb4dd 111 read_line();
itslinear 63:be13158bb4dd 112 // Read ASCII number from rx line buffer
itslinear 63:be13158bb4dd 113 sscanf(rx_line,"%d",&rx_i);
itslinear 63:be13158bb4dd 114 // Read a line from the large rx buffer from rx interrupt routine
itslinear 63:be13158bb4dd 115 read_line();
itslinear 63:be13158bb4dd 116 // Read ASCII number from rx line buffer
itslinear 63:be13158bb4dd 117 sscanf(rx_line,"%o",&rx_i);
itslinear 63:be13158bb4dd 118 }
itslinear 63:be13158bb4dd 119 }
aeschsim 34:9d6dd3a12641 120 break;
aeschsim 70:cc7ed2325202 121
aeschsim 70:cc7ed2325202 122
aeschsim 34:9d6dd3a12641 123 case 11:
aeschsim 34:9d6dd3a12641 124 break;
cittecla 2:72886e996098 125
aeschsim 34:9d6dd3a12641 126 case 15:
aeschsim 34:9d6dd3a12641 127 break;
aeschsim 34:9d6dd3a12641 128 case 16:
aeschsim 34:9d6dd3a12641 129 break;
aeschsim 34:9d6dd3a12641 130 case 17:
aeschsim 34:9d6dd3a12641 131 break;
aeschsim 34:9d6dd3a12641 132
aeschsim 34:9d6dd3a12641 133 case 25:
aeschsim 34:9d6dd3a12641 134 break;
aeschsim 34:9d6dd3a12641 135 case 26:
aeschsim 34:9d6dd3a12641 136 break;
aeschsim 34:9d6dd3a12641 137 case 27:
aeschsim 34:9d6dd3a12641 138 break;
aeschsim 34:9d6dd3a12641 139 case 28:
aeschsim 34:9d6dd3a12641 140 break;
cittecla 2:72886e996098 141
cittecla 2:72886e996098 142
aeschsim 34:9d6dd3a12641 143 case 35:
aeschsim 34:9d6dd3a12641 144 break;
aeschsim 34:9d6dd3a12641 145 case 36:
aeschsim 34:9d6dd3a12641 146 break;
aeschsim 34:9d6dd3a12641 147 case 37:
aeschsim 34:9d6dd3a12641 148 break;
aeschsim 34:9d6dd3a12641 149 case 38:
aeschsim 34:9d6dd3a12641 150 break;
aeschsim 27:bf0577ecfa6e 151
aeschsim 40:b5e533e1e033 152 /**************************************************************/
aeschsim 40:b5e533e1e033 153 /* Test Zone */
aeschsim 40:b5e533e1e033 154
cittecla 52:701d0c2f47d7 155 /* init PWM board */
cittecla 52:701d0c2f47d7 156 case 44:
cittecla 55:bdab541f434d 157 printf("Init PWM\r\n");
cittecla 53:1c61cadbcb35 158 initPWM(50);
cittecla 53:1c61cadbcb35 159 enableOutput();
cittecla 52:701d0c2f47d7 160 state = 47;
cittecla 52:701d0c2f47d7 161 break;
aeschsim 40:b5e533e1e033 162
aeschsim 40:b5e533e1e033 163 /* Time test tool: set Time */
aeschsim 34:9d6dd3a12641 164 case 45:
aeschsim 34:9d6dd3a12641 165 s_time time;
aeschsim 34:9d6dd3a12641 166 time.second = 30;
aeschsim 34:9d6dd3a12641 167 time.minute = 53;
aeschsim 34:9d6dd3a12641 168 time.hour = 12;
aeschsim 34:9d6dd3a12641 169 time.day = 27;
aeschsim 34:9d6dd3a12641 170 time.weekday = 2;
aeschsim 34:9d6dd3a12641 171 time.month = 3;
aeschsim 34:9d6dd3a12641 172 time.year = 18;
cittecla 55:bdab541f434d 173 printf("set now time\r\n");
aeschsim 34:9d6dd3a12641 174 setTime(&time);
cittecla 55:bdab541f434d 175 printf("read time: %02d:%02d:%02d, %02d.%02d.20%02d, weekday: %d\r\n",time.hour, time.minute, time.second, time.day, time.month, time.year, time.weekday);
aeschsim 40:b5e533e1e033 176 state = 47;
cittecla 55:bdab541f434d 177 printf("change state to: %d\r\n",state);
aeschsim 34:9d6dd3a12641 178 break;
aeschsim 40:b5e533e1e033 179
aeschsim 40:b5e533e1e033 180 /* Time test tool: get Time */
aeschsim 34:9d6dd3a12641 181 case 46:
aeschsim 34:9d6dd3a12641 182 wait(5);
aeschsim 34:9d6dd3a12641 183 time = getTime();
cittecla 55:bdab541f434d 184 printf("read time: %02d:%02d:%02d, %02d.%02d.20%02d, weekday: %d\r\n",time.hour, time.minute, time.second, time.day, time.month, time.year, time.weekday);
aeschsim 40:b5e533e1e033 185 state = 47;
aeschsim 40:b5e533e1e033 186 break;
aeschsim 40:b5e533e1e033 187
aeschsim 40:b5e533e1e033 188 /* Console State Switch */
aeschsim 40:b5e533e1e033 189 case 47:
cittecla 55:bdab541f434d 190 printf("\n\rState Switch: Enter next State: \n\r");
cittecla 55:bdab541f434d 191 printf("45\tset Time\n\r46\tread Time\n\r47\tState Switch\n\r48\tServo Board Test Tool\n\r49\tenable servo output\n\r50\tdisable servo output\n\r51\ttest SD Card\n\r52\tread medication\n\r53\tMedication display test tool\n\r");
aeschsim 70:cc7ed2325202 192 printf("54\tWrite Medication\n\r55\tDemo Tool for Summer and RGB LED\n\r");
aeschsim 40:b5e533e1e033 193 pc.scanf("%d", &state);
cittecla 55:bdab541f434d 194 printf("Switch to State %d\n\r",state);
aeschsim 40:b5e533e1e033 195 fflush(stdin);
aeschsim 34:9d6dd3a12641 196 break;
aeschsim 40:b5e533e1e033 197
aeschsim 40:b5e533e1e033 198 /* Servo-Board Test Tool */
aeschsim 34:9d6dd3a12641 199 case 48:
cittecla 55:bdab541f434d 200 printf("\n\r\n\rServo-Board Test Tool\n\rEnter Channel Number and then Duty Cycle\n\r");
cittecla 55:bdab541f434d 201 printf("for exit, enter Channel > 15\n\r");
aeschsim 40:b5e533e1e033 202 channel = 0;
aeschsim 40:b5e533e1e033 203 while (channel < 16) {
cittecla 55:bdab541f434d 204 printf("Channel Number: ");
aeschsim 40:b5e533e1e033 205 pc.scanf("%d", &channel);
cittecla 55:bdab541f434d 206 printf("\r\nchannel: %d\n\rDuty Cycle 0..4095: ",channel);
aeschsim 40:b5e533e1e033 207 pc.scanf("%d", &dutyCycle);
cittecla 55:bdab541f434d 208 printf("\n\r%d\n\r",dutyCycle);
aeschsim 46:cdc24a24e4e3 209 setPWM((char)channel, (int)dutyCycle);
aeschsim 40:b5e533e1e033 210 fflush(stdin);
aeschsim 40:b5e533e1e033 211 }
cittecla 55:bdab541f434d 212 printf("leave Servo-Board Test Tool\n\r");
aeschsim 40:b5e533e1e033 213 state = 47;
aeschsim 40:b5e533e1e033 214 break;
aeschsim 40:b5e533e1e033 215
aeschsim 40:b5e533e1e033 216 case 49:
cittecla 42:ba59ed6a2a06 217 enableOutput();
cittecla 55:bdab541f434d 218 printf("Servo output enabled\r\n\n");
aeschsim 40:b5e533e1e033 219 state = 47;
aeschsim 34:9d6dd3a12641 220 break;
cittecla 2:72886e996098 221
aeschsim 34:9d6dd3a12641 222 case 50:
cittecla 42:ba59ed6a2a06 223 disableOutput();
cittecla 55:bdab541f434d 224 printf("Servo output disabled\r\n\n");
aeschsim 40:b5e533e1e033 225 state = 47;
aeschsim 40:b5e533e1e033 226 break;
cittecla 53:1c61cadbcb35 227
cittecla 42:ba59ed6a2a06 228 case 51:
cittecla 52:701d0c2f47d7 229 testSd();
cittecla 55:bdab541f434d 230 printf("\rSD Card test finished\r\n\n");
aeschsim 40:b5e533e1e033 231 state = 47;
aeschsim 34:9d6dd3a12641 232 break;
cittecla 2:72886e996098 233
cittecla 2:72886e996098 234
cittecla 55:bdab541f434d 235 case 52:
cittecla 56:218601547d13 236 user[1] = readMedication(1);
cittecla 56:218601547d13 237 if(user[1].valid) {
cittecla 56:218601547d13 238 printf("\rread successful\r\n\n");
cittecla 55:bdab541f434d 239 } else {
cittecla 55:bdab541f434d 240 printf("\rread failed\r\n\n");
cittecla 55:bdab541f434d 241 }
aeschsim 40:b5e533e1e033 242 state = 47;
aeschsim 40:b5e533e1e033 243 break;
aeschsim 40:b5e533e1e033 244
cittecla 55:bdab541f434d 245 case 53:
cittecla 56:218601547d13 246 if(user[1].valid == false) {
cittecla 57:79fed71031da 247 printf("\r\nNo valid data in struct, try to run case 52\r\n\n");
cittecla 55:bdab541f434d 248 } else {
cittecla 55:bdab541f434d 249 printf("\r\nMedication display test tool:\r\n");
cittecla 55:bdab541f434d 250 printf("Enter Day (0..6) and Moment (0..4)\r\nTo exit enter Day > 6:\r\n");
cittecla 55:bdab541f434d 251 day = 0;
cittecla 55:bdab541f434d 252 while (day < 7) {
cittecla 55:bdab541f434d 253 printf("\r\nDay Number: ");
cittecla 55:bdab541f434d 254 pc.scanf("%d", &day);
cittecla 55:bdab541f434d 255 if(day < 7) {
cittecla 55:bdab541f434d 256 printf("\r\nDay: %d\n\rMoment 0..3: ",day);
cittecla 55:bdab541f434d 257 pc.scanf("%d", &moment);
cittecla 55:bdab541f434d 258 printf("\r\nMedication for weekday %d Moment %d is ",day,moment);
cittecla 55:bdab541f434d 259 for(int i=0; i<6; i++) {
cittecla 56:218601547d13 260 printf("%d", user[1].medication.day[day].moment[moment].medContainer.container[i]);
cittecla 55:bdab541f434d 261 }
cittecla 58:cda5298c9b7f 262 printf("\r\nThe alarm clock is set for %02d:%02d",user[1].medication.day[day].moment[moment].time.hour,user[1].medication.day[day].moment[moment].time.minute);
cittecla 55:bdab541f434d 263 }
cittecla 55:bdab541f434d 264 printf("\r\n");
cittecla 55:bdab541f434d 265 fflush(stdin);
cittecla 55:bdab541f434d 266 }
cittecla 55:bdab541f434d 267 printf("exit Tool\n\r");
cittecla 55:bdab541f434d 268 }
cittecla 56:218601547d13 269 state = 47;
cittecla 56:218601547d13 270 break;
cittecla 58:cda5298c9b7f 271 case 54:
cittecla 58:cda5298c9b7f 272 writeMedication(0,user[1]);
cittecla 58:cda5298c9b7f 273 state = 47;
cittecla 58:cda5298c9b7f 274 break;
aeschsim 59:1867088695f3 275 case 55:
aeschsim 59:1867088695f3 276 /* Demo Tool */
PESGruppe1 61:086bcd5ca968 277 enableOutput();
PESGruppe1 61:086bcd5ca968 278 printf("Servo output enabled\r\n\n");
aeschsim 70:cc7ed2325202 279 summer.period_us(500);
aeschsim 70:cc7ed2325202 280 summer.write(0.5f);
aeschsim 70:cc7ed2325202 281 wait(3);
aeschsim 70:cc7ed2325202 282 summer.write(0.0f);
PESGruppe1 61:086bcd5ca968 283
aeschsim 70:cc7ed2325202 284 userButton1.fall(&summ);
aeschsim 70:cc7ed2325202 285
aeschsim 70:cc7ed2325202 286 printf("Summer Interrupt on Button 0 enabled\n\r");
aeschsim 60:7554e4272b3b 287
PESGruppe1 61:086bcd5ca968 288 color = 0xFFFFFF;
PESGruppe1 61:086bcd5ca968 289 colorTime = 0;
PESGruppe1 61:086bcd5ca968 290 setLED(0,color);
PESGruppe1 61:086bcd5ca968 291 wait(5);
aeschsim 70:cc7ed2325202 292
aeschsim 70:cc7ed2325202 293 printf("start fading loop for DEMOTIME loops\n\r");
aeschsim 70:cc7ed2325202 294
aeschsim 59:1867088695f3 295 while(colorTime < DEMOTIME) {
PESGruppe1 61:086bcd5ca968 296 printf("fade1\n\r");
aeschsim 60:7554e4272b3b 297 for(r = 0; r <= 255; r++) {
aeschsim 59:1867088695f3 298 color &= 0x00FFFF;
PESGruppe1 61:086bcd5ca968 299 color |= (r<<16);
PESGruppe1 61:086bcd5ca968 300 setLED(0,color);
aeschsim 59:1867088695f3 301 wait_ms(FADESPEED);
aeschsim 59:1867088695f3 302 }
aeschsim 70:cc7ed2325202 303 printf("fade2\n\r");
aeschsim 60:7554e4272b3b 304 for(b = 255; b >= 0; b--) {
aeschsim 59:1867088695f3 305 color &= 0xFFFF00;
PESGruppe1 61:086bcd5ca968 306 color |= b;
PESGruppe1 61:086bcd5ca968 307 setLED(0,color);
aeschsim 59:1867088695f3 308 wait_ms(FADESPEED);
aeschsim 59:1867088695f3 309 }
aeschsim 70:cc7ed2325202 310 printf("fade3\n\r");
aeschsim 60:7554e4272b3b 311 for(g = 0; g <= 255; g++) {
aeschsim 59:1867088695f3 312 color &= 0xFF00FF;
PESGruppe1 61:086bcd5ca968 313 color |= (g<<8);
PESGruppe1 61:086bcd5ca968 314 setLED(0,color);
aeschsim 59:1867088695f3 315 wait_ms(FADESPEED);
aeschsim 59:1867088695f3 316 }
aeschsim 70:cc7ed2325202 317 printf("fade4\n\r");
aeschsim 59:1867088695f3 318 for(r = 255; r > 0; r--) {
aeschsim 59:1867088695f3 319 color &= 0x00FFFF;
PESGruppe1 61:086bcd5ca968 320 color |= (r<<16);
PESGruppe1 61:086bcd5ca968 321 setLED(0,color);
aeschsim 59:1867088695f3 322 wait_ms(FADESPEED);
aeschsim 59:1867088695f3 323 }
aeschsim 70:cc7ed2325202 324 printf("fade5\n\r");
aeschsim 59:1867088695f3 325 for(b = 0; b <= 255; b++) {
aeschsim 59:1867088695f3 326 color &= 0xFFFF00;
PESGruppe1 61:086bcd5ca968 327 color |= b;
PESGruppe1 61:086bcd5ca968 328 setLED(0,color);
aeschsim 59:1867088695f3 329 wait_ms(FADESPEED);
aeschsim 59:1867088695f3 330 }
aeschsim 70:cc7ed2325202 331 printf("fade6\n\r");
aeschsim 59:1867088695f3 332 for (g = 255; g > 0; g--) {
aeschsim 59:1867088695f3 333 color &= 0xFF00FF;
PESGruppe1 61:086bcd5ca968 334 color |= (g<<8);
PESGruppe1 61:086bcd5ca968 335 setLED(0,color);
aeschsim 59:1867088695f3 336 wait_ms(FADESPEED);
aeschsim 59:1867088695f3 337 }
aeschsim 59:1867088695f3 338 colorTime++;
aeschsim 59:1867088695f3 339 }
aeschsim 70:cc7ed2325202 340 printf("exit demo tool\n\r");
aeschsim 59:1867088695f3 341
aeschsim 59:1867088695f3 342 break;
aeschsim 40:b5e533e1e033 343 default:
aeschsim 40:b5e533e1e033 344 state = 47; /* Go to State Switch */
aeschsim 34:9d6dd3a12641 345 break;
aeschsim 34:9d6dd3a12641 346 }
cittecla 2:72886e996098 347 }
cittecla 55:bdab541f434d 348 printf("exit while 1 loop");
cittecla 2:72886e996098 349 }