sender backup
Dependencies: mbed
Fork of XBee_write by
main.cpp@1:bd05bf8bbe0a, 2018-04-18 (annotated)
- Committer:
- bmwaleks
- Date:
- Wed Apr 18 13:21:16 2018 +0000
- Revision:
- 1:bd05bf8bbe0a
- Parent:
- 0:74b4b9fc9480
sende backup
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alex89 | 0:74b4b9fc9480 | 1 | #include "main.h" |
alex89 | 0:74b4b9fc9480 | 2 | |
bmwaleks | 1:bd05bf8bbe0a | 3 | //XBee B med bargraph og digitale innganger + potmeter |
alex89 | 0:74b4b9fc9480 | 4 | |
bmwaleks | 1:bd05bf8bbe0a | 5 | Serial xbee(p9, p10); |
bmwaleks | 1:bd05bf8bbe0a | 6 | Serial pc(USBTX, USBRX); |
bmwaleks | 1:bd05bf8bbe0a | 7 | DigitalOut rst(p11); |
alex89 | 0:74b4b9fc9480 | 8 | DigitalOut myled(LED1); |
alex89 | 0:74b4b9fc9480 | 9 | DigitalOut myled2(LED2); |
bmwaleks | 1:bd05bf8bbe0a | 10 | DigitalIn sb(p8); |
bmwaleks | 1:bd05bf8bbe0a | 11 | BusIn bryter[8] = {p12, p13, p14, p15, p16, p17, p18, p19}; |
bmwaleks | 1:bd05bf8bbe0a | 12 | DigitalOut leds[8] = {p21, p22, p23, p24, p25, p26, p27, p28}; |
bmwaleks | 1:bd05bf8bbe0a | 13 | char check_array_a[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
bmwaleks | 1:bd05bf8bbe0a | 14 | char digital_in_array[8] = {0, 0, 0, 0, 0, 0, 0, 0}; |
alex89 | 0:74b4b9fc9480 | 15 | |
bmwaleks | 1:bd05bf8bbe0a | 16 | AnalogIn ain(p20); |
bmwaleks | 1:bd05bf8bbe0a | 17 | char recievedmessage[8]; |
bmwaleks | 1:bd05bf8bbe0a | 18 | |
bmwaleks | 1:bd05bf8bbe0a | 19 | void read_command(); |
bmwaleks | 1:bd05bf8bbe0a | 20 | void reset(); |
bmwaleks | 1:bd05bf8bbe0a | 21 | void check_digital_in(); |
bmwaleks | 1:bd05bf8bbe0a | 22 | void get_array(); |
bmwaleks | 1:bd05bf8bbe0a | 23 | void send_array(char *arr_name); |
bmwaleks | 1:bd05bf8bbe0a | 24 | |
bmwaleks | 1:bd05bf8bbe0a | 25 | int main() |
bmwaleks | 1:bd05bf8bbe0a | 26 | { |
bmwaleks | 1:bd05bf8bbe0a | 27 | reset(); |
bmwaleks | 1:bd05bf8bbe0a | 28 | while(1) { |
bmwaleks | 1:bd05bf8bbe0a | 29 | read_command(); |
bmwaleks | 1:bd05bf8bbe0a | 30 | get_array(); |
bmwaleks | 1:bd05bf8bbe0a | 31 | check_digital_in(); |
bmwaleks | 1:bd05bf8bbe0a | 32 | send_array(digital_in_array); |
bmwaleks | 1:bd05bf8bbe0a | 33 | } |
bmwaleks | 1:bd05bf8bbe0a | 34 | } |
alex89 | 0:74b4b9fc9480 | 35 | |
bmwaleks | 1:bd05bf8bbe0a | 36 | void read_command() |
bmwaleks | 1:bd05bf8bbe0a | 37 | { |
bmwaleks | 1:bd05bf8bbe0a | 38 | if (xbee.readable()) { |
bmwaleks | 1:bd05bf8bbe0a | 39 | //Henter kommando og eventuell info for LEDs |
bmwaleks | 1:bd05bf8bbe0a | 40 | //xbee.gets(recievedmessage, sizeof(recievedmessage)); |
bmwaleks | 1:bd05bf8bbe0a | 41 | //xbee.scanf("%s", recieveptr); |
bmwaleks | 1:bd05bf8bbe0a | 42 | for (uint8_t i = 0; i <= 7; i++) { |
bmwaleks | 1:bd05bf8bbe0a | 43 | if (xbee.readable()) { |
bmwaleks | 1:bd05bf8bbe0a | 44 | //recievedmessage[i] = xbee.getc(); |
bmwaleks | 1:bd05bf8bbe0a | 45 | check_array_a[i] = xbee.getc(); |
bmwaleks | 1:bd05bf8bbe0a | 46 | pc.printf("\n\n %d Sjekk verdi pa bryter nr: %d", i, check_array_a[i]); |
bmwaleks | 1:bd05bf8bbe0a | 47 | |
bmwaleks | 1:bd05bf8bbe0a | 48 | } else { |
bmwaleks | 1:bd05bf8bbe0a | 49 | recievedmessage[i] = NULL; |
bmwaleks | 1:bd05bf8bbe0a | 50 | } |
bmwaleks | 1:bd05bf8bbe0a | 51 | wait_ms(5); |
bmwaleks | 1:bd05bf8bbe0a | 52 | } |
bmwaleks | 1:bd05bf8bbe0a | 53 | |
bmwaleks | 1:bd05bf8bbe0a | 54 | //Tømmer bufferet i tilfelle det dukker opp input som ikke hører hjemme |
bmwaleks | 1:bd05bf8bbe0a | 55 | while (xbee.readable()) { |
bmwaleks | 1:bd05bf8bbe0a | 56 | xbee.getc(); |
bmwaleks | 1:bd05bf8bbe0a | 57 | wait_ms(5); |
alex89 | 0:74b4b9fc9480 | 58 | } |
alex89 | 0:74b4b9fc9480 | 59 | } |
alex89 | 0:74b4b9fc9480 | 60 | } |
bmwaleks | 1:bd05bf8bbe0a | 61 | |
bmwaleks | 1:bd05bf8bbe0a | 62 | void check_digital_in() //sjekker om lokal digital input er satt til 1 og lagrer verdier i array |
bmwaleks | 1:bd05bf8bbe0a | 63 | { |
bmwaleks | 1:bd05bf8bbe0a | 64 | for (int a=0; a<=7; a++) { |
bmwaleks | 1:bd05bf8bbe0a | 65 | if (bryter[a] != 0) { |
bmwaleks | 1:bd05bf8bbe0a | 66 | digital_in_array[a] = 1; |
bmwaleks | 1:bd05bf8bbe0a | 67 | //bryter_nr[a] = a; |
bmwaleks | 1:bd05bf8bbe0a | 68 | //xbee.putc(x); |
bmwaleks | 1:bd05bf8bbe0a | 69 | } else { |
bmwaleks | 1:bd05bf8bbe0a | 70 | digital_in_array[a] = 0; |
bmwaleks | 1:bd05bf8bbe0a | 71 | } |
bmwaleks | 1:bd05bf8bbe0a | 72 | } |
bmwaleks | 1:bd05bf8bbe0a | 73 | } |
bmwaleks | 1:bd05bf8bbe0a | 74 | |
bmwaleks | 1:bd05bf8bbe0a | 75 | void reset() |
bmwaleks | 1:bd05bf8bbe0a | 76 | { |
bmwaleks | 1:bd05bf8bbe0a | 77 | // reset the xbees (at least 200ns) |
bmwaleks | 1:bd05bf8bbe0a | 78 | rst = 0; |
bmwaleks | 1:bd05bf8bbe0a | 79 | wait_ms(1); |
bmwaleks | 1:bd05bf8bbe0a | 80 | rst = 1; |
bmwaleks | 1:bd05bf8bbe0a | 81 | wait_ms(1); |
bmwaleks | 1:bd05bf8bbe0a | 82 | } |
bmwaleks | 1:bd05bf8bbe0a | 83 | |
bmwaleks | 1:bd05bf8bbe0a | 84 | void get_array() //Leser array sendt fra xbee og lagrer dette lokalt |
bmwaleks | 1:bd05bf8bbe0a | 85 | { |
bmwaleks | 1:bd05bf8bbe0a | 86 | for (int b=0; b<=7; b++) { |
bmwaleks | 1:bd05bf8bbe0a | 87 | //if (check_array_a[b] != 0) { |
bmwaleks | 1:bd05bf8bbe0a | 88 | if ((recievedmessage[b] == b) && (b !=0)) { |
bmwaleks | 1:bd05bf8bbe0a | 89 | check_array_a[b] = 1; |
bmwaleks | 1:bd05bf8bbe0a | 90 | } |
bmwaleks | 1:bd05bf8bbe0a | 91 | pc.printf("\n Lest fra array, nr: %d er verdi: %d", b, check_array_a[b]); |
bmwaleks | 1:bd05bf8bbe0a | 92 | wait(1.5); |
bmwaleks | 1:bd05bf8bbe0a | 93 | //} |
bmwaleks | 1:bd05bf8bbe0a | 94 | } |
bmwaleks | 1:bd05bf8bbe0a | 95 | } |
bmwaleks | 1:bd05bf8bbe0a | 96 | |
bmwaleks | 1:bd05bf8bbe0a | 97 | void send_array(char *arr_name) //sender over hele arrayet |
bmwaleks | 1:bd05bf8bbe0a | 98 | { |
bmwaleks | 1:bd05bf8bbe0a | 99 | for (int c=0; c<=7; c++) { |
bmwaleks | 1:bd05bf8bbe0a | 100 | //if (check_array[c] !=0) { |
bmwaleks | 1:bd05bf8bbe0a | 101 | //xbee.printf("%c", c); |
bmwaleks | 1:bd05bf8bbe0a | 102 | //xbee.printf("%c", check_array[c]); |
bmwaleks | 1:bd05bf8bbe0a | 103 | xbee.putc(arr_name[c]); |
bmwaleks | 1:bd05bf8bbe0a | 104 | } |
bmwaleks | 1:bd05bf8bbe0a | 105 | pc.printf("\n array sendt \n"); |
bmwaleks | 1:bd05bf8bbe0a | 106 | } |