Projet S5 Info - Beginner

Dependencies:   TextLCD XBeeLib mbed-rtos mbed

Fork of Coordinateur-Advanced by Vincent Belanger

Committer:
vinbel93
Date:
Thu Apr 14 15:04:29 2016 +0000
Revision:
10:3e598dcf9e59
Parent:
9:912506053afd
Final Beginner Version 3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jasmou 0:3acd447c12c0 1 #include "mbed.h"
jasmou 0:3acd447c12c0 2 #include "XBeeLib.h"
vinbel93 7:bd9bc9fa66c7 3 #include "TextLCD.h"
vinbel93 7:bd9bc9fa66c7 4 #include "rtos.h"
vinbel93 7:bd9bc9fa66c7 5
jasmou 0:3acd447c12c0 6 using namespace XBeeLib;
vinbel93 1:33b4a3b5cdb5 7
vinbel93 5:08245a3e93a8 8 PwmOut speaker(p21);
vinbel93 7:bd9bc9fa66c7 9 TextLCD lcd(p15, p16, p17, p18, p19, p20); // rs, e, d4-d7
vinbel93 7:bd9bc9fa66c7 10 DigitalIn button_back(p28);
vinbel93 7:bd9bc9fa66c7 11 DigitalIn button_enter(p29);
vinbel93 7:bd9bc9fa66c7 12 DigitalIn button_next(p30);
vinbel93 7:bd9bc9fa66c7 13 Ticker timer;
vinbel93 7:bd9bc9fa66c7 14 int btn_b;
vinbel93 7:bd9bc9fa66c7 15 int btn_e;
vinbel93 7:bd9bc9fa66c7 16 int btn_n;
jasmou 0:3acd447c12c0 17
jasmou 0:3acd447c12c0 18 bool capt_1;
jasmou 0:3acd447c12c0 19 bool capt_2;
jasmou 0:3acd447c12c0 20 bool capt_3;
jasmou 0:3acd447c12c0 21 bool capt_4;
jasmou 0:3acd447c12c0 22 bool capt_5;
jasmou 0:3acd447c12c0 23 bool capt_6;
vinbel93 7:bd9bc9fa66c7 24
vinbel93 7:bd9bc9fa66c7 25 const float DO = 261.63;
vinbel93 7:bd9bc9fa66c7 26 const float RE = 293.66;
vinbel93 7:bd9bc9fa66c7 27 const float MI = 329.63;
vinbel93 10:3e598dcf9e59 28 const float FA = 349.23;
vinbel93 10:3e598dcf9e59 29 const float SOL = 392.00;
vinbel93 10:3e598dcf9e59 30 const float LA = 440.00;
vinbel93 6:59b0b6c1b5bb 31
vinbel93 6:59b0b6c1b5bb 32 float dutyCycle = 0.0002;
vinbel93 7:bd9bc9fa66c7 33 float octaveMult = 2.0;
vinbel93 7:bd9bc9fa66c7 34 int lastCapt = -1;
vinbel93 7:bd9bc9fa66c7 35 Thread* thread_digital;
vinbel93 7:bd9bc9fa66c7 36 Thread* thread_menu;
vinbel93 6:59b0b6c1b5bb 37
vinbel93 7:bd9bc9fa66c7 38 const char MainMenuText[2][16] = {"1. Volume", "2. Octave"};
vinbel93 7:bd9bc9fa66c7 39 const char VolumeMenuText[4][16] = {"+1", "-1", "+10", "-10"};
vinbel93 7:bd9bc9fa66c7 40 const char OctaveMenuText[3][16] = {"3", "4", "5"};
vinbel93 7:bd9bc9fa66c7 41 const int maxMenuCount = 2;
vinbel93 7:bd9bc9fa66c7 42 const int maxVolumeCount = 4;
vinbel93 7:bd9bc9fa66c7 43 const int maxOctaveCount = 3;
vinbel93 7:bd9bc9fa66c7 44
vinbel93 7:bd9bc9fa66c7 45 bool inMainMenu = true;
vinbel93 7:bd9bc9fa66c7 46
vinbel93 7:bd9bc9fa66c7 47 int menuCount = 0;
vinbel93 7:bd9bc9fa66c7 48 int itemCount = 0;
vinbel93 7:bd9bc9fa66c7 49
vinbel93 10:3e598dcf9e59 50 static float findPeriod(float frequency)
vinbel93 7:bd9bc9fa66c7 51 {
vinbel93 6:59b0b6c1b5bb 52 float freq = frequency * octaveMult;
vinbel93 6:59b0b6c1b5bb 53 return 1.0/freq;
vinbel93 6:59b0b6c1b5bb 54 }
vinbel93 7:bd9bc9fa66c7 55
vinbel93 8:120807be23b8 56 void timer_Signal()
vinbel93 7:bd9bc9fa66c7 57 {
vinbel93 7:bd9bc9fa66c7 58 thread_menu->signal_set(0x2);
vinbel93 7:bd9bc9fa66c7 59 }
vinbel93 7:bd9bc9fa66c7 60
vinbel93 7:bd9bc9fa66c7 61 void digitLect(void const *args)
vinbel93 7:bd9bc9fa66c7 62 {
vinbel93 7:bd9bc9fa66c7 63 while(true)
vinbel93 7:bd9bc9fa66c7 64 {
vinbel93 7:bd9bc9fa66c7 65 btn_b = button_back;
vinbel93 7:bd9bc9fa66c7 66 btn_e = button_enter;
vinbel93 7:bd9bc9fa66c7 67 btn_n = button_next;
vinbel93 7:bd9bc9fa66c7 68 }
vinbel93 7:bd9bc9fa66c7 69 }
vinbel93 7:bd9bc9fa66c7 70
jasmou 0:3acd447c12c0 71 static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
jasmou 0:3acd447c12c0 72 {
jasmou 0:3acd447c12c0 73 const uint64_t remote_addr64 = remote.get_addr64();
vinbel93 7:bd9bc9fa66c7 74
vinbel93 1:33b4a3b5cdb5 75 capt_1 = (data[0] & 0b10000000);
vinbel93 1:33b4a3b5cdb5 76 capt_2 = (data[0] & 0b01000000);
vinbel93 1:33b4a3b5cdb5 77 capt_3 = (data[0] & 0b00100000);
vinbel93 1:33b4a3b5cdb5 78 capt_4 = (data[0] & 0b00010000);
vinbel93 1:33b4a3b5cdb5 79 capt_5 = (data[0] & 0b00001000);
vinbel93 1:33b4a3b5cdb5 80 capt_6 = (data[0] & 0b00000100);
vinbel93 7:bd9bc9fa66c7 81
vinbel93 7:bd9bc9fa66c7 82 if(!capt_1) {
vinbel93 10:3e598dcf9e59 83 if (lastCapt != 1) {
vinbel93 10:3e598dcf9e59 84 speaker.period(findPeriod(DO));
vinbel93 10:3e598dcf9e59 85 }
vinbel93 10:3e598dcf9e59 86
vinbel93 7:bd9bc9fa66c7 87 speaker = dutyCycle;
vinbel93 6:59b0b6c1b5bb 88 lastCapt = 1;
vinbel93 7:bd9bc9fa66c7 89 } else if(!capt_2) {
vinbel93 10:3e598dcf9e59 90 if (lastCapt != 2) {
vinbel93 10:3e598dcf9e59 91 speaker.period(findPeriod(RE));
vinbel93 10:3e598dcf9e59 92 }
vinbel93 7:bd9bc9fa66c7 93
vinbel93 7:bd9bc9fa66c7 94 speaker = dutyCycle;
vinbel93 6:59b0b6c1b5bb 95 lastCapt = 2;
vinbel93 7:bd9bc9fa66c7 96 } else if(!capt_3) {
vinbel93 10:3e598dcf9e59 97 if (lastCapt != 3) {
vinbel93 10:3e598dcf9e59 98 speaker.period(findPeriod(MI));
vinbel93 10:3e598dcf9e59 99 }
vinbel93 7:bd9bc9fa66c7 100
vinbel93 7:bd9bc9fa66c7 101 speaker = dutyCycle;
vinbel93 6:59b0b6c1b5bb 102 lastCapt = 3;
vinbel93 7:bd9bc9fa66c7 103 } else if(!capt_4) {
vinbel93 10:3e598dcf9e59 104 if (lastCapt != 4) {
vinbel93 10:3e598dcf9e59 105 speaker.period(findPeriod(FA));
vinbel93 10:3e598dcf9e59 106 }
vinbel93 7:bd9bc9fa66c7 107
vinbel93 7:bd9bc9fa66c7 108 speaker = dutyCycle;
vinbel93 6:59b0b6c1b5bb 109 lastCapt = 4;
vinbel93 7:bd9bc9fa66c7 110 } else if(!capt_5) {
vinbel93 10:3e598dcf9e59 111 if (lastCapt != 5) {
vinbel93 10:3e598dcf9e59 112 speaker.period(findPeriod(SOL));
vinbel93 10:3e598dcf9e59 113 }
vinbel93 7:bd9bc9fa66c7 114
vinbel93 7:bd9bc9fa66c7 115 speaker = dutyCycle;
vinbel93 6:59b0b6c1b5bb 116 lastCapt = 5;
vinbel93 7:bd9bc9fa66c7 117 } else if(!capt_6) {
vinbel93 10:3e598dcf9e59 118 if (lastCapt != 6) {
vinbel93 10:3e598dcf9e59 119 speaker.period(findPeriod(LA));
vinbel93 10:3e598dcf9e59 120 }
vinbel93 7:bd9bc9fa66c7 121
vinbel93 7:bd9bc9fa66c7 122 speaker = dutyCycle;
vinbel93 6:59b0b6c1b5bb 123 lastCapt = 6;
vinbel93 4:714484401021 124 } else {
vinbel93 10:3e598dcf9e59 125 if (lastCapt != 0)
vinbel93 10:3e598dcf9e59 126 {
vinbel93 10:3e598dcf9e59 127 speaker = 0.0f;
vinbel93 10:3e598dcf9e59 128 }
vinbel93 10:3e598dcf9e59 129
vinbel93 10:3e598dcf9e59 130 lastCapt = 0;
vinbel93 7:bd9bc9fa66c7 131 }
vinbel93 7:bd9bc9fa66c7 132 }
vinbel93 7:bd9bc9fa66c7 133
vinbel93 7:bd9bc9fa66c7 134 void nextItem(int menuCount, int itemCount, bool inMainMenu)
vinbel93 7:bd9bc9fa66c7 135 {
vinbel93 7:bd9bc9fa66c7 136 lcd.cls();
vinbel93 7:bd9bc9fa66c7 137 if(inMainMenu) {
vinbel93 7:bd9bc9fa66c7 138 lcd.printf(MainMenuText[menuCount]);
vinbel93 7:bd9bc9fa66c7 139 } else if(!inMainMenu && menuCount == 0) {
vinbel93 7:bd9bc9fa66c7 140 lcd.printf(VolumeMenuText[itemCount]);
vinbel93 7:bd9bc9fa66c7 141 } else if(!inMainMenu && menuCount == 1) {
vinbel93 7:bd9bc9fa66c7 142 lcd.printf(OctaveMenuText[itemCount]);
vinbel93 7:bd9bc9fa66c7 143 } else {
vinbel93 7:bd9bc9fa66c7 144 lcd.printf("nextItem ERROR.");
vinbel93 4:714484401021 145 }
jasmou 0:3acd447c12c0 146 }
vinbel93 2:a7820185e9a8 147
vinbel93 7:bd9bc9fa66c7 148 void enterMenu()
vinbel93 1:33b4a3b5cdb5 149 {
vinbel93 7:bd9bc9fa66c7 150 lcd.cls();
vinbel93 7:bd9bc9fa66c7 151 nextItem(menuCount, itemCount, inMainMenu);
vinbel93 7:bd9bc9fa66c7 152
vinbel93 7:bd9bc9fa66c7 153 if(menuCount == 0) {
vinbel93 7:bd9bc9fa66c7 154 switch (itemCount) {
vinbel93 7:bd9bc9fa66c7 155 case 0:
vinbel93 7:bd9bc9fa66c7 156 dutyCycle += 0.0001;
vinbel93 7:bd9bc9fa66c7 157 if (dutyCycle > 0.5)
vinbel93 7:bd9bc9fa66c7 158 {
vinbel93 7:bd9bc9fa66c7 159 dutyCycle = 0.5;
vinbel93 7:bd9bc9fa66c7 160 }
vinbel93 7:bd9bc9fa66c7 161 break;
vinbel93 7:bd9bc9fa66c7 162 case 1:
vinbel93 7:bd9bc9fa66c7 163 dutyCycle -= 0.0001;
vinbel93 7:bd9bc9fa66c7 164 if (dutyCycle < 0)
vinbel93 7:bd9bc9fa66c7 165 {
vinbel93 7:bd9bc9fa66c7 166 dutyCycle = 0;
vinbel93 7:bd9bc9fa66c7 167 }
vinbel93 7:bd9bc9fa66c7 168 break;
vinbel93 7:bd9bc9fa66c7 169 case 2:
vinbel93 7:bd9bc9fa66c7 170 dutyCycle += 0.001;
vinbel93 7:bd9bc9fa66c7 171 if (dutyCycle > 0.5)
vinbel93 7:bd9bc9fa66c7 172 {
vinbel93 7:bd9bc9fa66c7 173 dutyCycle = 0.5;
vinbel93 7:bd9bc9fa66c7 174 }
vinbel93 7:bd9bc9fa66c7 175 break;
vinbel93 7:bd9bc9fa66c7 176 case 3:
vinbel93 7:bd9bc9fa66c7 177 dutyCycle -= 0.001;
vinbel93 7:bd9bc9fa66c7 178 if (dutyCycle < 0)
vinbel93 7:bd9bc9fa66c7 179 {
vinbel93 7:bd9bc9fa66c7 180 dutyCycle = 0;
vinbel93 7:bd9bc9fa66c7 181 }
vinbel93 7:bd9bc9fa66c7 182 break;
vinbel93 7:bd9bc9fa66c7 183 default:
vinbel93 7:bd9bc9fa66c7 184 break;
vinbel93 7:bd9bc9fa66c7 185 }
vinbel93 1:33b4a3b5cdb5 186 }
vinbel93 7:bd9bc9fa66c7 187 else if(menuCount == 1) {
vinbel93 7:bd9bc9fa66c7 188 switch (itemCount) {
vinbel93 7:bd9bc9fa66c7 189 case 0:
vinbel93 7:bd9bc9fa66c7 190 octaveMult = 0.5;
vinbel93 7:bd9bc9fa66c7 191 break;
vinbel93 7:bd9bc9fa66c7 192 case 1:
vinbel93 7:bd9bc9fa66c7 193 octaveMult = 1.0;
vinbel93 7:bd9bc9fa66c7 194 break;
vinbel93 7:bd9bc9fa66c7 195 case 2:
vinbel93 7:bd9bc9fa66c7 196 octaveMult = 2.0;
vinbel93 7:bd9bc9fa66c7 197 break;
vinbel93 7:bd9bc9fa66c7 198 default:
vinbel93 7:bd9bc9fa66c7 199 break;
vinbel93 7:bd9bc9fa66c7 200 }
vinbel93 4:714484401021 201 }
vinbel93 2:a7820185e9a8 202 }
vinbel93 2:a7820185e9a8 203
vinbel93 7:bd9bc9fa66c7 204 void backMenu()
vinbel93 2:a7820185e9a8 205 {
vinbel93 7:bd9bc9fa66c7 206 lcd.cls();
vinbel93 7:bd9bc9fa66c7 207 nextItem(menuCount, itemCount, inMainMenu);
vinbel93 7:bd9bc9fa66c7 208 }
vinbel93 2:a7820185e9a8 209
vinbel93 7:bd9bc9fa66c7 210 void processMenu(void const *args)
vinbel93 7:bd9bc9fa66c7 211 {
vinbel93 7:bd9bc9fa66c7 212 while(true)
vinbel93 7:bd9bc9fa66c7 213 {
vinbel93 7:bd9bc9fa66c7 214 Thread::signal_wait(0x2);
vinbel93 7:bd9bc9fa66c7 215 if(btn_n) {
vinbel93 7:bd9bc9fa66c7 216 if(menuCount != maxMenuCount - 1 && inMainMenu) {
vinbel93 7:bd9bc9fa66c7 217 menuCount++;
vinbel93 7:bd9bc9fa66c7 218 }
vinbel93 7:bd9bc9fa66c7 219
vinbel93 7:bd9bc9fa66c7 220 else if(menuCount == maxMenuCount - 1 && inMainMenu) {
vinbel93 7:bd9bc9fa66c7 221 menuCount = 0;
vinbel93 7:bd9bc9fa66c7 222 }
vinbel93 7:bd9bc9fa66c7 223
vinbel93 7:bd9bc9fa66c7 224 if(menuCount == 0 && !inMainMenu) {
vinbel93 7:bd9bc9fa66c7 225 if(itemCount != maxVolumeCount - 1) {
vinbel93 7:bd9bc9fa66c7 226 itemCount++;
vinbel93 7:bd9bc9fa66c7 227 } else {
vinbel93 7:bd9bc9fa66c7 228 itemCount = 0;
vinbel93 7:bd9bc9fa66c7 229 }
vinbel93 7:bd9bc9fa66c7 230 }
vinbel93 7:bd9bc9fa66c7 231
vinbel93 7:bd9bc9fa66c7 232 if(menuCount == 1 && !inMainMenu) {
vinbel93 7:bd9bc9fa66c7 233 if(itemCount != maxOctaveCount - 1) {
vinbel93 7:bd9bc9fa66c7 234 itemCount++;
vinbel93 7:bd9bc9fa66c7 235 } else {
vinbel93 7:bd9bc9fa66c7 236 itemCount = 0;
vinbel93 7:bd9bc9fa66c7 237 }
vinbel93 7:bd9bc9fa66c7 238 }
vinbel93 7:bd9bc9fa66c7 239 nextItem(menuCount, itemCount, inMainMenu);
vinbel93 7:bd9bc9fa66c7 240 }
vinbel93 7:bd9bc9fa66c7 241
vinbel93 7:bd9bc9fa66c7 242 if(btn_e) {
vinbel93 7:bd9bc9fa66c7 243
vinbel93 7:bd9bc9fa66c7 244 inMainMenu = false;
vinbel93 7:bd9bc9fa66c7 245 enterMenu();
vinbel93 7:bd9bc9fa66c7 246
vinbel93 7:bd9bc9fa66c7 247 }
vinbel93 7:bd9bc9fa66c7 248
vinbel93 7:bd9bc9fa66c7 249 if(btn_b) {
vinbel93 7:bd9bc9fa66c7 250 inMainMenu = true;
vinbel93 7:bd9bc9fa66c7 251 itemCount = 0;
vinbel93 7:bd9bc9fa66c7 252 backMenu();
vinbel93 7:bd9bc9fa66c7 253 }
vinbel93 4:714484401021 254 }
vinbel93 2:a7820185e9a8 255 }
vinbel93 7:bd9bc9fa66c7 256
vinbel93 2:a7820185e9a8 257 int main()
vinbel93 7:bd9bc9fa66c7 258 {
vinbel93 7:bd9bc9fa66c7 259 XBeeZB xbee = XBeeZB(p13, p14, NC, NC, NC, 9600);
jasmou 0:3acd447c12c0 260 xbee.register_receive_cb(&receive_cb);
jasmou 0:3acd447c12c0 261 RadioStatus const radioStatus = xbee.init();
vinbel93 7:bd9bc9fa66c7 262
vinbel93 8:120807be23b8 263 timer.attach_us(&timer_Signal, 100000);
vinbel93 7:bd9bc9fa66c7 264 thread_digital = new Thread(digitLect);
vinbel93 7:bd9bc9fa66c7 265 thread_menu = new Thread(processMenu);
jasmou 0:3acd447c12c0 266 MBED_ASSERT(radioStatus == Success);
vinbel93 7:bd9bc9fa66c7 267
jasmou 0:3acd447c12c0 268 while (!xbee.is_joined()) {
jasmou 0:3acd447c12c0 269 wait_ms(1000);
jasmou 0:3acd447c12c0 270 }
vinbel93 7:bd9bc9fa66c7 271
vinbel93 7:bd9bc9fa66c7 272 lcd.printf("Make a Selection:");
vinbel93 7:bd9bc9fa66c7 273
jasmou 0:3acd447c12c0 274 while (true) {
jasmou 0:3acd447c12c0 275 xbee.process_rx_frames();
vinbel93 7:bd9bc9fa66c7 276 wait_ms(10);
jasmou 0:3acd447c12c0 277 }
jasmou 0:3acd447c12c0 278 }