Level 2 Project Range Device
Dependencies: N5110 SDFileSystem SRF02 TMP102 mbed
Fork of Ranger by
main.cpp@2:329597081c06, 2016-04-01 (annotated)
- Committer:
- el15pjt
- Date:
- Fri Apr 01 23:09:07 2016 +0000
- Revision:
- 2:329597081c06
- Parent:
- 1:5b991c2302e1
- Child:
- 3:8782b8b8658b
Added flashing to struct array and and interrupt for the flashing
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 | 1:5b991c2302e1 | 6 | |
el15pjt | 0:0b180348c7e4 | 7 | */ |
el15pjt | 0:0b180348c7e4 | 8 | |
el15pjt | 0:0b180348c7e4 | 9 | #include "mbed.h" |
el15pjt | 0:0b180348c7e4 | 10 | #include "SRF02.h" |
el15pjt | 0:0b180348c7e4 | 11 | #include "N5110.h" |
el15pjt | 0:0b180348c7e4 | 12 | |
el15pjt | 2:329597081c06 | 13 | #define LOW 0 |
el15pjt | 2:329597081c06 | 14 | #define HIGH 1 |
el15pjt | 2:329597081c06 | 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 | 0:0b180348c7e4 | 19 | |
el15pjt | 0:0b180348c7e4 | 20 | // Ranger object |
el15pjt | 0:0b180348c7e4 | 21 | SRF02 srf02(I2C_SDA,I2C_SCL); |
el15pjt | 0:0b180348c7e4 | 22 | |
el15pjt | 0:0b180348c7e4 | 23 | //N5110 Object VCC, SCE,RST, D/C, MOSI,SCLK, LED |
el15pjt | 0:0b180348c7e4 | 24 | N5110 lcd(PTE26,PTA0,PTC4,PTD0,PTD2,PTD1,PTC3); |
el15pjt | 0:0b180348c7e4 | 25 | |
el15pjt | 2:329597081c06 | 26 | Ticker ticker; |
el15pjt | 2:329597081c06 | 27 | |
el15pjt | 0:0b180348c7e4 | 28 | // K64F on-board LEDs |
el15pjt | 0:0b180348c7e4 | 29 | DigitalOut r_led(LED_RED); |
el15pjt | 0:0b180348c7e4 | 30 | DigitalOut g_led(LED_GREEN); |
el15pjt | 0:0b180348c7e4 | 31 | DigitalOut b_led(LED_BLUE); |
el15pjt | 0:0b180348c7e4 | 32 | |
el15pjt | 0:0b180348c7e4 | 33 | // K64F on-board switches |
el15pjt | 0:0b180348c7e4 | 34 | InterruptIn sw2(SW2); |
el15pjt | 0:0b180348c7e4 | 35 | InterruptIn sw3(SW3); |
el15pjt | 0:0b180348c7e4 | 36 | |
el15pjt | 2:329597081c06 | 37 | volatile int g_timer_flag = 0; |
el15pjt | 2:329597081c06 | 38 | |
el15pjt | 0:0b180348c7e4 | 39 | int distance; |
el15pjt | 0:0b180348c7e4 | 40 | int length; |
el15pjt | 2:329597081c06 | 41 | int alert; |
el15pjt | 2:329597081c06 | 42 | |
el15pjt | 2:329597081c06 | 43 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
el15pjt | 0:0b180348c7e4 | 44 | |
el15pjt | 2:329597081c06 | 45 | // struct for state |
el15pjt | 2:329597081c06 | 46 | struct Alertlevel { |
el15pjt | 2:329597081c06 | 47 | char srr_led; // output value |
el15pjt | 2:329597081c06 | 48 | char sa_led; // time in state |
el15pjt | 2:329597081c06 | 49 | char sgg_led; // array of next states |
el15pjt | 2:329597081c06 | 50 | char frr_led; |
el15pjt | 2:329597081c06 | 51 | char fa_led; |
el15pjt | 2:329597081c06 | 52 | char fgg_led; |
el15pjt | 2:329597081c06 | 53 | }; |
el15pjt | 2:329597081c06 | 54 | typedef const struct Alertlevel STyp; |
el15pjt | 2:329597081c06 | 55 | |
el15pjt | 2:329597081c06 | 56 | STyp Alertlevel[8] = { |
el15pjt | 2:329597081c06 | 57 | {LOW,LOW,LOW,LOW,LOW,LOW}, // no output |
el15pjt | 2:329597081c06 | 58 | {LOW,LOW,LOW,LOW,LOW,HIGH}, //flash green |
el15pjt | 2:329597081c06 | 59 | {LOW,LOW,HIGH,LOW,LOW,LOW}, //steady green |
el15pjt | 2:329597081c06 | 60 | {LOW,LOW,HIGH,LOW,HIGH,LOW}, //flash amber |
el15pjt | 2:329597081c06 | 61 | {LOW,HIGH,HIGH,LOW,LOW,LOW}, //steady amber |
el15pjt | 2:329597081c06 | 62 | {LOW,HIGH,HIGH,HIGH,LOW,LOW}, //flash red |
el15pjt | 2:329597081c06 | 63 | {HIGH,HIGH,HIGH,LOW,LOW,LOW},// steady red |
el15pjt | 2:329597081c06 | 64 | {LOW,LOW,LOW,HIGH,HIGH,HIGH} // all flash |
el15pjt | 2:329597081c06 | 65 | }; |
el15pjt | 2:329597081c06 | 66 | // timed interuprt |
el15pjt | 2:329597081c06 | 67 | void timer_isr(); |
el15pjt | 1:5b991c2302e1 | 68 | |
el15pjt | 0:0b180348c7e4 | 69 | // set up board |
el15pjt | 0:0b180348c7e4 | 70 | void init_K64F(); |
el15pjt | 0:0b180348c7e4 | 71 | |
el15pjt | 0:0b180348c7e4 | 72 | int main() |
el15pjt | 0:0b180348c7e4 | 73 | { |
el15pjt | 0:0b180348c7e4 | 74 | lcd.init(); |
el15pjt | 0:0b180348c7e4 | 75 | init_K64F(); |
el15pjt | 2:329597081c06 | 76 | ticker.attach(&timer_isr,0.05); |
el15pjt | 1:5b991c2302e1 | 77 | |
el15pjt | 0:0b180348c7e4 | 78 | while(1) { |
el15pjt | 0:0b180348c7e4 | 79 | |
el15pjt | 2:329597081c06 | 80 | // read sensor distance in cm and print over serial port |
el15pjt | 0:0b180348c7e4 | 81 | int distance = srf02.getDistanceCm(); |
el15pjt | 2:329597081c06 | 82 | length = sprintf(buffer,"D = %i Cm",distance); |
el15pjt | 2:329597081c06 | 83 | if (length <= 14) |
el15pjt | 2:329597081c06 | 84 | lcd.printString(buffer,0,2); |
el15pjt | 2:329597081c06 | 85 | // need to refresh display after setting pixels |
el15pjt | 2:329597081c06 | 86 | lcd.refresh(); |
el15pjt | 1:5b991c2302e1 | 87 | |
el15pjt | 2:329597081c06 | 88 | // short delay before next measurement |
el15pjt | 0:0b180348c7e4 | 89 | wait(0.25); |
el15pjt | 0:0b180348c7e4 | 90 | |
el15pjt | 2:329597081c06 | 91 | //Range Alert selection use to adjust alert boundarys |
el15pjt | 2:329597081c06 | 92 | if (distance >= 200 && distance < 250) { |
el15pjt | 2:329597081c06 | 93 | alert = 1; //flash green |
el15pjt | 2:329597081c06 | 94 | } else if (distance >= 150 && distance < 200) { |
el15pjt | 2:329597081c06 | 95 | alert = 2; //steady green |
el15pjt | 2:329597081c06 | 96 | } else if (distance >= 100 && distance < 150) { |
el15pjt | 2:329597081c06 | 97 | alert = 3; //flashing amber |
el15pjt | 2:329597081c06 | 98 | } else if (distance >= 50 && distance < 100) { |
el15pjt | 2:329597081c06 | 99 | alert = 4; //steady amber |
el15pjt | 2:329597081c06 | 100 | } else if ( distance > 10 && distance < 50) { |
el15pjt | 2:329597081c06 | 101 | alert = 5; //flashing red |
el15pjt | 2:329597081c06 | 102 | } else if (distance > 1 && distance <= 10) { |
el15pjt | 2:329597081c06 | 103 | alert = 6; //steady red |
el15pjt | 2:329597081c06 | 104 | } else if (distance <=1) { |
el15pjt | 2:329597081c06 | 105 | alert = 7; //all flashing |
el15pjt | 2:329597081c06 | 106 | } else { |
el15pjt | 2:329597081c06 | 107 | alert = 0; //no output |
el15pjt | 2:329597081c06 | 108 | } |
el15pjt | 2:329597081c06 | 109 | // Steady light outputs |
el15pjt | 2:329597081c06 | 110 | rr_led = Alertlevel[alert].srr_led; |
el15pjt | 2:329597081c06 | 111 | a_led = Alertlevel[alert].sa_led; |
el15pjt | 2:329597081c06 | 112 | gg_led = Alertlevel[alert].sgg_led; |
el15pjt | 0:0b180348c7e4 | 113 | |
el15pjt | 2:329597081c06 | 114 | //Flashing light outputs |
el15pjt | 2:329597081c06 | 115 | if (g_timer_flag) { |
el15pjt | 2:329597081c06 | 116 | g_timer_flag = 0; // if it has, clear the flag |
el15pjt | 2:329597081c06 | 117 | |
el15pjt | 2:329597081c06 | 118 | if ( Alertlevel[alert].frr_led == HIGH) { |
el15pjt | 2:329597081c06 | 119 | rr_led = !rr_led; |
el15pjt | 2:329597081c06 | 120 | } else if ( Alertlevel[alert].fa_led == HIGH) { |
el15pjt | 2:329597081c06 | 121 | a_led = !a_led; |
el15pjt | 2:329597081c06 | 122 | } else if ( Alertlevel[alert].fgg_led == HIGH) { |
el15pjt | 2:329597081c06 | 123 | gg_led = !gg_led; |
el15pjt | 2:329597081c06 | 124 | } |
el15pjt | 0:0b180348c7e4 | 125 | } |
el15pjt | 0:0b180348c7e4 | 126 | } |
el15pjt | 1:5b991c2302e1 | 127 | |
el15pjt | 0:0b180348c7e4 | 128 | } |
el15pjt | 0:0b180348c7e4 | 129 | |
el15pjt | 0:0b180348c7e4 | 130 | //Set up board switches and LEDS |
el15pjt | 2:329597081c06 | 131 | void init_K64F() |
el15pjt | 2:329597081c06 | 132 | { |
el15pjt | 0:0b180348c7e4 | 133 | //on-board LEDs are active-low, so set pin high to turn them off. |
el15pjt | 2:329597081c06 | 134 | r_led = 1; |
el15pjt | 2:329597081c06 | 135 | g_led = 1; |
el15pjt | 2:329597081c06 | 136 | b_led = 1; |
el15pjt | 0:0b180348c7e4 | 137 | |
el15pjt | 0:0b180348c7e4 | 138 | // since the on-board switches have external pull-ups, we should disable the internal pull-down |
el15pjt | 0:0b180348c7e4 | 139 | // resistors that are enabled by default using InterruptIn |
el15pjt | 2:329597081c06 | 140 | sw2.mode(PullNone); |
el15pjt | 2:329597081c06 | 141 | sw3.mode(PullNone); |
el15pjt | 2:329597081c06 | 142 | } |
el15pjt | 2:329597081c06 | 143 | |
el15pjt | 2:329597081c06 | 144 | |
el15pjt | 2:329597081c06 | 145 | void timer_isr() |
el15pjt | 2:329597081c06 | 146 | { |
el15pjt | 2:329597081c06 | 147 | g_timer_flag = 1; // set flag in ISR |
el15pjt | 2:329597081c06 | 148 | } |