Level 2 Project Range Device
Dependencies: N5110 SDFileSystem SRF02 TMP102 mbed
Fork of Ranger by
main.cpp@4:673930f04866, 2016-04-14 (annotated)
- Committer:
- el15pjt
- Date:
- Thu Apr 14 14:23:37 2016 +0000
- Revision:
- 4:673930f04866
- Parent:
- 3:8782b8b8658b
- Child:
- 5:083d484e95a1
Buzzer added, needs tidying.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el15pjt | 0:0b180348c7e4 | 1 | /* |
el15pjt | 0:0b180348c7e4 | 2 | Philip Thompsonn |
el15pjt | 0:0b180348c7e4 | 3 | EL15PJT |
el15pjt | 0:0b180348c7e4 | 4 | Embedded System Project |
el15pjt | 0:0b180348c7e4 | 5 | EL2645 |
el15pjt | 0:0b180348c7e4 | 6 | */ |
el15pjt | 0:0b180348c7e4 | 7 | |
el15pjt | 0:0b180348c7e4 | 8 | #include "mbed.h" |
el15pjt | 0:0b180348c7e4 | 9 | #include "SRF02.h" |
el15pjt | 0:0b180348c7e4 | 10 | #include "N5110.h" |
el15pjt | 0:0b180348c7e4 | 11 | |
el15pjt | 2:329597081c06 | 12 | #define LOW 0 |
el15pjt | 2:329597081c06 | 13 | #define HIGH 1 |
el15pjt | 2:329597081c06 | 14 | |
el15pjt | 3:8782b8b8658b | 15 | |
el15pjt | 2:329597081c06 | 16 | DigitalOut rr_led (PTA1); |
el15pjt | 2:329597081c06 | 17 | DigitalOut a_led (PTC2); |
el15pjt | 2:329597081c06 | 18 | DigitalOut gg_led(PTB23); |
el15pjt | 4:673930f04866 | 19 | PwmOut buzzer(PTA2); |
el15pjt | 3:8782b8b8658b | 20 | |
el15pjt | 0:0b180348c7e4 | 21 | // Ranger object |
el15pjt | 0:0b180348c7e4 | 22 | SRF02 srf02(I2C_SDA,I2C_SCL); |
el15pjt | 0:0b180348c7e4 | 23 | |
el15pjt | 0:0b180348c7e4 | 24 | //N5110 Object VCC, SCE,RST, D/C, MOSI,SCLK, LED |
el15pjt | 0:0b180348c7e4 | 25 | N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3); |
el15pjt | 0:0b180348c7e4 | 26 | |
el15pjt | 2:329597081c06 | 27 | Ticker ticker; |
el15pjt | 3:8782b8b8658b | 28 | Ticker ticker_srf02; |
el15pjt | 4:673930f04866 | 29 | Ticker ticker_tone; |
el15pjt | 4:673930f04866 | 30 | Timeout buzzoff; |
el15pjt | 4:673930f04866 | 31 | |
el15pjt | 4:673930f04866 | 32 | |
el15pjt | 2:329597081c06 | 33 | |
el15pjt | 0:0b180348c7e4 | 34 | // K64F on-board LEDs |
el15pjt | 0:0b180348c7e4 | 35 | DigitalOut r_led(LED_RED); |
el15pjt | 0:0b180348c7e4 | 36 | DigitalOut g_led(LED_GREEN); |
el15pjt | 0:0b180348c7e4 | 37 | DigitalOut b_led(LED_BLUE); |
el15pjt | 0:0b180348c7e4 | 38 | |
el15pjt | 0:0b180348c7e4 | 39 | // K64F on-board switches |
el15pjt | 3:8782b8b8658b | 40 | //InterruptIn sw1(SW2); |
el15pjt | 3:8782b8b8658b | 41 | InterruptIn sw2(SW3); |
el15pjt | 0:0b180348c7e4 | 42 | |
el15pjt | 2:329597081c06 | 43 | volatile int g_timer_flag = 0; |
el15pjt | 3:8782b8b8658b | 44 | volatile int g_timer_flag_srf02 = 0; |
el15pjt | 3:8782b8b8658b | 45 | volatile int g_sw2_flag = 0; |
el15pjt | 4:673930f04866 | 46 | volatile int g_timer_flag_tone = 0; |
el15pjt | 4:673930f04866 | 47 | volatile int g_sw1_flag = 0; |
el15pjt | 2:329597081c06 | 48 | |
el15pjt | 3:8782b8b8658b | 49 | int alert; |
el15pjt | 0:0b180348c7e4 | 50 | int distance; |
el15pjt | 3:8782b8b8658b | 51 | float bright = 1; |
el15pjt | 0:0b180348c7e4 | 52 | |
el15pjt | 2:329597081c06 | 53 | // struct for state |
el15pjt | 2:329597081c06 | 54 | struct Alertlevel { |
el15pjt | 2:329597081c06 | 55 | char srr_led; // output value |
el15pjt | 2:329597081c06 | 56 | char sa_led; // time in state |
el15pjt | 2:329597081c06 | 57 | char sgg_led; // array of next states |
el15pjt | 2:329597081c06 | 58 | char frr_led; |
el15pjt | 2:329597081c06 | 59 | char fa_led; |
el15pjt | 2:329597081c06 | 60 | char fgg_led; |
el15pjt | 4:673930f04866 | 61 | float toneon; |
el15pjt | 4:673930f04866 | 62 | float toneoff; |
el15pjt | 2:329597081c06 | 63 | }; |
el15pjt | 2:329597081c06 | 64 | typedef const struct Alertlevel STyp; |
el15pjt | 2:329597081c06 | 65 | |
el15pjt | 2:329597081c06 | 66 | STyp Alertlevel[8] = { |
el15pjt | 4:673930f04866 | 67 | {LOW,LOW,LOW,LOW,LOW,LOW,0,1}, // no output |
el15pjt | 4:673930f04866 | 68 | {LOW,LOW,LOW,LOW,LOW,HIGH,0.05,1}, //flash green |
el15pjt | 4:673930f04866 | 69 | {LOW,LOW,HIGH,LOW,LOW,LOW,0.1,1}, //steady green |
el15pjt | 4:673930f04866 | 70 | {LOW,LOW,HIGH,LOW,HIGH,LOW,0.2,0.5}, //flash amber |
el15pjt | 4:673930f04866 | 71 | {LOW,HIGH,HIGH,LOW,LOW,LOW,0.55,0.25}, //steady amber |
el15pjt | 4:673930f04866 | 72 | {LOW,HIGH,HIGH,HIGH,LOW,LOW,0.8,0.1}, //flash red |
el15pjt | 4:673930f04866 | 73 | {HIGH,HIGH,HIGH,LOW,LOW,LOW,1,1.05},// steady red |
el15pjt | 4:673930f04866 | 74 | {LOW,LOW,LOW,HIGH,HIGH,HIGH,1,0.05} // all flash |
el15pjt | 2:329597081c06 | 75 | }; |
el15pjt | 3:8782b8b8658b | 76 | void lcdoutput(); |
el15pjt | 3:8782b8b8658b | 77 | void timer_isr(); // timed interuprt |
el15pjt | 3:8782b8b8658b | 78 | void timer_isr_srf02(); |
el15pjt | 4:673930f04866 | 79 | void timer_isr_tone(); |
el15pjt | 3:8782b8b8658b | 80 | void backlight(); |
el15pjt | 3:8782b8b8658b | 81 | void init_K64F(); // set up board |
el15pjt | 3:8782b8b8658b | 82 | void sw2_isr(); |
el15pjt | 3:8782b8b8658b | 83 | //void sw1_isr(); |
el15pjt | 3:8782b8b8658b | 84 | void setalert(); |
el15pjt | 3:8782b8b8658b | 85 | void setleds(); |
el15pjt | 4:673930f04866 | 86 | void setbuzzer(); |
el15pjt | 4:673930f04866 | 87 | void flip(); |
el15pjt | 4:673930f04866 | 88 | |
el15pjt | 0:0b180348c7e4 | 89 | |
el15pjt | 0:0b180348c7e4 | 90 | int main() |
el15pjt | 0:0b180348c7e4 | 91 | { |
el15pjt | 0:0b180348c7e4 | 92 | lcd.init(); |
el15pjt | 0:0b180348c7e4 | 93 | init_K64F(); |
el15pjt | 3:8782b8b8658b | 94 | ticker.attach(&timer_isr,0.35); |
el15pjt | 3:8782b8b8658b | 95 | ticker_srf02.attach(&timer_isr_srf02,0.02); |
el15pjt | 4:673930f04866 | 96 | ticker_tone.attach(&timer_isr_tone,Alertlevel[alert].toneoff); |
el15pjt | 3:8782b8b8658b | 97 | // sw1.fall(&sw1_isr); |
el15pjt | 3:8782b8b8658b | 98 | sw2.fall(&sw2_isr); |
el15pjt | 0:0b180348c7e4 | 99 | while(1) { |
el15pjt | 4:673930f04866 | 100 | |
el15pjt | 3:8782b8b8658b | 101 | if (g_sw2_flag) { |
el15pjt | 3:8782b8b8658b | 102 | g_sw2_flag = 0; // if it has, clear the flag |
el15pjt | 3:8782b8b8658b | 103 | backlight(); |
el15pjt | 2:329597081c06 | 104 | } |
el15pjt | 3:8782b8b8658b | 105 | if (g_timer_flag_srf02) { |
el15pjt | 3:8782b8b8658b | 106 | g_timer_flag_srf02 = 0; // if it has, clear the flag |
el15pjt | 4:673930f04866 | 107 | distance = srf02.getDistanceCm(); |
el15pjt | 0:0b180348c7e4 | 108 | } |
el15pjt | 4:673930f04866 | 109 | |
el15pjt | 4:673930f04866 | 110 | |
el15pjt | 3:8782b8b8658b | 111 | lcdoutput(); |
el15pjt | 3:8782b8b8658b | 112 | setalert(); |
el15pjt | 4:673930f04866 | 113 | |
el15pjt | 4:673930f04866 | 114 | setbuzzer(); |
el15pjt | 3:8782b8b8658b | 115 | setleds(); |
el15pjt | 4:673930f04866 | 116 | sleep(); |
el15pjt | 0:0b180348c7e4 | 117 | } |
el15pjt | 0:0b180348c7e4 | 118 | } |
el15pjt | 0:0b180348c7e4 | 119 | |
el15pjt | 0:0b180348c7e4 | 120 | //Set up board switches and LEDS |
el15pjt | 2:329597081c06 | 121 | void init_K64F() |
el15pjt | 2:329597081c06 | 122 | { |
el15pjt | 0:0b180348c7e4 | 123 | //on-board LEDs are active-low, so set pin high to turn them off. |
el15pjt | 2:329597081c06 | 124 | r_led = 1; |
el15pjt | 2:329597081c06 | 125 | g_led = 1; |
el15pjt | 2:329597081c06 | 126 | b_led = 1; |
el15pjt | 0:0b180348c7e4 | 127 | |
el15pjt | 0:0b180348c7e4 | 128 | // since the on-board switches have external pull-ups, we should disable the internal pull-down |
el15pjt | 0:0b180348c7e4 | 129 | // resistors that are enabled by default using InterruptIn |
el15pjt | 2:329597081c06 | 130 | sw2.mode(PullNone); |
el15pjt | 3:8782b8b8658b | 131 | // sw1.mode(PullNone); |
el15pjt | 3:8782b8b8658b | 132 | } |
el15pjt | 3:8782b8b8658b | 133 | void lcdoutput() |
el15pjt | 3:8782b8b8658b | 134 | { |
el15pjt | 3:8782b8b8658b | 135 | int length; |
el15pjt | 3:8782b8b8658b | 136 | int length1; |
el15pjt | 3:8782b8b8658b | 137 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
el15pjt | 3:8782b8b8658b | 138 | char buffer1[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
el15pjt | 3:8782b8b8658b | 139 | |
el15pjt | 3:8782b8b8658b | 140 | |
el15pjt | 3:8782b8b8658b | 141 | length = sprintf(buffer,"D = %i Cm",distance); |
el15pjt | 3:8782b8b8658b | 142 | length1 = sprintf(buffer1,"Bright = %f Cm",bright); |
el15pjt | 3:8782b8b8658b | 143 | |
el15pjt | 4:673930f04866 | 144 | if (length <= 14) { |
el15pjt | 3:8782b8b8658b | 145 | lcd.printString(buffer,0,0); |
el15pjt | 4:673930f04866 | 146 | lcd.printString(buffer1,0,1); |
el15pjt | 4:673930f04866 | 147 | } |
el15pjt | 3:8782b8b8658b | 148 | |
el15pjt | 3:8782b8b8658b | 149 | // draw a line across the display at y = 40 pixels (origin top-left) |
el15pjt | 3:8782b8b8658b | 150 | for (int i = 0; i < WIDTH; i++) { |
el15pjt | 3:8782b8b8658b | 151 | lcd.setPixel(i,45); |
el15pjt | 3:8782b8b8658b | 152 | } |
el15pjt | 3:8782b8b8658b | 153 | |
el15pjt | 3:8782b8b8658b | 154 | lcd.refresh(); // need to refresh display after setting pixels |
el15pjt | 3:8782b8b8658b | 155 | } |
el15pjt | 3:8782b8b8658b | 156 | |
el15pjt | 3:8782b8b8658b | 157 | void setalert() |
el15pjt | 3:8782b8b8658b | 158 | { |
el15pjt | 3:8782b8b8658b | 159 | if (distance >= 200 && distance < 250) { |
el15pjt | 3:8782b8b8658b | 160 | alert = 1; //flash green |
el15pjt | 3:8782b8b8658b | 161 | } else if (distance >= 150 && distance < 200) { |
el15pjt | 3:8782b8b8658b | 162 | alert = 2; //steady green |
el15pjt | 3:8782b8b8658b | 163 | } else if (distance >= 100 && distance < 150) { |
el15pjt | 3:8782b8b8658b | 164 | alert = 3; //flashing amber |
el15pjt | 3:8782b8b8658b | 165 | } else if (distance >= 50 && distance < 100) { |
el15pjt | 3:8782b8b8658b | 166 | alert = 4; //steady amber |
el15pjt | 3:8782b8b8658b | 167 | } else if ( distance > 10 && distance < 50) { |
el15pjt | 3:8782b8b8658b | 168 | alert = 5; //flashing red |
el15pjt | 3:8782b8b8658b | 169 | } else if (distance > 1 && distance <= 10) { |
el15pjt | 3:8782b8b8658b | 170 | alert = 6; //steady red |
el15pjt | 3:8782b8b8658b | 171 | } else if (distance <=1) { |
el15pjt | 3:8782b8b8658b | 172 | alert = 7; //all flashing |
el15pjt | 3:8782b8b8658b | 173 | } else { |
el15pjt | 3:8782b8b8658b | 174 | alert = 0; //no output |
el15pjt | 3:8782b8b8658b | 175 | } |
el15pjt | 2:329597081c06 | 176 | } |
el15pjt | 2:329597081c06 | 177 | |
el15pjt | 3:8782b8b8658b | 178 | void setleds() |
el15pjt | 3:8782b8b8658b | 179 | { |
el15pjt | 3:8782b8b8658b | 180 | // If statments to determin the output of each LED |
el15pjt | 3:8782b8b8658b | 181 | |
el15pjt | 3:8782b8b8658b | 182 | if (Alertlevel[alert].frr_led == HIGH) { |
el15pjt | 3:8782b8b8658b | 183 | if (g_timer_flag) { |
el15pjt | 3:8782b8b8658b | 184 | g_timer_flag = 0; // if it has, clear the flag |
el15pjt | 3:8782b8b8658b | 185 | rr_led = !rr_led; |
el15pjt | 3:8782b8b8658b | 186 | } |
el15pjt | 3:8782b8b8658b | 187 | } else { |
el15pjt | 3:8782b8b8658b | 188 | rr_led = Alertlevel[alert].srr_led; |
el15pjt | 3:8782b8b8658b | 189 | } |
el15pjt | 3:8782b8b8658b | 190 | |
el15pjt | 3:8782b8b8658b | 191 | if (Alertlevel[alert].fa_led == HIGH) { |
el15pjt | 3:8782b8b8658b | 192 | if (g_timer_flag) { |
el15pjt | 3:8782b8b8658b | 193 | g_timer_flag = 0; // if it has, clear the flag |
el15pjt | 3:8782b8b8658b | 194 | a_led = !a_led; |
el15pjt | 3:8782b8b8658b | 195 | } |
el15pjt | 3:8782b8b8658b | 196 | } else { |
el15pjt | 3:8782b8b8658b | 197 | a_led = Alertlevel[alert].sa_led; |
el15pjt | 3:8782b8b8658b | 198 | } |
el15pjt | 3:8782b8b8658b | 199 | |
el15pjt | 3:8782b8b8658b | 200 | if (Alertlevel[alert].fgg_led == HIGH) { |
el15pjt | 3:8782b8b8658b | 201 | if (g_timer_flag) { |
el15pjt | 3:8782b8b8658b | 202 | g_timer_flag = 0; // if it has, clear the flag |
el15pjt | 3:8782b8b8658b | 203 | gg_led = !gg_led; |
el15pjt | 3:8782b8b8658b | 204 | } |
el15pjt | 3:8782b8b8658b | 205 | } else { |
el15pjt | 3:8782b8b8658b | 206 | gg_led = Alertlevel[alert].sgg_led; |
el15pjt | 3:8782b8b8658b | 207 | } |
el15pjt | 3:8782b8b8658b | 208 | } |
el15pjt | 3:8782b8b8658b | 209 | |
el15pjt | 3:8782b8b8658b | 210 | void backlight () |
el15pjt | 3:8782b8b8658b | 211 | { |
el15pjt | 3:8782b8b8658b | 212 | |
el15pjt | 3:8782b8b8658b | 213 | if (bright == 1.0) { |
el15pjt | 3:8782b8b8658b | 214 | bright = 0; |
el15pjt | 3:8782b8b8658b | 215 | } else { |
el15pjt | 3:8782b8b8658b | 216 | bright += 0.2; |
el15pjt | 3:8782b8b8658b | 217 | } |
el15pjt | 3:8782b8b8658b | 218 | lcd.setBrightness(bright); |
el15pjt | 3:8782b8b8658b | 219 | } |
el15pjt | 3:8782b8b8658b | 220 | |
el15pjt | 3:8782b8b8658b | 221 | void sw2_isr() |
el15pjt | 3:8782b8b8658b | 222 | { |
el15pjt | 3:8782b8b8658b | 223 | g_sw2_flag = 1; // set flag in ISR |
el15pjt | 3:8782b8b8658b | 224 | } |
el15pjt | 3:8782b8b8658b | 225 | |
el15pjt | 3:8782b8b8658b | 226 | /*void sw1_isr() |
el15pjt | 3:8782b8b8658b | 227 | { |
el15pjt | 3:8782b8b8658b | 228 | g_sw1_flag = 1; // set flag in ISR |
el15pjt | 3:8782b8b8658b | 229 | }*/ |
el15pjt | 3:8782b8b8658b | 230 | |
el15pjt | 2:329597081c06 | 231 | void timer_isr() |
el15pjt | 2:329597081c06 | 232 | { |
el15pjt | 2:329597081c06 | 233 | g_timer_flag = 1; // set flag in ISR |
el15pjt | 3:8782b8b8658b | 234 | } |
el15pjt | 3:8782b8b8658b | 235 | |
el15pjt | 3:8782b8b8658b | 236 | void timer_isr_srf02() |
el15pjt | 3:8782b8b8658b | 237 | { |
el15pjt | 3:8782b8b8658b | 238 | g_timer_flag_srf02 = 1; // set flag in ISR |
el15pjt | 4:673930f04866 | 239 | } |
el15pjt | 4:673930f04866 | 240 | |
el15pjt | 4:673930f04866 | 241 | void timer_isr_tone() |
el15pjt | 4:673930f04866 | 242 | { |
el15pjt | 4:673930f04866 | 243 | g_timer_flag_tone = 1; |
el15pjt | 4:673930f04866 | 244 | } |
el15pjt | 4:673930f04866 | 245 | |
el15pjt | 4:673930f04866 | 246 | void setbuzzer() |
el15pjt | 4:673930f04866 | 247 | { |
el15pjt | 4:673930f04866 | 248 | if (g_timer_flag_tone) { |
el15pjt | 4:673930f04866 | 249 | g_timer_flag_tone = 0; |
el15pjt | 4:673930f04866 | 250 | buzzer.period (1/1000); |
el15pjt | 4:673930f04866 | 251 | buzzer=0.5; |
el15pjt | 4:673930f04866 | 252 | buzzoff.attach(&flip,Alertlevel[alert].toneon); |
el15pjt | 4:673930f04866 | 253 | } |
el15pjt | 4:673930f04866 | 254 | } |
el15pjt | 4:673930f04866 | 255 | |
el15pjt | 4:673930f04866 | 256 | void flip() { |
el15pjt | 4:673930f04866 | 257 | buzzer = 0.0; |
el15pjt | 4:673930f04866 | 258 | } |
el15pjt | 4:673930f04866 | 259 |