VERSAO FINAL
Dependencies: 4DGL-uLCD-SE C12832 LM75B MMA7660 mbed-rtos mbed
Fork of main by
main.cpp@1:39b4c6a60595, 2016-05-20 (annotated)
- Committer:
- andrelopes31
- Date:
- Fri May 20 17:34:35 2016 +0000
- Revision:
- 1:39b4c6a60595
- Parent:
- 0:7677c6736b9f
FINAL
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kvothe345 | 0:7677c6736b9f | 1 | #include "mbed.h" |
kvothe345 | 0:7677c6736b9f | 2 | #include "C12832.h" |
kvothe345 | 0:7677c6736b9f | 3 | #include "LM75B.h" |
andrelopes31 | 1:39b4c6a60595 | 4 | #include "MMA7660.h" |
andrelopes31 | 1:39b4c6a60595 | 5 | #include "rtos.h" |
andrelopes31 | 1:39b4c6a60595 | 6 | #include "PowerControl/PowerControl.h" |
andrelopes31 | 1:39b4c6a60595 | 7 | #include "PowerControl/EthernetPowerControl.h" |
kvothe345 | 0:7677c6736b9f | 8 | |
kvothe345 | 0:7677c6736b9f | 9 | //------------------------------------------------------------------------// |
kvothe345 | 0:7677c6736b9f | 10 | // Hardware Variables // |
kvothe345 | 0:7677c6736b9f | 11 | //------------------------------------------------------------------------// |
kvothe345 | 0:7677c6736b9f | 12 | Serial rn41 (p9,p10); |
kvothe345 | 0:7677c6736b9f | 13 | C12832 lcd (p5, p7, p6, p8, p11); |
andrelopes31 | 1:39b4c6a60595 | 14 | BusIn up (p15); |
andrelopes31 | 1:39b4c6a60595 | 15 | BusIn down (p12); |
andrelopes31 | 1:39b4c6a60595 | 16 | BusIn left (p13); |
andrelopes31 | 1:39b4c6a60595 | 17 | BusIn right (p16); |
andrelopes31 | 1:39b4c6a60595 | 18 | BusIn center (p14); |
kvothe345 | 0:7677c6736b9f | 19 | DigitalOut led1 (LED1); |
kvothe345 | 0:7677c6736b9f | 20 | DigitalOut led2 (LED2); |
kvothe345 | 0:7677c6736b9f | 21 | DigitalOut led3 (LED3); |
kvothe345 | 0:7677c6736b9f | 22 | DigitalOut led4 (LED4); |
andrelopes31 | 1:39b4c6a60595 | 23 | LM75B sensor (p28,p27); |
andrelopes31 | 1:39b4c6a60595 | 24 | MMA7660 MMA (p28,p27); |
andrelopes31 | 1:39b4c6a60595 | 25 | AnalogIn pot1 (p19); |
andrelopes31 | 1:39b4c6a60595 | 26 | AnalogIn pot2 (p20); |
andrelopes31 | 1:39b4c6a60595 | 27 | PwmOut spkr (p26); |
andrelopes31 | 1:39b4c6a60595 | 28 | Serial tty_usb (USBTX, USBRX); |
andrelopes31 | 1:39b4c6a60595 | 29 | PwmOut r (p23); |
andrelopes31 | 1:39b4c6a60595 | 30 | PwmOut g (p24); |
andrelopes31 | 1:39b4c6a60595 | 31 | PwmOut b (p25); |
kvothe345 | 0:7677c6736b9f | 32 | |
andrelopes31 | 1:39b4c6a60595 | 33 | Ticker t; |
andrelopes31 | 1:39b4c6a60595 | 34 | char tty_buffer[100]; |
andrelopes31 | 1:39b4c6a60595 | 35 | int buffer_ptr; |
andrelopes31 | 1:39b4c6a60595 | 36 | int speaker_on, period_spkr; |
andrelopes31 | 1:39b4c6a60595 | 37 | float spkr_volume; |
andrelopes31 | 1:39b4c6a60595 | 38 | int user_is_back; |
kvothe345 | 0:7677c6736b9f | 39 | //------------------------------------------------------------------------// |
kvothe345 | 0:7677c6736b9f | 40 | // Function Headears // |
kvothe345 | 0:7677c6736b9f | 41 | //------------------------------------------------------------------------// |
andrelopes31 | 1:39b4c6a60595 | 42 | void receive_command (); |
andrelopes31 | 1:39b4c6a60595 | 43 | float read_temp (); |
kvothe345 | 0:7677c6736b9f | 44 | void bluetoothReceive (); |
kvothe345 | 0:7677c6736b9f | 45 | void setupBluetooth (); |
andrelopes31 | 1:39b4c6a60595 | 46 | void userpcinterface_thread (void const *args); |
andrelopes31 | 1:39b4c6a60595 | 47 | void setupBluetooth (void const *args); |
andrelopes31 | 1:39b4c6a60595 | 48 | void fsleep (); |
kvothe345 | 0:7677c6736b9f | 49 | |
kvothe345 | 0:7677c6736b9f | 50 | |
kvothe345 | 0:7677c6736b9f | 51 | char buff[100]; |
kvothe345 | 0:7677c6736b9f | 52 | |
kvothe345 | 0:7677c6736b9f | 53 | //------------------------------------------------------------------------// |
kvothe345 | 0:7677c6736b9f | 54 | // Functions // |
kvothe345 | 0:7677c6736b9f | 55 | //------------------------------------------------------------------------// |
kvothe345 | 0:7677c6736b9f | 56 | |
andrelopes31 | 1:39b4c6a60595 | 57 | |
andrelopes31 | 1:39b4c6a60595 | 58 | void receive_command() |
andrelopes31 | 1:39b4c6a60595 | 59 | { |
andrelopes31 | 1:39b4c6a60595 | 60 | buffer_ptr = 0; |
andrelopes31 | 1:39b4c6a60595 | 61 | char ch; |
andrelopes31 | 1:39b4c6a60595 | 62 | memset(tty_buffer, '\0', sizeof(tty_buffer)); |
andrelopes31 | 1:39b4c6a60595 | 63 | tty_usb.putc('>'); |
andrelopes31 | 1:39b4c6a60595 | 64 | while (1) { |
andrelopes31 | 1:39b4c6a60595 | 65 | if (tty_usb.readable()) { |
andrelopes31 | 1:39b4c6a60595 | 66 | ch = tty_usb.getc(); |
andrelopes31 | 1:39b4c6a60595 | 67 | tty_usb.putc(ch); |
andrelopes31 | 1:39b4c6a60595 | 68 | |
andrelopes31 | 1:39b4c6a60595 | 69 | tty_buffer[strlen(tty_buffer)] = ch; |
andrelopes31 | 1:39b4c6a60595 | 70 | buffer_ptr++; |
andrelopes31 | 1:39b4c6a60595 | 71 | if ((ch == '\n') || (ch == '\r')) { |
andrelopes31 | 1:39b4c6a60595 | 72 | tty_usb.putc('\n'); |
andrelopes31 | 1:39b4c6a60595 | 73 | tty_usb.putc('\r'); |
andrelopes31 | 1:39b4c6a60595 | 74 | break; |
andrelopes31 | 1:39b4c6a60595 | 75 | } |
andrelopes31 | 1:39b4c6a60595 | 76 | if (ch == 0x7f) { |
andrelopes31 | 1:39b4c6a60595 | 77 | buffer_ptr -= 2; |
andrelopes31 | 1:39b4c6a60595 | 78 | tty_usb.putc(0x8); |
andrelopes31 | 1:39b4c6a60595 | 79 | tty_usb.putc(' '); |
andrelopes31 | 1:39b4c6a60595 | 80 | tty_usb.putc(0x8); |
andrelopes31 | 1:39b4c6a60595 | 81 | } |
andrelopes31 | 1:39b4c6a60595 | 82 | } |
andrelopes31 | 1:39b4c6a60595 | 83 | } |
andrelopes31 | 1:39b4c6a60595 | 84 | } |
andrelopes31 | 1:39b4c6a60595 | 85 | |
andrelopes31 | 1:39b4c6a60595 | 86 | |
andrelopes31 | 1:39b4c6a60595 | 87 | |
kvothe345 | 0:7677c6736b9f | 88 | float read_temp() |
kvothe345 | 0:7677c6736b9f | 89 | { |
kvothe345 | 0:7677c6736b9f | 90 | if (sensor.open()) { |
andrelopes31 | 1:39b4c6a60595 | 91 | return sensor; |
kvothe345 | 0:7677c6736b9f | 92 | } else { |
kvothe345 | 0:7677c6736b9f | 93 | error("Device not detected!\n"); |
kvothe345 | 0:7677c6736b9f | 94 | return -1; |
kvothe345 | 0:7677c6736b9f | 95 | } |
andrelopes31 | 1:39b4c6a60595 | 96 | } |
kvothe345 | 0:7677c6736b9f | 97 | |
kvothe345 | 0:7677c6736b9f | 98 | |
kvothe345 | 0:7677c6736b9f | 99 | |
kvothe345 | 0:7677c6736b9f | 100 | |
kvothe345 | 0:7677c6736b9f | 101 | void bluetoothReceive () |
kvothe345 | 0:7677c6736b9f | 102 | { |
kvothe345 | 0:7677c6736b9f | 103 | char caracter; |
andrelopes31 | 1:39b4c6a60595 | 104 | char msg[10]; |
andrelopes31 | 1:39b4c6a60595 | 105 | int flag_clear=0; |
andrelopes31 | 1:39b4c6a60595 | 106 | float t, pot; |
andrelopes31 | 1:39b4c6a60595 | 107 | if (rn41.readable()) { |
kvothe345 | 0:7677c6736b9f | 108 | caracter = rn41.getc(); |
kvothe345 | 0:7677c6736b9f | 109 | buff[strlen(buff)]= caracter; |
andrelopes31 | 1:39b4c6a60595 | 110 | } |
andrelopes31 | 1:39b4c6a60595 | 111 | |
andrelopes31 | 1:39b4c6a60595 | 112 | |
andrelopes31 | 1:39b4c6a60595 | 113 | if(strcmp(buff, "temp") == 0) { |
andrelopes31 | 1:39b4c6a60595 | 114 | flag_clear = 1; |
andrelopes31 | 1:39b4c6a60595 | 115 | t = read_temp(); |
andrelopes31 | 1:39b4c6a60595 | 116 | sprintf(msg, "Temperatura %f", t); |
andrelopes31 | 1:39b4c6a60595 | 117 | rn41.printf(msg); |
andrelopes31 | 1:39b4c6a60595 | 118 | |
andrelopes31 | 1:39b4c6a60595 | 119 | } else if(strcmp(buff, "hum") == 0) { |
andrelopes31 | 1:39b4c6a60595 | 120 | pot = pot1; |
andrelopes31 | 1:39b4c6a60595 | 121 | sprintf(msg, "Humidade %f", pot); |
andrelopes31 | 1:39b4c6a60595 | 122 | rn41.printf(msg); |
andrelopes31 | 1:39b4c6a60595 | 123 | flag_clear = 1; |
andrelopes31 | 1:39b4c6a60595 | 124 | } else if(strcmp(buff, "lum") == 0) { |
andrelopes31 | 1:39b4c6a60595 | 125 | pot = pot2; |
andrelopes31 | 1:39b4c6a60595 | 126 | sprintf(msg, "Luminosidade %f", pot); |
andrelopes31 | 1:39b4c6a60595 | 127 | rn41.printf(msg); |
andrelopes31 | 1:39b4c6a60595 | 128 | flag_clear = 1; |
andrelopes31 | 1:39b4c6a60595 | 129 | } else if(strcmp(buff, "speaker on") == 0) { |
andrelopes31 | 1:39b4c6a60595 | 130 | period_spkr = 6000; |
andrelopes31 | 1:39b4c6a60595 | 131 | spkr.period(1.0/period_spkr); |
andrelopes31 | 1:39b4c6a60595 | 132 | spkr=0.5; |
andrelopes31 | 1:39b4c6a60595 | 133 | spkr_volume=0.5; |
andrelopes31 | 1:39b4c6a60595 | 134 | speaker_on = 1; |
andrelopes31 | 1:39b4c6a60595 | 135 | flag_clear = 1; |
andrelopes31 | 1:39b4c6a60595 | 136 | } else if(strcmp(buff, "speaker off") == 0) { |
andrelopes31 | 1:39b4c6a60595 | 137 | spkr = 0; |
andrelopes31 | 1:39b4c6a60595 | 138 | speaker_on = 0; |
andrelopes31 | 1:39b4c6a60595 | 139 | spkr_volume=0; |
andrelopes31 | 1:39b4c6a60595 | 140 | flag_clear = 1; |
andrelopes31 | 1:39b4c6a60595 | 141 | } |
andrelopes31 | 1:39b4c6a60595 | 142 | if((!rn41.readable() && flag_clear == 1) || strlen(buff)>11) { |
andrelopes31 | 1:39b4c6a60595 | 143 | memset (buff,'\0',sizeof(buff)); |
andrelopes31 | 1:39b4c6a60595 | 144 | flag_clear = 0; |
kvothe345 | 0:7677c6736b9f | 145 | } |
andrelopes31 | 1:39b4c6a60595 | 146 | |
andrelopes31 | 1:39b4c6a60595 | 147 | |
andrelopes31 | 1:39b4c6a60595 | 148 | } |
andrelopes31 | 1:39b4c6a60595 | 149 | |
andrelopes31 | 1:39b4c6a60595 | 150 | void setupBluetooth (void const *args) |
andrelopes31 | 1:39b4c6a60595 | 151 | { |
andrelopes31 | 1:39b4c6a60595 | 152 | while(1) { |
andrelopes31 | 1:39b4c6a60595 | 153 | bluetoothReceive(); |
andrelopes31 | 1:39b4c6a60595 | 154 | } |
andrelopes31 | 1:39b4c6a60595 | 155 | |
andrelopes31 | 1:39b4c6a60595 | 156 | } |
andrelopes31 | 1:39b4c6a60595 | 157 | |
andrelopes31 | 1:39b4c6a60595 | 158 | void userpcinterface_thread (void const *args) |
andrelopes31 | 1:39b4c6a60595 | 159 | { |
andrelopes31 | 1:39b4c6a60595 | 160 | tty_usb.baud(9600); |
andrelopes31 | 1:39b4c6a60595 | 161 | float temper, pot; |
andrelopes31 | 1:39b4c6a60595 | 162 | while(1) { |
andrelopes31 | 1:39b4c6a60595 | 163 | receive_command(); |
andrelopes31 | 1:39b4c6a60595 | 164 | if (strncmp("temp", tty_buffer, buffer_ptr-1) == 0) { |
andrelopes31 | 1:39b4c6a60595 | 165 | temper = read_temp(); |
andrelopes31 | 1:39b4c6a60595 | 166 | tty_usb.printf("#\tValor Temperatura = %f C", temper); |
andrelopes31 | 1:39b4c6a60595 | 167 | } else if(strncmp("pot1", tty_buffer, buffer_ptr-1) == 0) { |
andrelopes31 | 1:39b4c6a60595 | 168 | pot = pot1; |
andrelopes31 | 1:39b4c6a60595 | 169 | tty_usb.printf("#\tValor Potenciometro1 = %f", pot); |
andrelopes31 | 1:39b4c6a60595 | 170 | } else if(strncmp("pot2", tty_buffer, buffer_ptr-1) == 0) { |
andrelopes31 | 1:39b4c6a60595 | 171 | pot = pot2; |
andrelopes31 | 1:39b4c6a60595 | 172 | tty_usb.printf("#\tValor Potenciometro2 = %f", pot); |
andrelopes31 | 1:39b4c6a60595 | 173 | } else if(strncmp("speaker on", tty_buffer, buffer_ptr-1) == 0) { |
andrelopes31 | 1:39b4c6a60595 | 174 | period_spkr = 6000; |
andrelopes31 | 1:39b4c6a60595 | 175 | spkr.period(1.0/period_spkr); |
andrelopes31 | 1:39b4c6a60595 | 176 | spkr=0.5; |
andrelopes31 | 1:39b4c6a60595 | 177 | spkr_volume=0.5; |
andrelopes31 | 1:39b4c6a60595 | 178 | speaker_on = 1; |
andrelopes31 | 1:39b4c6a60595 | 179 | tty_usb.printf("#\tSpeaker is o\n"); |
andrelopes31 | 1:39b4c6a60595 | 180 | }else if(strncmp("speaker off", tty_buffer, buffer_ptr-1) == 0) { |
andrelopes31 | 1:39b4c6a60595 | 181 | spkr = 0; |
andrelopes31 | 1:39b4c6a60595 | 182 | speaker_on = 0; |
andrelopes31 | 1:39b4c6a60595 | 183 | spkr_volume=0; |
andrelopes31 | 1:39b4c6a60595 | 184 | tty_usb.printf("#\tSpeaker volume set to 0\n"); |
andrelopes31 | 1:39b4c6a60595 | 185 | } else tty_usb.printf("#\tUnknown command...try again.\n"); |
andrelopes31 | 1:39b4c6a60595 | 186 | |
kvothe345 | 0:7677c6736b9f | 187 | } |
kvothe345 | 0:7677c6736b9f | 188 | } |
kvothe345 | 0:7677c6736b9f | 189 | |
kvothe345 | 0:7677c6736b9f | 190 | |
kvothe345 | 0:7677c6736b9f | 191 | |
kvothe345 | 0:7677c6736b9f | 192 | |
kvothe345 | 0:7677c6736b9f | 193 | |
kvothe345 | 0:7677c6736b9f | 194 | //------------------------------------------------------------------------// |
kvothe345 | 0:7677c6736b9f | 195 | // Main // |
kvothe345 | 0:7677c6736b9f | 196 | //------------------------------------------------------------------------// |
kvothe345 | 0:7677c6736b9f | 197 | int main() |
kvothe345 | 0:7677c6736b9f | 198 | { |
andrelopes31 | 1:39b4c6a60595 | 199 | float Zaxis_p, Zaxis_n; |
andrelopes31 | 1:39b4c6a60595 | 200 | r.period(0.001); |
kvothe345 | 0:7677c6736b9f | 201 | memset (buff,'\0',sizeof(buff)); |
kvothe345 | 0:7677c6736b9f | 202 | lcd.cls(); |
andrelopes31 | 1:39b4c6a60595 | 203 | lcd.locate(0,3); |
andrelopes31 | 1:39b4c6a60595 | 204 | lcd.printf("SER group 2 board!"); |
andrelopes31 | 1:39b4c6a60595 | 205 | rn41.baud(115200); |
andrelopes31 | 1:39b4c6a60595 | 206 | rn41.format(8,SerialBase::None,1); |
andrelopes31 | 1:39b4c6a60595 | 207 | Thread t1(userpcinterface_thread,0,osPriorityNormal); |
andrelopes31 | 1:39b4c6a60595 | 208 | Thread t2(setupBluetooth,0,osPriorityNormal); |
andrelopes31 | 1:39b4c6a60595 | 209 | while(1) { |
andrelopes31 | 1:39b4c6a60595 | 210 | |
andrelopes31 | 1:39b4c6a60595 | 211 | |
andrelopes31 | 1:39b4c6a60595 | 212 | |
andrelopes31 | 1:39b4c6a60595 | 213 | while (MMA.testConnection()!=1) { |
andrelopes31 | 1:39b4c6a60595 | 214 | } |
kvothe345 | 0:7677c6736b9f | 215 | |
andrelopes31 | 1:39b4c6a60595 | 216 | Zaxis_p = MMA.z(); |
andrelopes31 | 1:39b4c6a60595 | 217 | Zaxis_n = -MMA.z(); |
andrelopes31 | 1:39b4c6a60595 | 218 | /*lcd.cls(); |
andrelopes31 | 1:39b4c6a60595 | 219 | lcd.locate(0,3); |
andrelopes31 | 1:39b4c6a60595 | 220 | sprintf(buf, "Z_p-%f Z_n-%f", Zaxis_p,Zaxis_n); |
andrelopes31 | 1:39b4c6a60595 | 221 | lcd.printf(": %s",buf);*/ |
andrelopes31 | 1:39b4c6a60595 | 222 | wait(0.1); |
andrelopes31 | 1:39b4c6a60595 | 223 | led2 = !led2; |
andrelopes31 | 1:39b4c6a60595 | 224 | if(speaker_on) { |
andrelopes31 | 1:39b4c6a60595 | 225 | if(up) { |
andrelopes31 | 1:39b4c6a60595 | 226 | period_spkr+=100; |
andrelopes31 | 1:39b4c6a60595 | 227 | spkr.period(1.0/period_spkr); |
andrelopes31 | 1:39b4c6a60595 | 228 | wait(0.1); |
andrelopes31 | 1:39b4c6a60595 | 229 | } |
andrelopes31 | 1:39b4c6a60595 | 230 | if(down) { |
andrelopes31 | 1:39b4c6a60595 | 231 | period_spkr-=100; |
andrelopes31 | 1:39b4c6a60595 | 232 | spkr.period(1.0/period_spkr); |
andrelopes31 | 1:39b4c6a60595 | 233 | wait(0.1); |
andrelopes31 | 1:39b4c6a60595 | 234 | } |
andrelopes31 | 1:39b4c6a60595 | 235 | if(left) { |
andrelopes31 | 1:39b4c6a60595 | 236 | if(spkr_volume <0.5) |
andrelopes31 | 1:39b4c6a60595 | 237 | { |
andrelopes31 | 1:39b4c6a60595 | 238 | spkr_volume+=0.05; |
andrelopes31 | 1:39b4c6a60595 | 239 | spkr = spkr_volume; |
andrelopes31 | 1:39b4c6a60595 | 240 | wait(0.1); |
andrelopes31 | 1:39b4c6a60595 | 241 | } |
andrelopes31 | 1:39b4c6a60595 | 242 | } |
andrelopes31 | 1:39b4c6a60595 | 243 | if(right) { |
andrelopes31 | 1:39b4c6a60595 | 244 | if(spkr_volume >0) |
andrelopes31 | 1:39b4c6a60595 | 245 | { |
andrelopes31 | 1:39b4c6a60595 | 246 | spkr_volume -= 0.05; |
andrelopes31 | 1:39b4c6a60595 | 247 | spkr = spkr_volume; |
andrelopes31 | 1:39b4c6a60595 | 248 | wait(0.1); |
andrelopes31 | 1:39b4c6a60595 | 249 | } |
andrelopes31 | 1:39b4c6a60595 | 250 | } |
andrelopes31 | 1:39b4c6a60595 | 251 | } |
andrelopes31 | 1:39b4c6a60595 | 252 | r = 1.0 - Zaxis_p; |
andrelopes31 | 1:39b4c6a60595 | 253 | g = 1.0 - Zaxis_n; |
andrelopes31 | 1:39b4c6a60595 | 254 | b = Zaxis_p; |
andrelopes31 | 1:39b4c6a60595 | 255 | //Thread:wait(osWaitForever) |
kvothe345 | 0:7677c6736b9f | 256 | |
andrelopes31 | 1:39b4c6a60595 | 257 | |
andrelopes31 | 1:39b4c6a60595 | 258 | } |
kvothe345 | 0:7677c6736b9f | 259 | |
kvothe345 | 0:7677c6736b9f | 260 | |
kvothe345 | 0:7677c6736b9f | 261 | |
andrelopes31 | 1:39b4c6a60595 | 262 | } |
andrelopes31 | 1:39b4c6a60595 | 263 | |
andrelopes31 | 1:39b4c6a60595 | 264 |