Motta backup
Dependencies: C12832_lcd mbed xbee_lib
Fork of Xbee_Hello_world_A by
main.cpp@1:3036ca0c5e12, 2018-04-11 (annotated)
- Committer:
- bmwaleks
- Date:
- Wed Apr 11 14:35:41 2018 +0000
- Revision:
- 1:3036ca0c5e12
- Parent:
- 0:9cbddcc86466
Test1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tristanjph | 0:9cbddcc86466 | 1 | #include "mbed.h" |
bmwaleks | 1:3036ca0c5e12 | 2 | #include "xbee.h" // Include for xbee code |
bmwaleks | 1:3036ca0c5e12 | 3 | #include "C12832_lcd.h" // Include for LCD code |
bmwaleks | 1:3036ca0c5e12 | 4 | |
bmwaleks | 1:3036ca0c5e12 | 5 | char sjekk_XBeeB(char nr); |
bmwaleks | 1:3036ca0c5e12 | 6 | void Menu(); |
bmwaleks | 1:3036ca0c5e12 | 7 | void set_array(); |
bmwaleks | 1:3036ca0c5e12 | 8 | void send_array(); |
bmwaleks | 1:3036ca0c5e12 | 9 | void reset(); |
bmwaleks | 1:3036ca0c5e12 | 10 | void get_array(); |
bmwaleks | 1:3036ca0c5e12 | 11 | char print_array(char *arr_name); |
bmwaleks | 1:3036ca0c5e12 | 12 | |
bmwaleks | 1:3036ca0c5e12 | 13 | // ################################ |
bmwaleks | 1:3036ca0c5e12 | 14 | // ################################ |
bmwaleks | 1:3036ca0c5e12 | 15 | // ### XBee A (med LCD skjerm) ### |
bmwaleks | 1:3036ca0c5e12 | 16 | // ################################ |
bmwaleks | 1:3036ca0c5e12 | 17 | // ################################ |
tristanjph | 0:9cbddcc86466 | 18 | |
bmwaleks | 1:3036ca0c5e12 | 19 | //xbee xbee1(p9,p10,p11); //Initalise xbee_lib varName(rx,tx,reset) |
bmwaleks | 1:3036ca0c5e12 | 20 | |
bmwaleks | 1:3036ca0c5e12 | 21 | Serial xbee(p9, p10); |
bmwaleks | 1:3036ca0c5e12 | 22 | DigitalOut rst(p11); |
bmwaleks | 1:3036ca0c5e12 | 23 | |
bmwaleks | 1:3036ca0c5e12 | 24 | BusIn bryter[8] = {p12, p13, p14, p15, p16, p17, p18, p19}; |
bmwaleks | 1:3036ca0c5e12 | 25 | // Brytere 1 - 8 på XBee A. |
bmwaleks | 1:3036ca0c5e12 | 26 | char check_array[10] = {0, 0, 0, 0, 0, 0, 0, 0}; |
bmwaleks | 1:3036ca0c5e12 | 27 | char digital_in_array_b[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
bmwaleks | 1:3036ca0c5e12 | 28 | |
bmwaleks | 1:3036ca0c5e12 | 29 | DigitalOut b1(p21), b2(p22), b3(p23), b4(p24); |
tristanjph | 0:9cbddcc86466 | 30 | Serial pc(USBTX, USBRX); //Initalise PC serial comms |
bmwaleks | 1:3036ca0c5e12 | 31 | C12832_LCD lcd; //Initialize LCD Screen |
tristanjph | 0:9cbddcc86466 | 32 | |
bmwaleks | 1:3036ca0c5e12 | 33 | char recieveData; |
bmwaleks | 1:3036ca0c5e12 | 34 | |
bmwaleks | 1:3036ca0c5e12 | 35 | //Code to send strings acsross xbee with xbee.h |
bmwaleks | 1:3036ca0c5e12 | 36 | //Code should be on the End Device |
tristanjph | 0:9cbddcc86466 | 37 | int main() |
tristanjph | 0:9cbddcc86466 | 38 | { |
bmwaleks | 1:3036ca0c5e12 | 39 | Menu(); |
bmwaleks | 1:3036ca0c5e12 | 40 | |
bmwaleks | 1:3036ca0c5e12 | 41 | lcd.printf("Starting oppg2_vol1"); |
bmwaleks | 1:3036ca0c5e12 | 42 | wait(2); |
bmwaleks | 1:3036ca0c5e12 | 43 | reset(); |
bmwaleks | 1:3036ca0c5e12 | 44 | |
bmwaleks | 1:3036ca0c5e12 | 45 | //Establish a variable to receive data from End Device |
bmwaleks | 1:3036ca0c5e12 | 46 | //Max buffer is 202 |
bmwaleks | 1:3036ca0c5e12 | 47 | // char receiveData[4]; |
bmwaleks | 1:3036ca0c5e12 | 48 | |
bmwaleks | 1:3036ca0c5e12 | 49 | //Setup LCD screen |
bmwaleks | 1:3036ca0c5e12 | 50 | lcd.cls(); |
bmwaleks | 1:3036ca0c5e12 | 51 | lcd.locate(0,1); |
bmwaleks | 1:3036ca0c5e12 | 52 | |
bmwaleks | 1:3036ca0c5e12 | 53 | |
bmwaleks | 1:3036ca0c5e12 | 54 | /*for (int b=0; b<=7; b++) { |
bmwaleks | 1:3036ca0c5e12 | 55 | if (check_array[b] != 0) { |
bmwaleks | 1:3036ca0c5e12 | 56 | pc.printf("\n Sjekker punkt nr: %d ", b); |
bmwaleks | 1:3036ca0c5e12 | 57 | } |
bmwaleks | 1:3036ca0c5e12 | 58 | }*/ |
bmwaleks | 1:3036ca0c5e12 | 59 | } |
bmwaleks | 1:3036ca0c5e12 | 60 | |
bmwaleks | 1:3036ca0c5e12 | 61 | /*char sjekk_XBeeB(int nr) |
bmwaleks | 1:3036ca0c5e12 | 62 | { |
bmwaleks | 1:3036ca0c5e12 | 63 | |
bmwaleks | 1:3036ca0c5e12 | 64 | pc.printf("yoo"); |
bmwaleks | 1:3036ca0c5e12 | 65 | char kommando; |
bmwaleks | 1:3036ca0c5e12 | 66 | //kommando[0]=0; //0xxx (xxx = nr) |
bmwaleks | 1:3036ca0c5e12 | 67 | |
bmwaleks | 1:3036ca0c5e12 | 68 | //char *strcat(char *dest, const char *src) |
bmwaleks | 1:3036ca0c5e12 | 69 | char *strcat(char *kommando, const char *nr); |
bmwaleks | 1:3036ca0c5e12 | 70 | |
bmwaleks | 1:3036ca0c5e12 | 71 | //str.append(kommando); |
bmwaleks | 1:3036ca0c5e12 | 72 | //str.append(nr); |
bmwaleks | 1:3036ca0c5e12 | 73 | |
bmwaleks | 1:3036ca0c5e12 | 74 | |
bmwaleks | 1:3036ca0c5e12 | 75 | xbee.printf("yoo"); |
bmwaleks | 1:3036ca0c5e12 | 76 | return 1;*/ |
bmwaleks | 1:3036ca0c5e12 | 77 | //} |
tristanjph | 0:9cbddcc86466 | 78 | |
bmwaleks | 1:3036ca0c5e12 | 79 | |
bmwaleks | 1:3036ca0c5e12 | 80 | //void Menu(int innlogget) //Meny funksjonen (krever input av passord variabel. Blir satt til 1 ved rett passord) |
bmwaleks | 1:3036ca0c5e12 | 81 | void Menu() |
bmwaleks | 1:3036ca0c5e12 | 82 | { |
bmwaleks | 1:3036ca0c5e12 | 83 | //while (innlogget == 1) { //Ved riktig passord (logget inn) |
bmwaleks | 1:3036ca0c5e12 | 84 | pc.printf(" \r \n\n Trykk for onsket valg: \n 1. Sjekke status Digitale innganger \n 2. Sjekke status LEDs \n 3. Stille LEDs \n 4. Sjekke status analog inngang"); |
bmwaleks | 1:3036ca0c5e12 | 85 | switch(getchar()) { |
bmwaleks | 1:3036ca0c5e12 | 86 | case '1': //Sjekke status Digitale innganger |
bmwaleks | 1:3036ca0c5e12 | 87 | pc.printf("\r\n\n Sjekke status paa digitale innganger \n\n"); |
bmwaleks | 1:3036ca0c5e12 | 88 | set_array(); // Denne sjekker brytere på Xbee A som bestemmer hvilke punkter som skal kontrolleres på XBee B |
bmwaleks | 1:3036ca0c5e12 | 89 | print_array(check_array); |
bmwaleks | 1:3036ca0c5e12 | 90 | wait(2); |
bmwaleks | 1:3036ca0c5e12 | 91 | send_array(); |
bmwaleks | 1:3036ca0c5e12 | 92 | wait(6); |
bmwaleks | 1:3036ca0c5e12 | 93 | get_array(); |
bmwaleks | 1:3036ca0c5e12 | 94 | wait(2); |
bmwaleks | 1:3036ca0c5e12 | 95 | print_array(digital_in_array_b); |
bmwaleks | 1:3036ca0c5e12 | 96 | //reset(); |
bmwaleks | 1:3036ca0c5e12 | 97 | wait(1); |
bmwaleks | 1:3036ca0c5e12 | 98 | |
bmwaleks | 1:3036ca0c5e12 | 99 | break; |
bmwaleks | 1:3036ca0c5e12 | 100 | |
bmwaleks | 1:3036ca0c5e12 | 101 | case '2': //Sjekke status LEDs |
bmwaleks | 1:3036ca0c5e12 | 102 | pc.printf("\r\n\n Sjekke staus paa LEDs \n\n"); |
bmwaleks | 1:3036ca0c5e12 | 103 | |
bmwaleks | 1:3036ca0c5e12 | 104 | break; |
bmwaleks | 1:3036ca0c5e12 | 105 | |
bmwaleks | 1:3036ca0c5e12 | 106 | case '3': //Stille LEDs |
bmwaleks | 1:3036ca0c5e12 | 107 | pc.printf("\r\n\n Stille LEDs \n\n"); |
bmwaleks | 1:3036ca0c5e12 | 108 | |
bmwaleks | 1:3036ca0c5e12 | 109 | break; |
bmwaleks | 1:3036ca0c5e12 | 110 | case '4': //Sjekke status Analog inngang |
bmwaleks | 1:3036ca0c5e12 | 111 | pc.printf(" \n Sjekke staus paa Analog inngang \n\n"); |
bmwaleks | 1:3036ca0c5e12 | 112 | |
bmwaleks | 1:3036ca0c5e12 | 113 | break; |
bmwaleks | 1:3036ca0c5e12 | 114 | |
bmwaleks | 1:3036ca0c5e12 | 115 | |
bmwaleks | 1:3036ca0c5e12 | 116 | default: |
bmwaleks | 1:3036ca0c5e12 | 117 | pc.printf(" \r \n Ugyldig valg!"); |
tristanjph | 0:9cbddcc86466 | 118 | } |
tristanjph | 0:9cbddcc86466 | 119 | } |
bmwaleks | 1:3036ca0c5e12 | 120 | |
bmwaleks | 1:3036ca0c5e12 | 121 | void set_array() |
bmwaleks | 1:3036ca0c5e12 | 122 | { |
bmwaleks | 1:3036ca0c5e12 | 123 | for (int i=0; i<=7; i++) { |
bmwaleks | 1:3036ca0c5e12 | 124 | if (bryter[i] != 0) { |
bmwaleks | 1:3036ca0c5e12 | 125 | //xbee.printf("%c", i); |
bmwaleks | 1:3036ca0c5e12 | 126 | //pc.printf("\nsjekk verdi pa bryter nr: %d ", i); |
bmwaleks | 1:3036ca0c5e12 | 127 | //Setter bryter/led nr til array) |
bmwaleks | 1:3036ca0c5e12 | 128 | check_array[i] = 1; |
bmwaleks | 1:3036ca0c5e12 | 129 | |
bmwaleks | 1:3036ca0c5e12 | 130 | //char status = sjekk_XBeeB(i); |
bmwaleks | 1:3036ca0c5e12 | 131 | //pc.printf("Status for bryter %d er %d", i, status); |
bmwaleks | 1:3036ca0c5e12 | 132 | } |
bmwaleks | 1:3036ca0c5e12 | 133 | } |
bmwaleks | 1:3036ca0c5e12 | 134 | } |
bmwaleks | 1:3036ca0c5e12 | 135 | |
bmwaleks | 1:3036ca0c5e12 | 136 | void send_array() //sender over hele arrayet |
bmwaleks | 1:3036ca0c5e12 | 137 | { |
bmwaleks | 1:3036ca0c5e12 | 138 | for (int c=0; c<=7; c++) { |
bmwaleks | 1:3036ca0c5e12 | 139 | //if (check_array[c] !=0) { |
bmwaleks | 1:3036ca0c5e12 | 140 | //xbee.printf("%c", c); |
bmwaleks | 1:3036ca0c5e12 | 141 | //xbee.printf("%c", check_array[c]); |
bmwaleks | 1:3036ca0c5e12 | 142 | xbee.putc(check_array[c]); |
bmwaleks | 1:3036ca0c5e12 | 143 | //} |
bmwaleks | 1:3036ca0c5e12 | 144 | } |
bmwaleks | 1:3036ca0c5e12 | 145 | pc.printf("\n array sendt \n"); |
bmwaleks | 1:3036ca0c5e12 | 146 | } |
bmwaleks | 1:3036ca0c5e12 | 147 | |
bmwaleks | 1:3036ca0c5e12 | 148 | void get_array() //Leser array sendt fra xbee og lagrer dette lokalt |
bmwaleks | 1:3036ca0c5e12 | 149 | { |
bmwaleks | 1:3036ca0c5e12 | 150 | if (xbee.readable()) { |
bmwaleks | 1:3036ca0c5e12 | 151 | for (uint8_t i = 0; i <= 7; i++) { |
bmwaleks | 1:3036ca0c5e12 | 152 | if (xbee.readable()) { |
bmwaleks | 1:3036ca0c5e12 | 153 | digital_in_array_b[i] = xbee.getc(); |
bmwaleks | 1:3036ca0c5e12 | 154 | //pc.printf("\n\n %d Sjekk verdi pa bryter nr: %d", i, digital_in_array_b[i]); |
bmwaleks | 1:3036ca0c5e12 | 155 | |
bmwaleks | 1:3036ca0c5e12 | 156 | } else { |
bmwaleks | 1:3036ca0c5e12 | 157 | digital_in_array_b[i] = NULL; |
bmwaleks | 1:3036ca0c5e12 | 158 | pc.printf("\n feil under mottak\n"); |
bmwaleks | 1:3036ca0c5e12 | 159 | } |
bmwaleks | 1:3036ca0c5e12 | 160 | wait_ms(5); |
bmwaleks | 1:3036ca0c5e12 | 161 | } |
bmwaleks | 1:3036ca0c5e12 | 162 | |
bmwaleks | 1:3036ca0c5e12 | 163 | //Tømmer bufferet i tilfelle det dukker opp input som ikke hører hjemme |
bmwaleks | 1:3036ca0c5e12 | 164 | while (xbee.readable()) { |
bmwaleks | 1:3036ca0c5e12 | 165 | xbee.getc(); |
bmwaleks | 1:3036ca0c5e12 | 166 | wait_ms(5); |
bmwaleks | 1:3036ca0c5e12 | 167 | } |
bmwaleks | 1:3036ca0c5e12 | 168 | } |
bmwaleks | 1:3036ca0c5e12 | 169 | } |
bmwaleks | 1:3036ca0c5e12 | 170 | |
bmwaleks | 1:3036ca0c5e12 | 171 | void reset() |
bmwaleks | 1:3036ca0c5e12 | 172 | { |
bmwaleks | 1:3036ca0c5e12 | 173 | // reset the xbees (at least 200ns) |
bmwaleks | 1:3036ca0c5e12 | 174 | rst = 0; |
bmwaleks | 1:3036ca0c5e12 | 175 | wait_ms(1); |
bmwaleks | 1:3036ca0c5e12 | 176 | rst = 1; |
bmwaleks | 1:3036ca0c5e12 | 177 | wait_ms(1); |
bmwaleks | 1:3036ca0c5e12 | 178 | } |
bmwaleks | 1:3036ca0c5e12 | 179 | |
bmwaleks | 1:3036ca0c5e12 | 180 | char print_array(char *arr_name) { |
bmwaleks | 1:3036ca0c5e12 | 181 | pc.printf("\n"); |
bmwaleks | 1:3036ca0c5e12 | 182 | for (int d=0; d<=7; d++) { |
bmwaleks | 1:3036ca0c5e12 | 183 | //pc.printf("\n Skriver ut array %d", *arr_name); |
bmwaleks | 1:3036ca0c5e12 | 184 | pc.printf("\n index_nr: %d , verdi: %d \n", d, arr_name[d]); |
bmwaleks | 1:3036ca0c5e12 | 185 | } |
bmwaleks | 1:3036ca0c5e12 | 186 | } |
bmwaleks | 1:3036ca0c5e12 | 187 | |
bmwaleks | 1:3036ca0c5e12 | 188 | |
bmwaleks | 1:3036ca0c5e12 | 189 | /*for (int b=0; b<=7; b++) |
bmwaleks | 1:3036ca0c5e12 | 190 | { |
bmwaleks | 1:3036ca0c5e12 | 191 | //if (check_array_a[b] != 0) { |
bmwaleks | 1:3036ca0c5e12 | 192 | if ((recievedmessage[b] == b) && (b !=0)) { |
bmwaleks | 1:3036ca0c5e12 | 193 | check_array_a[b] = 1; |
bmwaleks | 1:3036ca0c5e12 | 194 | } |
bmwaleks | 1:3036ca0c5e12 | 195 | pc.printf("\n Lest fra array, nr: %d er verdi: %d", b, check_array_a[b]); |
bmwaleks | 1:3036ca0c5e12 | 196 | wait(1.5); |
bmwaleks | 1:3036ca0c5e12 | 197 | //} |
bmwaleks | 1:3036ca0c5e12 | 198 | } |
bmwaleks | 1:3036ca0c5e12 | 199 | }*/ |