Slave
Slave.cpp@0:cb801dad3124, 2019-06-04 (annotated)
- Committer:
- williequesada
- Date:
- Tue Jun 04 17:00:29 2019 +0000
- Revision:
- 0:cb801dad3124
pablo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
williequesada | 0:cb801dad3124 | 1 | #include "Slave.h" |
williequesada | 0:cb801dad3124 | 2 | #include "mbed.h" |
williequesada | 0:cb801dad3124 | 3 | #include "stdio.h" |
williequesada | 0:cb801dad3124 | 4 | |
williequesada | 0:cb801dad3124 | 5 | #define NEXT_STEP 0x85 |
williequesada | 0:cb801dad3124 | 6 | #define NEXT_SLEEP 0x86 |
williequesada | 0:cb801dad3124 | 7 | #define NO_DATA 0x87 |
williequesada | 0:cb801dad3124 | 8 | |
williequesada | 0:cb801dad3124 | 9 | |
williequesada | 0:cb801dad3124 | 10 | char Slave_Buffer[255]; |
williequesada | 0:cb801dad3124 | 11 | int Slave_Counter=0; |
williequesada | 0:cb801dad3124 | 12 | |
williequesada | 0:cb801dad3124 | 13 | SLAVE::SLAVE(PinName TX, PinName RX,PinName AWAKE):Uart(TX,RX),_AWAKE(AWAKE) |
williequesada | 0:cb801dad3124 | 14 | { |
williequesada | 0:cb801dad3124 | 15 | _AWAKE=0; |
williequesada | 0:cb801dad3124 | 16 | Uart.attach(this,&SLAVE::UartInterruption); |
williequesada | 0:cb801dad3124 | 17 | } |
williequesada | 0:cb801dad3124 | 18 | |
williequesada | 0:cb801dad3124 | 19 | bool SLAVE::Available() |
williequesada | 0:cb801dad3124 | 20 | { |
williequesada | 0:cb801dad3124 | 21 | if(Slave_Counter>0) { |
williequesada | 0:cb801dad3124 | 22 | return 1; |
williequesada | 0:cb801dad3124 | 23 | } else { |
williequesada | 0:cb801dad3124 | 24 | return 0; |
williequesada | 0:cb801dad3124 | 25 | } |
williequesada | 0:cb801dad3124 | 26 | } |
williequesada | 0:cb801dad3124 | 27 | |
williequesada | 0:cb801dad3124 | 28 | bool SLAVE::Mensaje() |
williequesada | 0:cb801dad3124 | 29 | { |
williequesada | 0:cb801dad3124 | 30 | if(Slave_Counter>4) { |
williequesada | 0:cb801dad3124 | 31 | return 1; |
williequesada | 0:cb801dad3124 | 32 | } else { |
williequesada | 0:cb801dad3124 | 33 | return 0; |
williequesada | 0:cb801dad3124 | 34 | } |
williequesada | 0:cb801dad3124 | 35 | } |
williequesada | 0:cb801dad3124 | 36 | |
williequesada | 0:cb801dad3124 | 37 | |
williequesada | 0:cb801dad3124 | 38 | void SLAVE::Command(uint8_t _Command) |
williequesada | 0:cb801dad3124 | 39 | { |
williequesada | 0:cb801dad3124 | 40 | if(Uart.writeable()) { |
williequesada | 0:cb801dad3124 | 41 | Uart.putc(_Command); |
williequesada | 0:cb801dad3124 | 42 | } else { |
williequesada | 0:cb801dad3124 | 43 | wait_ms(500); |
williequesada | 0:cb801dad3124 | 44 | if(Uart.writeable()) { |
williequesada | 0:cb801dad3124 | 45 | Uart.putc(_Command); |
williequesada | 0:cb801dad3124 | 46 | } |
williequesada | 0:cb801dad3124 | 47 | } |
williequesada | 0:cb801dad3124 | 48 | } |
williequesada | 0:cb801dad3124 | 49 | |
williequesada | 0:cb801dad3124 | 50 | void SLAVE::Send_Hosting(char Parquimetro[],char Municipio[],char Estado[]) |
williequesada | 0:cb801dad3124 | 51 | { |
williequesada | 0:cb801dad3124 | 52 | if(Uart.writeable()) { |
williequesada | 0:cb801dad3124 | 53 | Uart.printf("%s",Municipio); |
williequesada | 0:cb801dad3124 | 54 | Uart.printf("%s",Estado); |
williequesada | 0:cb801dad3124 | 55 | Uart.printf("%s",Parquimetro); |
williequesada | 0:cb801dad3124 | 56 | } else { |
williequesada | 0:cb801dad3124 | 57 | wait_ms(500); |
williequesada | 0:cb801dad3124 | 58 | if(Uart.writeable()) { |
williequesada | 0:cb801dad3124 | 59 | Uart.printf("%s",Municipio); |
williequesada | 0:cb801dad3124 | 60 | Uart.printf("%s",Estado); |
williequesada | 0:cb801dad3124 | 61 | Uart.printf("%s",Parquimetro); |
williequesada | 0:cb801dad3124 | 62 | } |
williequesada | 0:cb801dad3124 | 63 | } |
williequesada | 0:cb801dad3124 | 64 | } |
williequesada | 0:cb801dad3124 | 65 | |
williequesada | 0:cb801dad3124 | 66 | void SLAVE::Send_User(char Parquimetro[],char Municipio[],int Tiempo,char Espacio[],char Track2[]) |
williequesada | 0:cb801dad3124 | 67 | { |
williequesada | 0:cb801dad3124 | 68 | if(Uart.writeable()) { |
williequesada | 0:cb801dad3124 | 69 | Uart.printf("%s",Municipio); |
williequesada | 0:cb801dad3124 | 70 | Uart.printf("%s",Espacio); |
williequesada | 0:cb801dad3124 | 71 | Uart.printf("%s",Parquimetro); |
williequesada | 0:cb801dad3124 | 72 | if(Tiempo<100) { |
williequesada | 0:cb801dad3124 | 73 | Uart.putc('0'); |
williequesada | 0:cb801dad3124 | 74 | Uart.printf("%i",Tiempo); |
williequesada | 0:cb801dad3124 | 75 | } else { |
williequesada | 0:cb801dad3124 | 76 | Uart.printf("%i",Tiempo); |
williequesada | 0:cb801dad3124 | 77 | } |
williequesada | 0:cb801dad3124 | 78 | Uart.printf("%s",Track2); |
williequesada | 0:cb801dad3124 | 79 | } else { |
williequesada | 0:cb801dad3124 | 80 | wait_ms(500); |
williequesada | 0:cb801dad3124 | 81 | if(Uart.writeable()) { |
williequesada | 0:cb801dad3124 | 82 | Uart.printf("%s",Municipio); |
williequesada | 0:cb801dad3124 | 83 | Uart.printf("%s",Espacio); |
williequesada | 0:cb801dad3124 | 84 | Uart.printf("%s",Parquimetro); |
williequesada | 0:cb801dad3124 | 85 | if(Tiempo<100) { |
williequesada | 0:cb801dad3124 | 86 | Uart.putc('0'); |
williequesada | 0:cb801dad3124 | 87 | Uart.printf("%i",Tiempo); |
williequesada | 0:cb801dad3124 | 88 | } else { |
williequesada | 0:cb801dad3124 | 89 | Uart.printf("%i",Tiempo); |
williequesada | 0:cb801dad3124 | 90 | } |
williequesada | 0:cb801dad3124 | 91 | Uart.printf("%s",Track2); |
williequesada | 0:cb801dad3124 | 92 | } |
williequesada | 0:cb801dad3124 | 93 | } |
williequesada | 0:cb801dad3124 | 94 | } |
williequesada | 0:cb801dad3124 | 95 | |
williequesada | 0:cb801dad3124 | 96 | char SLAVE::Recibe() |
williequesada | 0:cb801dad3124 | 97 | { |
williequesada | 0:cb801dad3124 | 98 | if(Slave_Counter>0) { |
williequesada | 0:cb801dad3124 | 99 | Slave_Counter--; |
williequesada | 0:cb801dad3124 | 100 | return Slave_Buffer[Slave_Counter]; |
williequesada | 0:cb801dad3124 | 101 | } else { |
williequesada | 0:cb801dad3124 | 102 | return NO_DATA; |
williequesada | 0:cb801dad3124 | 103 | } |
williequesada | 0:cb801dad3124 | 104 | } |
williequesada | 0:cb801dad3124 | 105 | |
williequesada | 0:cb801dad3124 | 106 | bool SLAVE::Answer() |
williequesada | 0:cb801dad3124 | 107 | { |
williequesada | 0:cb801dad3124 | 108 | if(Slave_Counter>0) { |
williequesada | 0:cb801dad3124 | 109 | Slave_Counter--; |
williequesada | 0:cb801dad3124 | 110 | if(Slave_Buffer[Slave_Counter]==NEXT_STEP) { |
williequesada | 0:cb801dad3124 | 111 | return 1; |
williequesada | 0:cb801dad3124 | 112 | } else { |
williequesada | 0:cb801dad3124 | 113 | return 0; |
williequesada | 0:cb801dad3124 | 114 | } |
williequesada | 0:cb801dad3124 | 115 | } |
williequesada | 0:cb801dad3124 | 116 | return 0; |
williequesada | 0:cb801dad3124 | 117 | } |
williequesada | 0:cb801dad3124 | 118 | |
williequesada | 0:cb801dad3124 | 119 | void SLAVE::Awake() |
williequesada | 0:cb801dad3124 | 120 | { |
williequesada | 0:cb801dad3124 | 121 | _AWAKE=1; |
williequesada | 0:cb801dad3124 | 122 | wait_us(50); |
williequesada | 0:cb801dad3124 | 123 | _AWAKE=0; |
williequesada | 0:cb801dad3124 | 124 | } |
williequesada | 0:cb801dad3124 | 125 | |
williequesada | 0:cb801dad3124 | 126 | void SLAVE::Sleep() |
williequesada | 0:cb801dad3124 | 127 | { |
williequesada | 0:cb801dad3124 | 128 | if(Uart.writeable()) { |
williequesada | 0:cb801dad3124 | 129 | Uart.putc(NEXT_SLEEP); |
williequesada | 0:cb801dad3124 | 130 | } else { |
williequesada | 0:cb801dad3124 | 131 | wait_ms(500); |
williequesada | 0:cb801dad3124 | 132 | if(Uart.writeable()) { |
williequesada | 0:cb801dad3124 | 133 | Uart.putc(NEXT_SLEEP); |
williequesada | 0:cb801dad3124 | 134 | } |
williequesada | 0:cb801dad3124 | 135 | } |
williequesada | 0:cb801dad3124 | 136 | |
williequesada | 0:cb801dad3124 | 137 | } |
williequesada | 0:cb801dad3124 | 138 | |
williequesada | 0:cb801dad3124 | 139 | void SLAVE::UartInterruption() |
williequesada | 0:cb801dad3124 | 140 | { |
williequesada | 0:cb801dad3124 | 141 | if(Uart.readable()) { |
williequesada | 0:cb801dad3124 | 142 | Slave_Buffer[Slave_Counter]=Uart.getc(); |
williequesada | 0:cb801dad3124 | 143 | Slave_Counter++; |
williequesada | 0:cb801dad3124 | 144 | } |
williequesada | 0:cb801dad3124 | 145 | } |