Level 2 Project Range Device

Dependencies:   N5110 SDFileSystem SRF02 TMP102 mbed

Committer:
el15pjt
Date:
Sun Apr 10 15:29:57 2016 +0000
Revision:
3:8782b8b8658b
Parent:
2:329597081c06
Child:
4:673930f04866
Main reduced and functions introduced.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:0b180348c7e4 19
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 2:329597081c06 29
el15pjt 0:0b180348c7e4 30 // K64F on-board LEDs
el15pjt 0:0b180348c7e4 31 DigitalOut r_led(LED_RED);
el15pjt 0:0b180348c7e4 32 DigitalOut g_led(LED_GREEN);
el15pjt 0:0b180348c7e4 33 DigitalOut b_led(LED_BLUE);
el15pjt 0:0b180348c7e4 34
el15pjt 0:0b180348c7e4 35 // K64F on-board switches
el15pjt 3:8782b8b8658b 36 //InterruptIn sw1(SW2);
el15pjt 3:8782b8b8658b 37 InterruptIn sw2(SW3);
el15pjt 0:0b180348c7e4 38
el15pjt 2:329597081c06 39 volatile int g_timer_flag = 0;
el15pjt 3:8782b8b8658b 40 volatile int g_timer_flag_srf02 = 0;
el15pjt 3:8782b8b8658b 41 volatile int g_sw2_flag = 0;
el15pjt 3:8782b8b8658b 42 //volatile int g_sw1_flag = 0;
el15pjt 2:329597081c06 43
el15pjt 3:8782b8b8658b 44 int alert;
el15pjt 0:0b180348c7e4 45 int distance;
el15pjt 3:8782b8b8658b 46 float bright = 1;
el15pjt 0:0b180348c7e4 47
el15pjt 2:329597081c06 48 // struct for state
el15pjt 2:329597081c06 49 struct Alertlevel {
el15pjt 2:329597081c06 50 char srr_led; // output value
el15pjt 2:329597081c06 51 char sa_led; // time in state
el15pjt 2:329597081c06 52 char sgg_led; // array of next states
el15pjt 2:329597081c06 53 char frr_led;
el15pjt 2:329597081c06 54 char fa_led;
el15pjt 2:329597081c06 55 char fgg_led;
el15pjt 2:329597081c06 56 };
el15pjt 2:329597081c06 57 typedef const struct Alertlevel STyp;
el15pjt 2:329597081c06 58
el15pjt 2:329597081c06 59 STyp Alertlevel[8] = {
el15pjt 2:329597081c06 60 {LOW,LOW,LOW,LOW,LOW,LOW}, // no output
el15pjt 2:329597081c06 61 {LOW,LOW,LOW,LOW,LOW,HIGH}, //flash green
el15pjt 2:329597081c06 62 {LOW,LOW,HIGH,LOW,LOW,LOW}, //steady green
el15pjt 2:329597081c06 63 {LOW,LOW,HIGH,LOW,HIGH,LOW}, //flash amber
el15pjt 2:329597081c06 64 {LOW,HIGH,HIGH,LOW,LOW,LOW}, //steady amber
el15pjt 2:329597081c06 65 {LOW,HIGH,HIGH,HIGH,LOW,LOW}, //flash red
el15pjt 2:329597081c06 66 {HIGH,HIGH,HIGH,LOW,LOW,LOW},// steady red
el15pjt 2:329597081c06 67 {LOW,LOW,LOW,HIGH,HIGH,HIGH} // all flash
el15pjt 2:329597081c06 68 };
el15pjt 3:8782b8b8658b 69 void lcdoutput();
el15pjt 3:8782b8b8658b 70 void timer_isr(); // timed interuprt
el15pjt 3:8782b8b8658b 71 void timer_isr_srf02();
el15pjt 3:8782b8b8658b 72 void backlight();
el15pjt 3:8782b8b8658b 73 void init_K64F(); // set up board
el15pjt 3:8782b8b8658b 74 void sw2_isr();
el15pjt 3:8782b8b8658b 75 //void sw1_isr();
el15pjt 3:8782b8b8658b 76 void setalert();
el15pjt 3:8782b8b8658b 77 void setleds();
el15pjt 0:0b180348c7e4 78
el15pjt 0:0b180348c7e4 79 int main()
el15pjt 0:0b180348c7e4 80 {
el15pjt 0:0b180348c7e4 81 lcd.init();
el15pjt 0:0b180348c7e4 82 init_K64F();
el15pjt 3:8782b8b8658b 83 ticker.attach(&timer_isr,0.35);
el15pjt 3:8782b8b8658b 84 ticker_srf02.attach(&timer_isr_srf02,0.02);
el15pjt 3:8782b8b8658b 85 // sw1.fall(&sw1_isr);
el15pjt 3:8782b8b8658b 86 sw2.fall(&sw2_isr);
el15pjt 0:0b180348c7e4 87 while(1) {
el15pjt 3:8782b8b8658b 88 if (g_sw2_flag) {
el15pjt 3:8782b8b8658b 89 g_sw2_flag = 0; // if it has, clear the flag
el15pjt 3:8782b8b8658b 90 backlight();
el15pjt 2:329597081c06 91 }
el15pjt 3:8782b8b8658b 92 if (g_timer_flag_srf02) {
el15pjt 3:8782b8b8658b 93 g_timer_flag_srf02 = 0; // if it has, clear the flag
el15pjt 3:8782b8b8658b 94 int distance = srf02.getDistanceCm();
el15pjt 0:0b180348c7e4 95 }
el15pjt 3:8782b8b8658b 96 lcdoutput();
el15pjt 3:8782b8b8658b 97 setalert();
el15pjt 3:8782b8b8658b 98 setleds();
el15pjt 0:0b180348c7e4 99 }
el15pjt 0:0b180348c7e4 100 }
el15pjt 0:0b180348c7e4 101
el15pjt 0:0b180348c7e4 102 //Set up board switches and LEDS
el15pjt 2:329597081c06 103 void init_K64F()
el15pjt 2:329597081c06 104 {
el15pjt 0:0b180348c7e4 105 //on-board LEDs are active-low, so set pin high to turn them off.
el15pjt 2:329597081c06 106 r_led = 1;
el15pjt 2:329597081c06 107 g_led = 1;
el15pjt 2:329597081c06 108 b_led = 1;
el15pjt 0:0b180348c7e4 109
el15pjt 0:0b180348c7e4 110 // since the on-board switches have external pull-ups, we should disable the internal pull-down
el15pjt 0:0b180348c7e4 111 // resistors that are enabled by default using InterruptIn
el15pjt 2:329597081c06 112 sw2.mode(PullNone);
el15pjt 3:8782b8b8658b 113 // sw1.mode(PullNone);
el15pjt 3:8782b8b8658b 114 }
el15pjt 3:8782b8b8658b 115 void lcdoutput()
el15pjt 3:8782b8b8658b 116 {
el15pjt 3:8782b8b8658b 117 int length;
el15pjt 3:8782b8b8658b 118 int length1;
el15pjt 3:8782b8b8658b 119 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
el15pjt 3:8782b8b8658b 120 char buffer1[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
el15pjt 3:8782b8b8658b 121
el15pjt 3:8782b8b8658b 122
el15pjt 3:8782b8b8658b 123 length = sprintf(buffer,"D = %i Cm",distance);
el15pjt 3:8782b8b8658b 124 length1 = sprintf(buffer1,"Bright = %f Cm",bright);
el15pjt 3:8782b8b8658b 125
el15pjt 3:8782b8b8658b 126 if (length <= 14)
el15pjt 3:8782b8b8658b 127 lcd.printString(buffer,0,0);
el15pjt 3:8782b8b8658b 128 lcd.printString(buffer1,0,1);
el15pjt 3:8782b8b8658b 129
el15pjt 3:8782b8b8658b 130 // draw a line across the display at y = 40 pixels (origin top-left)
el15pjt 3:8782b8b8658b 131 for (int i = 0; i < WIDTH; i++) {
el15pjt 3:8782b8b8658b 132 lcd.setPixel(i,45);
el15pjt 3:8782b8b8658b 133 }
el15pjt 3:8782b8b8658b 134
el15pjt 3:8782b8b8658b 135 lcd.refresh(); // need to refresh display after setting pixels
el15pjt 3:8782b8b8658b 136 }
el15pjt 3:8782b8b8658b 137
el15pjt 3:8782b8b8658b 138 void setalert()
el15pjt 3:8782b8b8658b 139 {
el15pjt 3:8782b8b8658b 140 if (distance >= 200 && distance < 250) {
el15pjt 3:8782b8b8658b 141 alert = 1; //flash green
el15pjt 3:8782b8b8658b 142 } else if (distance >= 150 && distance < 200) {
el15pjt 3:8782b8b8658b 143 alert = 2; //steady green
el15pjt 3:8782b8b8658b 144 } else if (distance >= 100 && distance < 150) {
el15pjt 3:8782b8b8658b 145 alert = 3; //flashing amber
el15pjt 3:8782b8b8658b 146 } else if (distance >= 50 && distance < 100) {
el15pjt 3:8782b8b8658b 147 alert = 4; //steady amber
el15pjt 3:8782b8b8658b 148 } else if ( distance > 10 && distance < 50) {
el15pjt 3:8782b8b8658b 149 alert = 5; //flashing red
el15pjt 3:8782b8b8658b 150 } else if (distance > 1 && distance <= 10) {
el15pjt 3:8782b8b8658b 151 alert = 6; //steady red
el15pjt 3:8782b8b8658b 152 } else if (distance <=1) {
el15pjt 3:8782b8b8658b 153 alert = 7; //all flashing
el15pjt 3:8782b8b8658b 154 } else {
el15pjt 3:8782b8b8658b 155 alert = 0; //no output
el15pjt 3:8782b8b8658b 156 }
el15pjt 2:329597081c06 157 }
el15pjt 2:329597081c06 158
el15pjt 3:8782b8b8658b 159 void setleds()
el15pjt 3:8782b8b8658b 160 {
el15pjt 3:8782b8b8658b 161 // If statments to determin the output of each LED
el15pjt 3:8782b8b8658b 162
el15pjt 3:8782b8b8658b 163 if (Alertlevel[alert].frr_led == HIGH) {
el15pjt 3:8782b8b8658b 164 if (g_timer_flag) {
el15pjt 3:8782b8b8658b 165 g_timer_flag = 0; // if it has, clear the flag
el15pjt 3:8782b8b8658b 166 rr_led = !rr_led;
el15pjt 3:8782b8b8658b 167 }
el15pjt 3:8782b8b8658b 168 } else {
el15pjt 3:8782b8b8658b 169 rr_led = Alertlevel[alert].srr_led;
el15pjt 3:8782b8b8658b 170 }
el15pjt 3:8782b8b8658b 171
el15pjt 3:8782b8b8658b 172 if (Alertlevel[alert].fa_led == HIGH) {
el15pjt 3:8782b8b8658b 173 if (g_timer_flag) {
el15pjt 3:8782b8b8658b 174 g_timer_flag = 0; // if it has, clear the flag
el15pjt 3:8782b8b8658b 175 a_led = !a_led;
el15pjt 3:8782b8b8658b 176 }
el15pjt 3:8782b8b8658b 177 } else {
el15pjt 3:8782b8b8658b 178 a_led = Alertlevel[alert].sa_led;
el15pjt 3:8782b8b8658b 179 }
el15pjt 3:8782b8b8658b 180
el15pjt 3:8782b8b8658b 181 if (Alertlevel[alert].fgg_led == HIGH) {
el15pjt 3:8782b8b8658b 182 if (g_timer_flag) {
el15pjt 3:8782b8b8658b 183 g_timer_flag = 0; // if it has, clear the flag
el15pjt 3:8782b8b8658b 184 gg_led = !gg_led;
el15pjt 3:8782b8b8658b 185 }
el15pjt 3:8782b8b8658b 186 } else {
el15pjt 3:8782b8b8658b 187 gg_led = Alertlevel[alert].sgg_led;
el15pjt 3:8782b8b8658b 188 }
el15pjt 3:8782b8b8658b 189 }
el15pjt 3:8782b8b8658b 190
el15pjt 3:8782b8b8658b 191 void backlight ()
el15pjt 3:8782b8b8658b 192 {
el15pjt 3:8782b8b8658b 193
el15pjt 3:8782b8b8658b 194 if (bright == 1.0) {
el15pjt 3:8782b8b8658b 195 bright = 0;
el15pjt 3:8782b8b8658b 196 } else {
el15pjt 3:8782b8b8658b 197 bright += 0.2;
el15pjt 3:8782b8b8658b 198 }
el15pjt 3:8782b8b8658b 199 lcd.setBrightness(bright);
el15pjt 3:8782b8b8658b 200 }
el15pjt 3:8782b8b8658b 201
el15pjt 3:8782b8b8658b 202 void sw2_isr()
el15pjt 3:8782b8b8658b 203 {
el15pjt 3:8782b8b8658b 204 g_sw2_flag = 1; // set flag in ISR
el15pjt 3:8782b8b8658b 205 }
el15pjt 3:8782b8b8658b 206
el15pjt 3:8782b8b8658b 207 /*void sw1_isr()
el15pjt 3:8782b8b8658b 208 {
el15pjt 3:8782b8b8658b 209 g_sw1_flag = 1; // set flag in ISR
el15pjt 3:8782b8b8658b 210 }*/
el15pjt 3:8782b8b8658b 211
el15pjt 2:329597081c06 212
el15pjt 2:329597081c06 213 void timer_isr()
el15pjt 2:329597081c06 214 {
el15pjt 2:329597081c06 215 g_timer_flag = 1; // set flag in ISR
el15pjt 3:8782b8b8658b 216 }
el15pjt 3:8782b8b8658b 217
el15pjt 3:8782b8b8658b 218 void timer_isr_srf02()
el15pjt 3:8782b8b8658b 219 {
el15pjt 3:8782b8b8658b 220 g_timer_flag_srf02 = 1; // set flag in ISR
el15pjt 2:329597081c06 221 }