Rhys Wilson-Barford
/
a5_routerDevice
Device 2 for assessment 5. This is the router device
main.cpp@0:287adf2a2eb6, 2016-01-25 (annotated)
- Committer:
- riw2
- Date:
- Mon Jan 25 20:53:07 2016 +0000
- Revision:
- 0:287adf2a2eb6
Device 2 for assessment 5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
riw2 | 0:287adf2a2eb6 | 1 | #include "mbed.h" |
riw2 | 0:287adf2a2eb6 | 2 | #include "C12832.h" |
riw2 | 0:287adf2a2eb6 | 3 | |
riw2 | 0:287adf2a2eb6 | 4 | Serial xbee(D1, D0); /* xbee link to device 1 */ |
riw2 | 0:287adf2a2eb6 | 5 | Serial host(USBTX, USBRX); /* Serial link to PC */ |
riw2 | 0:287adf2a2eb6 | 6 | C12832 lcd(D11, D13, D12, D7, D10); /* Shield LCD */ |
riw2 | 0:287adf2a2eb6 | 7 | DigitalOut green_led_shield (PTA2); /**/ |
riw2 | 0:287adf2a2eb6 | 8 | DigitalOut red_led_shield (PTC4); /* Shield LEDs */ |
riw2 | 0:287adf2a2eb6 | 9 | DigitalOut blue_led_shield (PTA0); /**/ |
riw2 | 0:287adf2a2eb6 | 10 | PwmOut speaker(D6); /* Shield Speaker */ |
riw2 | 0:287adf2a2eb6 | 11 | |
riw2 | 0:287adf2a2eb6 | 12 | /* Makes the LED red */ |
riw2 | 0:287adf2a2eb6 | 13 | void makeLEDRed (void) |
riw2 | 0:287adf2a2eb6 | 14 | { |
riw2 | 0:287adf2a2eb6 | 15 | green_led_shield = 0; |
riw2 | 0:287adf2a2eb6 | 16 | red_led_shield = 255; |
riw2 | 0:287adf2a2eb6 | 17 | blue_led_shield = 0; |
riw2 | 0:287adf2a2eb6 | 18 | } |
riw2 | 0:287adf2a2eb6 | 19 | |
riw2 | 0:287adf2a2eb6 | 20 | /* Makes the LED green */ |
riw2 | 0:287adf2a2eb6 | 21 | void makeLEDGreen (void) |
riw2 | 0:287adf2a2eb6 | 22 | { |
riw2 | 0:287adf2a2eb6 | 23 | green_led_shield = 255; |
riw2 | 0:287adf2a2eb6 | 24 | red_led_shield = 0; |
riw2 | 0:287adf2a2eb6 | 25 | blue_led_shield = 0; |
riw2 | 0:287adf2a2eb6 | 26 | } |
riw2 | 0:287adf2a2eb6 | 27 | |
riw2 | 0:287adf2a2eb6 | 28 | /* Makes the LED yellow */ |
riw2 | 0:287adf2a2eb6 | 29 | void makeLEDYellow (void) |
riw2 | 0:287adf2a2eb6 | 30 | { |
riw2 | 0:287adf2a2eb6 | 31 | green_led_shield = 0; |
riw2 | 0:287adf2a2eb6 | 32 | red_led_shield = 0; |
riw2 | 0:287adf2a2eb6 | 33 | blue_led_shield = 255; |
riw2 | 0:287adf2a2eb6 | 34 | } |
riw2 | 0:287adf2a2eb6 | 35 | |
riw2 | 0:287adf2a2eb6 | 36 | /* Sets device to normal state */ |
riw2 | 0:287adf2a2eb6 | 37 | void setNormalState (void) |
riw2 | 0:287adf2a2eb6 | 38 | { |
riw2 | 0:287adf2a2eb6 | 39 | makeLEDGreen(); |
riw2 | 0:287adf2a2eb6 | 40 | lcd.cls(); |
riw2 | 0:287adf2a2eb6 | 41 | lcd.locate (1,1); //initial normal state. |
riw2 | 0:287adf2a2eb6 | 42 | lcd.printf ("All Is Well With The World."); |
riw2 | 0:287adf2a2eb6 | 43 | } |
riw2 | 0:287adf2a2eb6 | 44 | |
riw2 | 0:287adf2a2eb6 | 45 | /** |
riw2 | 0:287adf2a2eb6 | 46 | * This program accepts data from device 1 and sends it to the PC program. |
riw2 | 0:287adf2a2eb6 | 47 | * It then recieves data from the PC program acting as the router between device 1 and PC. |
riw2 | 0:287adf2a2eb6 | 48 | * At certain status' the LCD and LED display different indicators. |
riw2 | 0:287adf2a2eb6 | 49 | **/ |
riw2 | 0:287adf2a2eb6 | 50 | int main() |
riw2 | 0:287adf2a2eb6 | 51 | { |
riw2 | 0:287adf2a2eb6 | 52 | host.baud(38400); |
riw2 | 0:287adf2a2eb6 | 53 | char input; //characters sent from device 1. |
riw2 | 0:287adf2a2eb6 | 54 | char pcin; //characters sent from PC. |
riw2 | 0:287adf2a2eb6 | 55 | |
riw2 | 0:287adf2a2eb6 | 56 | setNormalState(); |
riw2 | 0:287adf2a2eb6 | 57 | |
riw2 | 0:287adf2a2eb6 | 58 | while(1) |
riw2 | 0:287adf2a2eb6 | 59 | { |
riw2 | 0:287adf2a2eb6 | 60 | if(xbee.readable()) //if recieiving data from device 1. |
riw2 | 0:287adf2a2eb6 | 61 | { |
riw2 | 0:287adf2a2eb6 | 62 | for(int i=0; i<8; i++) { //send host data for 8 char's. 4 for temp, 3 for degree and 1 for alert status. |
riw2 | 0:287adf2a2eb6 | 63 | input = xbee.getc(); |
riw2 | 0:287adf2a2eb6 | 64 | host.printf("%c", input); |
riw2 | 0:287adf2a2eb6 | 65 | } |
riw2 | 0:287adf2a2eb6 | 66 | |
riw2 | 0:287adf2a2eb6 | 67 | switch(input) //using the last input to determin if alert or warning even happened from device 1. |
riw2 | 0:287adf2a2eb6 | 68 | { |
riw2 | 0:287adf2a2eb6 | 69 | case 'A': //if received an a for alert (motion detected in the door). |
riw2 | 0:287adf2a2eb6 | 70 | makeLEDRed(); |
riw2 | 0:287adf2a2eb6 | 71 | lcd.cls(); |
riw2 | 0:287adf2a2eb6 | 72 | lcd.locate (1,1); |
riw2 | 0:287adf2a2eb6 | 73 | lcd.printf ("ALERT!"); |
riw2 | 0:287adf2a2eb6 | 74 | lcd.locate (1,11); |
riw2 | 0:287adf2a2eb6 | 75 | lcd.printf ("Motion Detected!"); |
riw2 | 0:287adf2a2eb6 | 76 | break; |
riw2 | 0:287adf2a2eb6 | 77 | |
riw2 | 0:287adf2a2eb6 | 78 | case 'W': //if received a warning about the temperature. |
riw2 | 0:287adf2a2eb6 | 79 | makeLEDRed(); |
riw2 | 0:287adf2a2eb6 | 80 | lcd.cls(); |
riw2 | 0:287adf2a2eb6 | 81 | lcd.locate (1,1); |
riw2 | 0:287adf2a2eb6 | 82 | lcd.printf ("WARNING!"); |
riw2 | 0:287adf2a2eb6 | 83 | lcd.locate (1,11); |
riw2 | 0:287adf2a2eb6 | 84 | lcd.printf ("Abnormal Temperature!"); |
riw2 | 0:287adf2a2eb6 | 85 | break; |
riw2 | 0:287adf2a2eb6 | 86 | |
riw2 | 0:287adf2a2eb6 | 87 | case '0': //no alert handle case |
riw2 | 0:287adf2a2eb6 | 88 | break; |
riw2 | 0:287adf2a2eb6 | 89 | } |
riw2 | 0:287adf2a2eb6 | 90 | |
riw2 | 0:287adf2a2eb6 | 91 | if(host.readable()) //receiving data from the PC program (GUI). |
riw2 | 0:287adf2a2eb6 | 92 | { |
riw2 | 0:287adf2a2eb6 | 93 | pcin = host.getc(); |
riw2 | 0:287adf2a2eb6 | 94 | switch(pcin) //using the PC program input to change status of device 1 and 2. |
riw2 | 0:287adf2a2eb6 | 95 | { |
riw2 | 0:287adf2a2eb6 | 96 | case'y': //set status to normal as indicated by PC. |
riw2 | 0:287adf2a2eb6 | 97 | setNormalState(); |
riw2 | 0:287adf2a2eb6 | 98 | break; |
riw2 | 0:287adf2a2eb6 | 99 | |
riw2 | 0:287adf2a2eb6 | 100 | case's': //sets status as investigating as indicated by the PC. |
riw2 | 0:287adf2a2eb6 | 101 | makeLEDYellow(); |
riw2 | 0:287adf2a2eb6 | 102 | lcd.cls(); |
riw2 | 0:287adf2a2eb6 | 103 | lcd.locate (1,1); |
riw2 | 0:287adf2a2eb6 | 104 | lcd.printf ("Issue Is Being Investigated."); |
riw2 | 0:287adf2a2eb6 | 105 | break; |
riw2 | 0:287adf2a2eb6 | 106 | } |
riw2 | 0:287adf2a2eb6 | 107 | xbee.putc(pcin); //send data from PC to device 1. |
riw2 | 0:287adf2a2eb6 | 108 | } |
riw2 | 0:287adf2a2eb6 | 109 | } |
riw2 | 0:287adf2a2eb6 | 110 | } |
riw2 | 0:287adf2a2eb6 | 111 | } |