Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem mbed
Fork of PES4_Programme by
source/main.cpp@60:7554e4272b3b, 2018-04-05 (annotated)
- Committer:
- aeschsim
- Date:
- Thu Apr 05 14:12:27 2018 +0000
- Revision:
- 60:7554e4272b3b
- Parent:
- 59:1867088695f3
- Child:
- 61:086bcd5ca968
implemented summer switch on user button 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cittecla | 3:94026b29e994 | 1 | #include "main.h" |
aeschsim | 59:1867088695f3 | 2 | #define FADESPEED 5 |
aeschsim | 59:1867088695f3 | 3 | #define DEMOTIME 5 |
itslinear | 17:bbafd216e059 | 4 | |
cittecla | 52:701d0c2f47d7 | 5 | int state = 44; |
cittecla | 41:f054a83f9556 | 6 | int oldState; |
cittecla | 2:72886e996098 | 7 | |
cittecla | 2:72886e996098 | 8 | Serial pc(USBTX, USBRX); // tx, rx |
aeschsim | 40:b5e533e1e033 | 9 | |
aeschsim | 60:7554e4272b3b | 10 | DigitalIn lichtschranke(PC_5); |
aeschsim | 60:7554e4272b3b | 11 | InterruptIn userButton1(PB_5); |
aeschsim | 60:7554e4272b3b | 12 | InterruptIn userButton2(PB_4); |
aeschsim | 60:7554e4272b3b | 13 | DigitalIn glasDetection(PA_10); |
aeschsim | 60:7554e4272b3b | 14 | PwmOut summer(PC_8); |
aeschsim | 59:1867088695f3 | 15 | |
aeschsim | 40:b5e533e1e033 | 16 | /* PWM Test Tool */ |
aeschsim | 40:b5e533e1e033 | 17 | int channel; |
aeschsim | 40:b5e533e1e033 | 18 | int dutyCycle; |
cittecla | 55:bdab541f434d | 19 | int day; |
cittecla | 55:bdab541f434d | 20 | int moment; |
cittecla | 55:bdab541f434d | 21 | s_user user[2]; |
cittecla | 2:72886e996098 | 22 | |
aeschsim | 59:1867088695f3 | 23 | /* Demo Tool */ |
aeschsim | 59:1867088695f3 | 24 | uint32_t color = 0; |
aeschsim | 59:1867088695f3 | 25 | int colorTime = 0; |
aeschsim | 59:1867088695f3 | 26 | int r,b,g; |
aeschsim | 59:1867088695f3 | 27 | |
aeschsim | 60:7554e4272b3b | 28 | void summ(void) |
aeschsim | 60:7554e4272b3b | 29 | { |
aeschsim | 60:7554e4272b3b | 30 | if(summer.read()>0) { |
aeschsim | 60:7554e4272b3b | 31 | summer.write(0.0f); |
aeschsim | 60:7554e4272b3b | 32 | } else { |
aeschsim | 60:7554e4272b3b | 33 | summer.write(0.5f); |
aeschsim | 60:7554e4272b3b | 34 | } |
aeschsim | 60:7554e4272b3b | 35 | } |
aeschsim | 60:7554e4272b3b | 36 | |
aeschsim | 60:7554e4272b3b | 37 | |
cittecla | 2:72886e996098 | 38 | int main() |
cittecla | 2:72886e996098 | 39 | { |
cittecla | 2:72886e996098 | 40 | pc.baud(460800); |
cittecla | 2:72886e996098 | 41 | printf("start...\r\n"); |
cittecla | 2:72886e996098 | 42 | |
aeschsim | 34:9d6dd3a12641 | 43 | while(1) { |
cittecla | 41:f054a83f9556 | 44 | if(state != oldState) { |
cittecla | 58:cda5298c9b7f | 45 | printf("\r\nstate: %d\r\n",state); |
cittecla | 41:f054a83f9556 | 46 | oldState = state; |
aeschsim | 34:9d6dd3a12641 | 47 | } |
aeschsim | 34:9d6dd3a12641 | 48 | switch (state) { |
aeschsim | 34:9d6dd3a12641 | 49 | case 0: |
aeschsim | 34:9d6dd3a12641 | 50 | printf("Hello World"); |
aeschsim | 40:b5e533e1e033 | 51 | state = 47; |
aeschsim | 34:9d6dd3a12641 | 52 | break; |
aeschsim | 34:9d6dd3a12641 | 53 | case 1: |
aeschsim | 34:9d6dd3a12641 | 54 | break; |
aeschsim | 34:9d6dd3a12641 | 55 | case 2: |
aeschsim | 34:9d6dd3a12641 | 56 | break; |
aeschsim | 34:9d6dd3a12641 | 57 | case 3: |
aeschsim | 34:9d6dd3a12641 | 58 | break; |
cittecla | 2:72886e996098 | 59 | |
aeschsim | 34:9d6dd3a12641 | 60 | case 10: |
aeschsim | 34:9d6dd3a12641 | 61 | break; |
aeschsim | 34:9d6dd3a12641 | 62 | case 11: |
aeschsim | 34:9d6dd3a12641 | 63 | break; |
cittecla | 2:72886e996098 | 64 | |
aeschsim | 34:9d6dd3a12641 | 65 | case 15: |
aeschsim | 34:9d6dd3a12641 | 66 | break; |
aeschsim | 34:9d6dd3a12641 | 67 | case 16: |
aeschsim | 34:9d6dd3a12641 | 68 | break; |
aeschsim | 34:9d6dd3a12641 | 69 | case 17: |
aeschsim | 34:9d6dd3a12641 | 70 | break; |
aeschsim | 34:9d6dd3a12641 | 71 | |
aeschsim | 34:9d6dd3a12641 | 72 | case 25: |
aeschsim | 34:9d6dd3a12641 | 73 | break; |
aeschsim | 34:9d6dd3a12641 | 74 | case 26: |
aeschsim | 34:9d6dd3a12641 | 75 | break; |
aeschsim | 34:9d6dd3a12641 | 76 | case 27: |
aeschsim | 34:9d6dd3a12641 | 77 | break; |
aeschsim | 34:9d6dd3a12641 | 78 | case 28: |
aeschsim | 34:9d6dd3a12641 | 79 | break; |
cittecla | 2:72886e996098 | 80 | |
cittecla | 2:72886e996098 | 81 | |
aeschsim | 34:9d6dd3a12641 | 82 | case 35: |
aeschsim | 34:9d6dd3a12641 | 83 | break; |
aeschsim | 34:9d6dd3a12641 | 84 | case 36: |
aeschsim | 34:9d6dd3a12641 | 85 | break; |
aeschsim | 34:9d6dd3a12641 | 86 | case 37: |
aeschsim | 34:9d6dd3a12641 | 87 | break; |
aeschsim | 34:9d6dd3a12641 | 88 | case 38: |
aeschsim | 34:9d6dd3a12641 | 89 | break; |
aeschsim | 27:bf0577ecfa6e | 90 | |
aeschsim | 40:b5e533e1e033 | 91 | /**************************************************************/ |
aeschsim | 40:b5e533e1e033 | 92 | /* Test Zone */ |
cittecla | 53:1c61cadbcb35 | 93 | |
cittecla | 52:701d0c2f47d7 | 94 | /* init PWM board */ |
cittecla | 52:701d0c2f47d7 | 95 | case 44: |
cittecla | 55:bdab541f434d | 96 | printf("Init PWM\r\n"); |
cittecla | 53:1c61cadbcb35 | 97 | initPWM(50); |
cittecla | 53:1c61cadbcb35 | 98 | enableOutput(); |
cittecla | 52:701d0c2f47d7 | 99 | state = 47; |
cittecla | 52:701d0c2f47d7 | 100 | break; |
aeschsim | 40:b5e533e1e033 | 101 | |
aeschsim | 40:b5e533e1e033 | 102 | /* Time test tool: set Time */ |
aeschsim | 34:9d6dd3a12641 | 103 | case 45: |
aeschsim | 34:9d6dd3a12641 | 104 | s_time time; |
aeschsim | 34:9d6dd3a12641 | 105 | time.second = 30; |
aeschsim | 34:9d6dd3a12641 | 106 | time.minute = 53; |
aeschsim | 34:9d6dd3a12641 | 107 | time.hour = 12; |
aeschsim | 34:9d6dd3a12641 | 108 | time.day = 27; |
aeschsim | 34:9d6dd3a12641 | 109 | time.weekday = 2; |
aeschsim | 34:9d6dd3a12641 | 110 | time.month = 3; |
aeschsim | 34:9d6dd3a12641 | 111 | time.year = 18; |
cittecla | 55:bdab541f434d | 112 | printf("set now time\r\n"); |
aeschsim | 34:9d6dd3a12641 | 113 | setTime(&time); |
cittecla | 55:bdab541f434d | 114 | 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 | 115 | state = 47; |
cittecla | 55:bdab541f434d | 116 | printf("change state to: %d\r\n",state); |
aeschsim | 34:9d6dd3a12641 | 117 | break; |
aeschsim | 40:b5e533e1e033 | 118 | |
aeschsim | 40:b5e533e1e033 | 119 | /* Time test tool: get Time */ |
aeschsim | 34:9d6dd3a12641 | 120 | case 46: |
aeschsim | 34:9d6dd3a12641 | 121 | wait(5); |
aeschsim | 34:9d6dd3a12641 | 122 | time = getTime(); |
cittecla | 55:bdab541f434d | 123 | 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 | 124 | state = 47; |
aeschsim | 40:b5e533e1e033 | 125 | break; |
aeschsim | 40:b5e533e1e033 | 126 | |
aeschsim | 40:b5e533e1e033 | 127 | /* Console State Switch */ |
aeschsim | 40:b5e533e1e033 | 128 | case 47: |
cittecla | 55:bdab541f434d | 129 | printf("\n\rState Switch: Enter next State: \n\r"); |
cittecla | 55:bdab541f434d | 130 | 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 | 40:b5e533e1e033 | 131 | pc.scanf("%d", &state); |
cittecla | 55:bdab541f434d | 132 | printf("Switch to State %d\n\r",state); |
aeschsim | 40:b5e533e1e033 | 133 | fflush(stdin); |
aeschsim | 34:9d6dd3a12641 | 134 | break; |
aeschsim | 40:b5e533e1e033 | 135 | |
aeschsim | 40:b5e533e1e033 | 136 | /* Servo-Board Test Tool */ |
aeschsim | 34:9d6dd3a12641 | 137 | case 48: |
cittecla | 55:bdab541f434d | 138 | printf("\n\r\n\rServo-Board Test Tool\n\rEnter Channel Number and then Duty Cycle\n\r"); |
cittecla | 55:bdab541f434d | 139 | printf("for exit, enter Channel > 15\n\r"); |
aeschsim | 40:b5e533e1e033 | 140 | channel = 0; |
aeschsim | 40:b5e533e1e033 | 141 | while (channel < 16) { |
cittecla | 55:bdab541f434d | 142 | printf("Channel Number: "); |
aeschsim | 40:b5e533e1e033 | 143 | pc.scanf("%d", &channel); |
cittecla | 55:bdab541f434d | 144 | printf("\r\nchannel: %d\n\rDuty Cycle 0..4095: ",channel); |
aeschsim | 40:b5e533e1e033 | 145 | pc.scanf("%d", &dutyCycle); |
cittecla | 55:bdab541f434d | 146 | printf("\n\r%d\n\r",dutyCycle); |
aeschsim | 46:cdc24a24e4e3 | 147 | setPWM((char)channel, (int)dutyCycle); |
aeschsim | 40:b5e533e1e033 | 148 | fflush(stdin); |
aeschsim | 40:b5e533e1e033 | 149 | } |
cittecla | 55:bdab541f434d | 150 | printf("leave Servo-Board Test Tool\n\r"); |
aeschsim | 40:b5e533e1e033 | 151 | state = 47; |
aeschsim | 40:b5e533e1e033 | 152 | break; |
aeschsim | 40:b5e533e1e033 | 153 | |
aeschsim | 40:b5e533e1e033 | 154 | case 49: |
cittecla | 42:ba59ed6a2a06 | 155 | enableOutput(); |
cittecla | 55:bdab541f434d | 156 | printf("Servo output enabled\r\n\n"); |
aeschsim | 40:b5e533e1e033 | 157 | state = 47; |
aeschsim | 34:9d6dd3a12641 | 158 | break; |
cittecla | 2:72886e996098 | 159 | |
aeschsim | 34:9d6dd3a12641 | 160 | case 50: |
cittecla | 42:ba59ed6a2a06 | 161 | disableOutput(); |
cittecla | 55:bdab541f434d | 162 | printf("Servo output disabled\r\n\n"); |
aeschsim | 40:b5e533e1e033 | 163 | state = 47; |
aeschsim | 40:b5e533e1e033 | 164 | break; |
cittecla | 53:1c61cadbcb35 | 165 | |
cittecla | 42:ba59ed6a2a06 | 166 | case 51: |
cittecla | 52:701d0c2f47d7 | 167 | testSd(); |
cittecla | 55:bdab541f434d | 168 | printf("\rSD Card test finished\r\n\n"); |
cittecla | 55:bdab541f434d | 169 | state = 47; |
cittecla | 55:bdab541f434d | 170 | break; |
cittecla | 58:cda5298c9b7f | 171 | |
cittecla | 58:cda5298c9b7f | 172 | |
cittecla | 55:bdab541f434d | 173 | case 52: |
cittecla | 56:218601547d13 | 174 | user[1] = readMedication(1); |
cittecla | 56:218601547d13 | 175 | if(user[1].valid) { |
cittecla | 56:218601547d13 | 176 | printf("\rread successful\r\n\n"); |
cittecla | 55:bdab541f434d | 177 | } else { |
cittecla | 55:bdab541f434d | 178 | printf("\rread failed\r\n\n"); |
cittecla | 55:bdab541f434d | 179 | } |
cittecla | 42:ba59ed6a2a06 | 180 | state = 47; |
cittecla | 53:1c61cadbcb35 | 181 | break; |
cittecla | 55:bdab541f434d | 182 | |
cittecla | 55:bdab541f434d | 183 | case 53: |
cittecla | 56:218601547d13 | 184 | if(user[1].valid == false) { |
cittecla | 57:79fed71031da | 185 | printf("\r\nNo valid data in struct, try to run case 52\r\n\n"); |
cittecla | 55:bdab541f434d | 186 | } else { |
cittecla | 55:bdab541f434d | 187 | printf("\r\nMedication display test tool:\r\n"); |
cittecla | 55:bdab541f434d | 188 | printf("Enter Day (0..6) and Moment (0..4)\r\nTo exit enter Day > 6:\r\n"); |
cittecla | 55:bdab541f434d | 189 | day = 0; |
cittecla | 55:bdab541f434d | 190 | while (day < 7) { |
cittecla | 55:bdab541f434d | 191 | printf("\r\nDay Number: "); |
cittecla | 55:bdab541f434d | 192 | pc.scanf("%d", &day); |
cittecla | 55:bdab541f434d | 193 | if(day < 7) { |
cittecla | 55:bdab541f434d | 194 | printf("\r\nDay: %d\n\rMoment 0..3: ",day); |
cittecla | 55:bdab541f434d | 195 | pc.scanf("%d", &moment); |
cittecla | 55:bdab541f434d | 196 | printf("\r\nMedication for weekday %d Moment %d is ",day,moment); |
cittecla | 55:bdab541f434d | 197 | for(int i=0; i<6; i++) { |
cittecla | 56:218601547d13 | 198 | printf("%d", user[1].medication.day[day].moment[moment].medContainer.container[i]); |
cittecla | 55:bdab541f434d | 199 | } |
cittecla | 58:cda5298c9b7f | 200 | 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 | 201 | } |
cittecla | 55:bdab541f434d | 202 | printf("\r\n"); |
cittecla | 55:bdab541f434d | 203 | fflush(stdin); |
cittecla | 55:bdab541f434d | 204 | } |
cittecla | 55:bdab541f434d | 205 | printf("exit Tool\n\r"); |
cittecla | 55:bdab541f434d | 206 | } |
cittecla | 56:218601547d13 | 207 | state = 47; |
cittecla | 56:218601547d13 | 208 | break; |
cittecla | 58:cda5298c9b7f | 209 | case 54: |
cittecla | 58:cda5298c9b7f | 210 | writeMedication(0,user[1]); |
cittecla | 58:cda5298c9b7f | 211 | state = 47; |
cittecla | 58:cda5298c9b7f | 212 | break; |
aeschsim | 59:1867088695f3 | 213 | case 55: |
aeschsim | 59:1867088695f3 | 214 | /* Demo Tool */ |
aeschsim | 60:7554e4272b3b | 215 | summer.period_us(500); |
aeschsim | 60:7554e4272b3b | 216 | summer.write(0.5f); |
aeschsim | 60:7554e4272b3b | 217 | wait(3); |
aeschsim | 60:7554e4272b3b | 218 | summer.write(0.0f); |
aeschsim | 60:7554e4272b3b | 219 | |
aeschsim | 60:7554e4272b3b | 220 | userButton1.fall(&summ); |
aeschsim | 60:7554e4272b3b | 221 | |
aeschsim | 59:1867088695f3 | 222 | color = 0x0055AA; |
aeschsim | 59:1867088695f3 | 223 | while(colorTime < DEMOTIME) { |
aeschsim | 60:7554e4272b3b | 224 | for(r = 0; r <= 255; r++) { |
aeschsim | 59:1867088695f3 | 225 | color &= 0x00FFFF; |
aeschsim | 59:1867088695f3 | 226 | color &= (r<<4); |
aeschsim | 59:1867088695f3 | 227 | wait_ms(FADESPEED); |
aeschsim | 59:1867088695f3 | 228 | } |
aeschsim | 60:7554e4272b3b | 229 | for(b = 255; b >= 0; b--) { |
aeschsim | 59:1867088695f3 | 230 | color &= 0xFFFF00; |
aeschsim | 59:1867088695f3 | 231 | color &= b; |
aeschsim | 59:1867088695f3 | 232 | wait_ms(FADESPEED); |
aeschsim | 59:1867088695f3 | 233 | } |
aeschsim | 60:7554e4272b3b | 234 | for(g = 0; g <= 255; g++) { |
aeschsim | 59:1867088695f3 | 235 | color &= 0xFF00FF; |
aeschsim | 59:1867088695f3 | 236 | color &= (g<<2); |
aeschsim | 59:1867088695f3 | 237 | wait_ms(FADESPEED); |
aeschsim | 59:1867088695f3 | 238 | } |
aeschsim | 59:1867088695f3 | 239 | for(r = 255; r > 0; r--) { |
aeschsim | 59:1867088695f3 | 240 | color &= 0x00FFFF; |
aeschsim | 59:1867088695f3 | 241 | color &= (r<<4); |
aeschsim | 59:1867088695f3 | 242 | wait_ms(FADESPEED); |
aeschsim | 59:1867088695f3 | 243 | } |
aeschsim | 59:1867088695f3 | 244 | for(b = 0; b <= 255; b++) { |
aeschsim | 59:1867088695f3 | 245 | color &= 0xFFFF00; |
aeschsim | 59:1867088695f3 | 246 | color &= b; |
aeschsim | 59:1867088695f3 | 247 | wait_ms(FADESPEED); |
aeschsim | 59:1867088695f3 | 248 | } |
aeschsim | 59:1867088695f3 | 249 | for (g = 255; g > 0; g--) { |
aeschsim | 59:1867088695f3 | 250 | color &= 0xFF00FF; |
aeschsim | 59:1867088695f3 | 251 | color &= (g<<2); |
aeschsim | 59:1867088695f3 | 252 | wait_ms(FADESPEED); |
aeschsim | 59:1867088695f3 | 253 | } |
aeschsim | 59:1867088695f3 | 254 | colorTime++; |
aeschsim | 59:1867088695f3 | 255 | } |
aeschsim | 59:1867088695f3 | 256 | |
aeschsim | 59:1867088695f3 | 257 | break; |
aeschsim | 40:b5e533e1e033 | 258 | default: |
aeschsim | 40:b5e533e1e033 | 259 | state = 47; /* Go to State Switch */ |
aeschsim | 34:9d6dd3a12641 | 260 | break; |
aeschsim | 34:9d6dd3a12641 | 261 | } |
cittecla | 2:72886e996098 | 262 | } |
cittecla | 55:bdab541f434d | 263 | printf("exit while 1 loop"); |
cittecla | 2:72886e996098 | 264 | } |