Wii Nunchuk via RFM69HW to Duplo 9203 Remote Control Car Kit using ARM mbed on a FRDM-KL25Z
Dependencies: CRC FastPWM RFM69 USBDevice WakeUp WiiChuk_compat mbed-rtos mbed tlc59108
Fork of wiiNunchuk_compat by
main.cpp@16:3490cdea4986, 2016-06-28 (annotated)
- Committer:
- eisd
- Date:
- Tue Jun 28 11:54:04 2016 +0000
- Revision:
- 16:3490cdea4986
- Parent:
- 15:8888e36afe43
- Child:
- 17:b9030e94b622
Deeper nunchuk transmitter sleep
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gbrush | 0:7c98bcd8a245 | 1 | #include "mbed.h" |
eisd | 5:7af5760d7e8f | 2 | #include "rtos.h" |
eisd | 13:ab524cb4f768 | 3 | #include "WakeUp.h" |
eisd | 11:7c3fb795af73 | 4 | #include "FastPWM.h" |
eisd | 1:de8c34c9ccdf | 5 | #ifndef M_PI |
eisd | 1:de8c34c9ccdf | 6 | #define M_PI 3.14159265358979323846 |
eisd | 1:de8c34c9ccdf | 7 | #endif |
eisd | 1:de8c34c9ccdf | 8 | |
eisd | 1:de8c34c9ccdf | 9 | #include "WiiChuk_compat.hpp" |
eisd | 7:f720032c2fb9 | 10 | #include "lib_crc.h" |
gbrush | 0:7c98bcd8a245 | 11 | |
eisd | 2:04fdd571a385 | 12 | //#include "USBSerial.h" |
eisd | 2:04fdd571a385 | 13 | |
eisd | 2:04fdd571a385 | 14 | #include "RFM69.h" |
eisd | 2:04fdd571a385 | 15 | #define GATEWAY_ID 2 |
eisd | 2:04fdd571a385 | 16 | #define NODE_ID 1 |
eisd | 2:04fdd571a385 | 17 | #define NETWORKID 100 |
gbrush | 0:7c98bcd8a245 | 18 | |
eisd | 2:04fdd571a385 | 19 | // Uncomment only one of the following three to match radio frequency |
eisd | 2:04fdd571a385 | 20 | //#define FREQUENCY RF69_433MHZ |
eisd | 2:04fdd571a385 | 21 | #define FREQUENCY RF69_868MHZ |
eisd | 2:04fdd571a385 | 22 | //#define FREQUENCY RF69_915MHZ |
eisd | 2:04fdd571a385 | 23 | |
eisd | 3:9091adbed369 | 24 | const char *directions[8] = { "XR", "RR", "RX", "FR", "FX", "FF", "XF", "RF" }; |
eisd | 5:7af5760d7e8f | 25 | |
eisd | 14:579137c8ceac | 26 | #define DEBUG 0 |
eisd | 14:579137c8ceac | 27 | #ifdef DEBUG |
eisd | 5:7af5760d7e8f | 28 | #ifdef USBSerial |
eisd | 5:7af5760d7e8f | 29 | USBSerial pc; |
eisd | 5:7af5760d7e8f | 30 | #else |
eisd | 2:04fdd571a385 | 31 | Serial pc(USBTX, USBRX); |
eisd | 1:de8c34c9ccdf | 32 | #endif |
eisd | 14:579137c8ceac | 33 | #else |
eisd | 14:579137c8ceac | 34 | class Null : public Stream { |
eisd | 14:579137c8ceac | 35 | public: |
eisd | 14:579137c8ceac | 36 | Null(): Stream("null") {} |
eisd | 14:579137c8ceac | 37 | void baud(int) {} |
eisd | 5:7af5760d7e8f | 38 | |
eisd | 14:579137c8ceac | 39 | protected: |
eisd | 14:579137c8ceac | 40 | virtual int _getc() { return 0; } |
eisd | 14:579137c8ceac | 41 | virtual int _putc(int c) { return 0; } |
eisd | 14:579137c8ceac | 42 | }; |
eisd | 14:579137c8ceac | 43 | Null pc; |
eisd | 14:579137c8ceac | 44 | #endif |
eisd | 5:7af5760d7e8f | 45 | #if TARGET_KL25Z |
eisd | 5:7af5760d7e8f | 46 | DigitalOut gnd(PTC0); |
eisd | 5:7af5760d7e8f | 47 | PwmOut ir(PTD4); |
eisd | 5:7af5760d7e8f | 48 | #endif |
eisd | 5:7af5760d7e8f | 49 | #define pulse(x, y) ir = 0.5; wait_us(x); ir = 0.0; wait_us(y); |
eisd | 5:7af5760d7e8f | 50 | |
eisd | 5:7af5760d7e8f | 51 | /* generated in bash from lirc raw codes */ |
eisd | 5:7af5760d7e8f | 52 | /* |
eisd | 5:7af5760d7e8f | 53 | f=duplo.conf |
eisd | 5:7af5760d7e8f | 54 | sed -n '/begin raw_codes/,/end raw_codes/s/name //p' $f| |
eisd | 5:7af5760d7e8f | 55 | while read n; do |
eisd | 5:7af5760d7e8f | 56 | echo -e "void $n() {$( |
eisd | 5:7af5760d7e8f | 57 | grep $n -A 1 $f|tail -n1| |
eisd | 5:7af5760d7e8f | 58 | sed 's/$/ 1000/;s@\([0-9]\+\)[[:space:]]\+\([0-9]\+\)[[:space:]]*@\n\tpulse(\1, \2);@g' |
eisd | 5:7af5760d7e8f | 59 | )\n}" |
eisd | 5:7af5760d7e8f | 60 | done |
eisd | 5:7af5760d7e8f | 61 | */ |
eisd | 5:7af5760d7e8f | 62 | |
eisd | 5:7af5760d7e8f | 63 | void FX() { |
eisd | 5:7af5760d7e8f | 64 | pulse(1042, 208); |
eisd | 5:7af5760d7e8f | 65 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 66 | pulse(208, 562); |
eisd | 5:7af5760d7e8f | 67 | pulse(625, 208); |
eisd | 5:7af5760d7e8f | 68 | pulse(208, 625); |
eisd | 5:7af5760d7e8f | 69 | pulse(208, 1); |
eisd | 5:7af5760d7e8f | 70 | } |
eisd | 5:7af5760d7e8f | 71 | void XF() { |
eisd | 5:7af5760d7e8f | 72 | pulse(229, 604); |
eisd | 5:7af5760d7e8f | 73 | pulse(229, 188); |
eisd | 5:7af5760d7e8f | 74 | pulse(229, 188); |
eisd | 5:7af5760d7e8f | 75 | pulse(438, 333); |
eisd | 5:7af5760d7e8f | 76 | pulse(625, 208); |
eisd | 5:7af5760d7e8f | 77 | pulse(1250, 1); |
eisd | 5:7af5760d7e8f | 78 | } |
eisd | 5:7af5760d7e8f | 79 | void FF() { |
eisd | 5:7af5760d7e8f | 80 | pulse(208, 625); |
eisd | 5:7af5760d7e8f | 81 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 82 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 83 | pulse(417, 354); |
eisd | 5:7af5760d7e8f | 84 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 85 | pulse(208, 1042); |
eisd | 5:7af5760d7e8f | 86 | pulse(208, 1); |
eisd | 5:7af5760d7e8f | 87 | } |
eisd | 5:7af5760d7e8f | 88 | void RX() { |
eisd | 5:7af5760d7e8f | 89 | pulse(1042, 208); |
eisd | 5:7af5760d7e8f | 90 | pulse(208, 188); |
eisd | 5:7af5760d7e8f | 91 | pulse(229, 542); |
eisd | 5:7af5760d7e8f | 92 | pulse(646, 1); |
eisd | 5:7af5760d7e8f | 93 | } |
eisd | 5:7af5760d7e8f | 94 | void XR() { |
eisd | 5:7af5760d7e8f | 95 | pulse(208, 1042); |
eisd | 5:7af5760d7e8f | 96 | pulse(208, 188); |
eisd | 5:7af5760d7e8f | 97 | pulse(229, 542); |
eisd | 5:7af5760d7e8f | 98 | pulse(625, 417); |
eisd | 5:7af5760d7e8f | 99 | pulse(854, 1); |
eisd | 5:7af5760d7e8f | 100 | } |
eisd | 5:7af5760d7e8f | 101 | void RR() { |
eisd | 5:7af5760d7e8f | 102 | pulse(229, 1021); |
eisd | 5:7af5760d7e8f | 103 | pulse(229, 208); |
eisd | 5:7af5760d7e8f | 104 | pulse(208, 542); |
eisd | 5:7af5760d7e8f | 105 | pulse(229, 188); |
eisd | 5:7af5760d7e8f | 106 | pulse(229, 1229); |
eisd | 5:7af5760d7e8f | 107 | pulse(229, 1); |
eisd | 5:7af5760d7e8f | 108 | } |
eisd | 5:7af5760d7e8f | 109 | void RF() { |
eisd | 5:7af5760d7e8f | 110 | pulse(208, 625); |
eisd | 5:7af5760d7e8f | 111 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 112 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 113 | pulse(417, 333); |
eisd | 5:7af5760d7e8f | 114 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 115 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 116 | pulse(208, 1); |
eisd | 5:7af5760d7e8f | 117 | } |
eisd | 5:7af5760d7e8f | 118 | void FR() { |
eisd | 5:7af5760d7e8f | 119 | pulse(229, 1021); |
eisd | 5:7af5760d7e8f | 120 | pulse(229, 188); |
eisd | 5:7af5760d7e8f | 121 | pulse(229, 542); |
eisd | 5:7af5760d7e8f | 122 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 123 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 124 | pulse(208, 625); |
eisd | 5:7af5760d7e8f | 125 | pulse(417, 1); |
eisd | 5:7af5760d7e8f | 126 | } |
eisd | 5:7af5760d7e8f | 127 | void BB() { |
eisd | 5:7af5760d7e8f | 128 | pulse(1042, 208); |
eisd | 5:7af5760d7e8f | 129 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 130 | pulse(208, 542); |
eisd | 5:7af5760d7e8f | 131 | pulse(208, 417); |
eisd | 5:7af5760d7e8f | 132 | pulse(208, 208); |
eisd | 5:7af5760d7e8f | 133 | pulse(1042, 1); |
eisd | 5:7af5760d7e8f | 134 | } |
eisd | 5:7af5760d7e8f | 135 | |
eisd | 5:7af5760d7e8f | 136 | bool central = true; |
eisd | 12:eec17d54a646 | 137 | Timer central_time; |
eisd | 7:f720032c2fb9 | 138 | int stops_sent = 0; |
eisd | 5:7af5760d7e8f | 139 | int direction = -1; |
eisd | 5:7af5760d7e8f | 140 | Thread *thread; |
eisd | 5:7af5760d7e8f | 141 | void ir_thread(void const *args) { |
eisd | 5:7af5760d7e8f | 142 | while(1) { |
eisd | 5:7af5760d7e8f | 143 | //if (!central) |
eisd | 5:7af5760d7e8f | 144 | for(int x = 0; x < 5; x++) { |
eisd | 14:579137c8ceac | 145 | #if DEBUG |
eisd | 14:579137c8ceac | 146 | pc.printf(central? "stop %s %d\r\n" : "direction %s %d\r\n", directions[direction], stops_sent); |
eisd | 14:579137c8ceac | 147 | #endif |
eisd | 9:dc6ffa2ef179 | 148 | if (central) { |
eisd | 10:ba778fc959b8 | 149 | if (stops_sent < 50) { |
eisd | 10:ba778fc959b8 | 150 | stops_sent++; |
eisd | 9:dc6ffa2ef179 | 151 | BB(); |
eisd | 10:ba778fc959b8 | 152 | } |
eisd | 9:dc6ffa2ef179 | 153 | } else { |
eisd | 7:f720032c2fb9 | 154 | stops_sent = 0; |
eisd | 7:f720032c2fb9 | 155 | switch(direction) { |
eisd | 7:f720032c2fb9 | 156 | case 0: XR(); break; |
eisd | 7:f720032c2fb9 | 157 | case 1: RR(); break; |
eisd | 7:f720032c2fb9 | 158 | case 2: RX(); break; |
eisd | 7:f720032c2fb9 | 159 | case 3: FR(); break; |
eisd | 7:f720032c2fb9 | 160 | case 4: FX(); break; |
eisd | 7:f720032c2fb9 | 161 | case 5: FF(); break; |
eisd | 7:f720032c2fb9 | 162 | case 6: XF(); break; |
eisd | 7:f720032c2fb9 | 163 | case 7: RF(); break; |
eisd | 7:f720032c2fb9 | 164 | } |
eisd | 5:7af5760d7e8f | 165 | } |
eisd | 6:3482f1e19d71 | 166 | Thread::wait(10); |
eisd | 5:7af5760d7e8f | 167 | } |
eisd | 5:7af5760d7e8f | 168 | Thread::wait(50); |
eisd | 5:7af5760d7e8f | 169 | } |
eisd | 5:7af5760d7e8f | 170 | } |
eisd | 5:7af5760d7e8f | 171 | |
eisd | 11:7c3fb795af73 | 172 | FastPWM speaker(PTA12); |
eisd | 7:f720032c2fb9 | 173 | DigitalOut speaker_gnd(PTC4); |
eisd | 7:f720032c2fb9 | 174 | //float speaker = 0; |
eisd | 7:f720032c2fb9 | 175 | //float speaker_gnd = 0; |
eisd | 7:f720032c2fb9 | 176 | |
eisd | 7:f720032c2fb9 | 177 | //global variables used by interrupt routine |
eisd | 11:7c3fb795af73 | 178 | volatile int i=512; |
eisd | 12:eec17d54a646 | 179 | float *wave;//[512]; |
eisd | 7:f720032c2fb9 | 180 | |
eisd | 7:f720032c2fb9 | 181 | // Interrupt routine |
eisd | 7:f720032c2fb9 | 182 | // used to output next analog sample whenever a timer interrupt occurs |
eisd | 7:f720032c2fb9 | 183 | void Sample_timer_interrupt(void) |
eisd | 7:f720032c2fb9 | 184 | { |
eisd | 7:f720032c2fb9 | 185 | // send next analog sample out to D to A |
eisd | 7:f720032c2fb9 | 186 | speaker = wave[i]; |
eisd | 7:f720032c2fb9 | 187 | // increment pointer and wrap around back to 0 at 128 |
eisd | 7:f720032c2fb9 | 188 | i = (i+1) & 0x07F; |
eisd | 7:f720032c2fb9 | 189 | } |
eisd | 7:f720032c2fb9 | 190 | |
eisd | 7:f720032c2fb9 | 191 | int Siren_pitch = 1; |
eisd | 7:f720032c2fb9 | 192 | void Siren_pitch_flip() { |
eisd | 11:7c3fb795af73 | 193 | speaker.period(wave[--i]); |
eisd | 11:7c3fb795af73 | 194 | if (i == 0) |
eisd | 11:7c3fb795af73 | 195 | i = 128; |
eisd | 7:f720032c2fb9 | 196 | } |
eisd | 7:f720032c2fb9 | 197 | |
eisd | 7:f720032c2fb9 | 198 | Ticker siren_pitch; |
eisd | 11:7c3fb795af73 | 199 | Timeout siren_pitch_switch; |
eisd | 7:f720032c2fb9 | 200 | bool Siren_last = 0; |
eisd | 11:7c3fb795af73 | 201 | void Siren_faster() { |
eisd | 11:7c3fb795af73 | 202 | siren_pitch.detach(); |
eisd | 11:7c3fb795af73 | 203 | siren_pitch.attach(&Siren_pitch_flip, 0.6/128); |
eisd | 11:7c3fb795af73 | 204 | } |
eisd | 7:f720032c2fb9 | 205 | void Siren_state(bool on) { |
eisd | 7:f720032c2fb9 | 206 | if (Siren_last != on) { |
eisd | 7:f720032c2fb9 | 207 | pc.printf("siren %d\r\n", on); |
eisd | 7:f720032c2fb9 | 208 | siren_pitch.detach(); |
eisd | 7:f720032c2fb9 | 209 | Siren_pitch = 1; |
eisd | 7:f720032c2fb9 | 210 | Siren_pitch_flip(); |
eisd | 11:7c3fb795af73 | 211 | siren_pitch.detach(); |
eisd | 11:7c3fb795af73 | 212 | if (on) |
eisd | 11:7c3fb795af73 | 213 | siren_pitch.attach(&Siren_pitch_flip, 0.6/128); |
eisd | 11:7c3fb795af73 | 214 | //siren_pitch_switch.attach(&Siren_faster, 2.0); |
eisd | 11:7c3fb795af73 | 215 | speaker = on ? 0.1 : 0; |
eisd | 7:f720032c2fb9 | 216 | } |
eisd | 7:f720032c2fb9 | 217 | Siren_last = on; |
eisd | 7:f720032c2fb9 | 218 | } |
eisd | 7:f720032c2fb9 | 219 | |
eisd | 8:5f933580b560 | 220 | DigitalOut Headlight_l(PTD7); |
eisd | 8:5f933580b560 | 221 | DigitalOut Headlight_r(PTB8); |
eisd | 8:5f933580b560 | 222 | |
eisd | 8:5f933580b560 | 223 | #define Headlight_swap 3 |
eisd | 8:5f933580b560 | 224 | #define Headlight_flicker 15 |
eisd | 8:5f933580b560 | 225 | uint8_t Headlight_mode = 0; |
eisd | 8:5f933580b560 | 226 | uint8_t Headlight_phase = 0; |
eisd | 8:5f933580b560 | 227 | Ticker Headlight_pattern; |
eisd | 8:5f933580b560 | 228 | void Headlight_interrupt() { |
eisd | 8:5f933580b560 | 229 | Headlight_phase = (Headlight_phase + 1) % Headlight_flicker; |
eisd | 8:5f933580b560 | 230 | Headlight_l = Headlight_mode == 1 || (Headlight_mode == 2) && ((Headlight_phase % 2) == 0) && (Headlight_phase * 2 > Headlight_flicker); |
eisd | 8:5f933580b560 | 231 | Headlight_r = Headlight_mode == 1 || (Headlight_mode == 2) && ((Headlight_phase % 2) == 0) && (Headlight_phase * 2 < Headlight_flicker); |
eisd | 8:5f933580b560 | 232 | } |
eisd | 8:5f933580b560 | 233 | void Headlight_state(bool a, bool b) { |
eisd | 8:5f933580b560 | 234 | uint8_t mode = b ? 1 : a ? 2 : 0; |
eisd | 8:5f933580b560 | 235 | if (Headlight_mode != mode) { |
eisd | 11:7c3fb795af73 | 236 | if (mode == 1) |
eisd | 11:7c3fb795af73 | 237 | i = 512; |
eisd | 8:5f933580b560 | 238 | Headlight_mode = mode; |
eisd | 12:eec17d54a646 | 239 | Headlight_pattern.detach(); |
eisd | 8:5f933580b560 | 240 | Headlight_interrupt(); |
eisd | 12:eec17d54a646 | 241 | if (mode > 0) |
eisd | 12:eec17d54a646 | 242 | Headlight_pattern.attach_us(&Headlight_interrupt, (timestamp_t)(1000000/Headlight_swap/Headlight_flicker)); |
eisd | 8:5f933580b560 | 243 | } |
eisd | 8:5f933580b560 | 244 | } |
eisd | 8:5f933580b560 | 245 | |
eisd | 12:eec17d54a646 | 246 | void sleep_loop(void const *argument) { |
eisd | 12:eec17d54a646 | 247 | while (true) { |
eisd | 12:eec17d54a646 | 248 | sleep(); |
eisd | 12:eec17d54a646 | 249 | Thread::wait(100); |
eisd | 12:eec17d54a646 | 250 | } |
eisd | 12:eec17d54a646 | 251 | } |
gbrush | 0:7c98bcd8a245 | 252 | |
eisd | 2:04fdd571a385 | 253 | #ifdef TARGET_KL25Z |
eisd | 6:3482f1e19d71 | 254 | PwmOut r(LED_RED); |
eisd | 6:3482f1e19d71 | 255 | //float r = 1.0f; |
eisd | 6:3482f1e19d71 | 256 | PwmOut g(LED_GREEN); |
eisd | 6:3482f1e19d71 | 257 | //float g = 1.0f; |
eisd | 6:3482f1e19d71 | 258 | PwmOut b(LED_BLUE); |
eisd | 6:3482f1e19d71 | 259 | //float b = 1.0f; |
eisd | 3:9091adbed369 | 260 | WiiChuck nun(PTE0, PTE1, pc); |
eisd | 2:04fdd571a385 | 261 | RFM69 radio(PTD2, PTD3, PTC5, PTD0, PTA13); |
eisd | 2:04fdd571a385 | 262 | #else |
eisd | 2:04fdd571a385 | 263 | WiiChuck nun(p9, p10, pc); |
eisd | 2:04fdd571a385 | 264 | #endif |
eisd | 2:04fdd571a385 | 265 | |
eisd | 13:ab524cb4f768 | 266 | Timer rx_last_contact; |
eisd | 12:eec17d54a646 | 267 | bool rx_to_snooze = true; |
eisd | 12:eec17d54a646 | 268 | bool rx_snoozing = false; |
eisd | 12:eec17d54a646 | 269 | bool rx_snoozed() { |
eisd | 12:eec17d54a646 | 270 | if (rx_snoozing) { |
eisd | 12:eec17d54a646 | 271 | pc.printf("still snoozing\r\n"); |
eisd | 12:eec17d54a646 | 272 | Thread::signal_wait(0x1); |
eisd | 12:eec17d54a646 | 273 | pc.printf("signalled?\r\n"); |
eisd | 12:eec17d54a646 | 274 | } |
eisd | 12:eec17d54a646 | 275 | return true; |
eisd | 12:eec17d54a646 | 276 | } |
eisd | 12:eec17d54a646 | 277 | |
eisd | 12:eec17d54a646 | 278 | void rx_snoozer(void const *mainThreadID) { |
eisd | 13:ab524cb4f768 | 279 | pc.printf("snooze rx %f\r\n", rx_last_contact.read()); |
eisd | 12:eec17d54a646 | 280 | |
eisd | 12:eec17d54a646 | 281 | rx_snoozing = true; |
eisd | 13:ab524cb4f768 | 282 | radio.sleep(); |
eisd | 13:ab524cb4f768 | 283 | WakeUp::set(5);//rx_last_contact.read() < 10? 1 : 5); |
eisd | 13:ab524cb4f768 | 284 | deepsleep(); |
eisd | 12:eec17d54a646 | 285 | rx_snoozing = false; |
eisd | 12:eec17d54a646 | 286 | rx_to_snooze = true; |
eisd | 12:eec17d54a646 | 287 | pc.printf("unsnooze rx\r\n"); |
eisd | 12:eec17d54a646 | 288 | osSignalSet((osThreadId)mainThreadID, 0x1); |
eisd | 12:eec17d54a646 | 289 | } |
eisd | 12:eec17d54a646 | 290 | |
eisd | 12:eec17d54a646 | 291 | int main() |
eisd | 12:eec17d54a646 | 292 | { |
eisd | 13:ab524cb4f768 | 293 | RtosTimer rx_snooze(&rx_snoozer, osTimerOnce, (void*)osThreadGetId()); |
eisd | 12:eec17d54a646 | 294 | |
eisd | 12:eec17d54a646 | 295 | #ifndef USBSerial |
eisd | 12:eec17d54a646 | 296 | pc.baud(115200); |
eisd | 12:eec17d54a646 | 297 | #endif |
eisd | 12:eec17d54a646 | 298 | |
eisd | 13:ab524cb4f768 | 299 | r = g = b = 1; |
eisd | 6:3482f1e19d71 | 300 | gnd = 0; |
eisd | 6:3482f1e19d71 | 301 | ir.period_us(1000/38); |
eisd | 6:3482f1e19d71 | 302 | |
eisd | 7:f720032c2fb9 | 303 | speaker_gnd = 0; |
eisd | 7:f720032c2fb9 | 304 | speaker = 0; |
eisd | 7:f720032c2fb9 | 305 | //speaker.period(1.0/200000.0); |
eisd | 7:f720032c2fb9 | 306 | |
eisd | 7:f720032c2fb9 | 307 | /* |
eisd | 7:f720032c2fb9 | 308 | speaker = 0.2; |
eisd | 7:f720032c2fb9 | 309 | while(1) { |
eisd | 7:f720032c2fb9 | 310 | speaker.period(.8/554.365); |
eisd | 7:f720032c2fb9 | 311 | wait(.8); |
eisd | 7:f720032c2fb9 | 312 | speaker.period(.8/523.251); |
eisd | 7:f720032c2fb9 | 313 | wait(.8); |
eisd | 7:f720032c2fb9 | 314 | } |
eisd | 7:f720032c2fb9 | 315 | //speaker = 0.2; |
eisd | 7:f720032c2fb9 | 316 | // set up a timer to be used for sample rate interrupts |
eisd | 7:f720032c2fb9 | 317 | Ticker Sample_Period; |
eisd | 7:f720032c2fb9 | 318 | // precompute 128 sample points on one sine wave cycle |
eisd | 7:f720032c2fb9 | 319 | // used for continuous sine wave output later |
eisd | 7:f720032c2fb9 | 320 | for(int k=0; k<128; k++) { |
eisd | 7:f720032c2fb9 | 321 | wave[k]=((1.0 + sin((float(k)/128.0*6.28318530717959)))/2.0); |
eisd | 7:f720032c2fb9 | 322 | // scale the sine wave from 0.0 to 1.0 - as needed for AnalogOut arg |
eisd | 7:f720032c2fb9 | 323 | } |
eisd | 7:f720032c2fb9 | 324 | // turn on timer interrupts to start sine wave output |
eisd | 7:f720032c2fb9 | 325 | // sample rate is 500Hz with 128 samples per cycle on sine wave |
eisd | 7:f720032c2fb9 | 326 | while(1) { |
eisd | 7:f720032c2fb9 | 327 | Sample_Period.detach(); |
eisd | 7:f720032c2fb9 | 328 | Sample_Period.attach(&Sample_timer_interrupt, 1.0/(554.365*128)); |
eisd | 7:f720032c2fb9 | 329 | wait(.8); |
eisd | 7:f720032c2fb9 | 330 | Sample_Period.detach(); |
eisd | 7:f720032c2fb9 | 331 | Sample_Period.attach(&Sample_timer_interrupt, 1.0/(523.251*128)); |
eisd | 7:f720032c2fb9 | 332 | wait(.8); |
eisd | 7:f720032c2fb9 | 333 | } |
eisd | 7:f720032c2fb9 | 334 | // everything else needed is already being done by the interrupt routine |
eisd | 7:f720032c2fb9 | 335 | */ |
eisd | 7:f720032c2fb9 | 336 | |
eisd | 5:7af5760d7e8f | 337 | nunchuk n1, n2; |
eisd | 5:7af5760d7e8f | 338 | nunchuk *n = &n1, *next = &n2; |
eisd | 5:7af5760d7e8f | 339 | bool sender = nun.Read(&n->X, &n->Y, &n->aX, &n->aY, &n->aZ, &n->C, &n->Z); |
eisd | 4:c9711f0cd097 | 340 | if (sender) { |
eisd | 4:c9711f0cd097 | 341 | pc.printf("chuck attached\r\n"); |
eisd | 4:c9711f0cd097 | 342 | radio.initialize(FREQUENCY, NODE_ID, NETWORKID); |
eisd | 15:8888e36afe43 | 343 | central_time.start(); |
eisd | 4:c9711f0cd097 | 344 | } else { |
eisd | 4:c9711f0cd097 | 345 | pc.printf("chuck unavailable\r\n"); |
eisd | 4:c9711f0cd097 | 346 | radio.initialize(FREQUENCY, GATEWAY_ID, NETWORKID); |
eisd | 15:8888e36afe43 | 347 | // only relevant to the receiver |
eisd | 5:7af5760d7e8f | 348 | thread = new Thread(ir_thread); |
eisd | 15:8888e36afe43 | 349 | rx_last_contact.start(); |
eisd | 15:8888e36afe43 | 350 | // these break the nunchuk |
eisd | 12:eec17d54a646 | 351 | wave = new float[i]; |
eisd | 12:eec17d54a646 | 352 | int m = i-128; |
eisd | 12:eec17d54a646 | 353 | for(int k = 0; k < m; k++) { |
eisd | 12:eec17d54a646 | 354 | // ramp up |
eisd | 12:eec17d54a646 | 355 | wave[i-k-1] = 1.0/(1000+k*400.0/m); |
eisd | 12:eec17d54a646 | 356 | } |
eisd | 12:eec17d54a646 | 357 | for(int k = 0; k < 128; k++) { |
eisd | 12:eec17d54a646 | 358 | // LFO |
eisd | 12:eec17d54a646 | 359 | wave[127-k] = 1.0/(1400+200*sin(k/128.0*6.28318530717959)); |
eisd | 12:eec17d54a646 | 360 | } |
eisd | 15:8888e36afe43 | 361 | WakeUp::calibrate(); |
eisd | 15:8888e36afe43 | 362 | } |
eisd | 3:9091adbed369 | 363 | radio.encrypt("0123456789054321"); |
eisd | 2:04fdd571a385 | 364 | //radio.promiscuous(false); |
eisd | 3:9091adbed369 | 365 | radio.setHighPower(true); |
eisd | 3:9091adbed369 | 366 | radio.setPowerLevel(20); |
eisd | 3:9091adbed369 | 367 | radio.rcCalibration(); |
eisd | 2:04fdd571a385 | 368 | //radio.readAllRegs(); |
eisd | 2:04fdd571a385 | 369 | pc.printf("temp %d\r\n", radio.readTemperature(-1)); |
eisd | 1:de8c34c9ccdf | 370 | |
eisd | 12:eec17d54a646 | 371 | Thread t_sleep_loop(&sleep_loop); |
eisd | 6:3482f1e19d71 | 372 | bool read = false; |
eisd | 1:de8c34c9ccdf | 373 | while(1) { |
eisd | 7:f720032c2fb9 | 374 | if (sender) { |
eisd | 16:3490cdea4986 | 375 | if (central_time.read() > 20) { |
eisd | 16:3490cdea4986 | 376 | #if DEBUG |
eisd | 16:3490cdea4986 | 377 | pc.printf("snooze tx %f\r\n", central_time.read()); |
eisd | 16:3490cdea4986 | 378 | g = 0; Thread::wait(10); g = 1; Thread::wait(10); |
eisd | 16:3490cdea4986 | 379 | #endif |
eisd | 16:3490cdea4986 | 380 | Thread::wait(10); |
eisd | 16:3490cdea4986 | 381 | WakeUp::set(1); |
eisd | 16:3490cdea4986 | 382 | deepsleep(); |
eisd | 16:3490cdea4986 | 383 | nun.Read(&n->X, &n->Y, &n->aX, &n->aY, &n->aZ, &n->C, &n->Z); |
eisd | 16:3490cdea4986 | 384 | Thread::wait(10); |
eisd | 16:3490cdea4986 | 385 | #if DEBUG |
eisd | 16:3490cdea4986 | 386 | r = 0; Thread::wait(10); r = 1; Thread::wait(10); |
eisd | 16:3490cdea4986 | 387 | pc.printf("unsnooze tx\r\n"); |
eisd | 16:3490cdea4986 | 388 | #endif |
eisd | 16:3490cdea4986 | 389 | } |
eisd | 5:7af5760d7e8f | 390 | read = nun.Read(&n->X, &n->Y, &n->aX, &n->aY, &n->aZ, &n->C, &n->Z); |
eisd | 7:f720032c2fb9 | 391 | n->sum = 0; |
eisd | 7:f720032c2fb9 | 392 | n->sum = calculate_crc8((char*)n, sizeof(struct nunchuk)); |
eisd | 7:f720032c2fb9 | 393 | } |
eisd | 12:eec17d54a646 | 394 | else if (rx_snoozed() && radio.receiveDone()) { |
eisd | 13:ab524cb4f768 | 395 | rx_last_contact.reset(); |
eisd | 12:eec17d54a646 | 396 | rx_snooze.stop(); |
eisd | 12:eec17d54a646 | 397 | rx_to_snooze = true; |
eisd | 12:eec17d54a646 | 398 | pc.printf("rssi %d\r\n", radio.RSSI); |
eisd | 4:c9711f0cd097 | 399 | read = (radio.DATALEN == sizeof(struct nunchuk)); |
eisd | 5:7af5760d7e8f | 400 | if (read) { |
eisd | 6:3482f1e19d71 | 401 | memcpy((void*)next, (const void*)radio.DATA, radio.DATALEN); |
eisd | 7:f720032c2fb9 | 402 | uint8_t sum = next->sum; |
eisd | 7:f720032c2fb9 | 403 | next->sum = 0; |
eisd | 7:f720032c2fb9 | 404 | read = sum == calculate_crc8((char*)next, sizeof(struct nunchuk)); |
eisd | 7:f720032c2fb9 | 405 | if (read) { |
eisd | 7:f720032c2fb9 | 406 | nunchuk *last = n; |
eisd | 7:f720032c2fb9 | 407 | n = next; |
eisd | 7:f720032c2fb9 | 408 | next = last; |
eisd | 7:f720032c2fb9 | 409 | } |
eisd | 7:f720032c2fb9 | 410 | } |
eisd | 7:f720032c2fb9 | 411 | if (!read) |
eisd | 7:f720032c2fb9 | 412 | pc.printf("len %d\r\n", radio.DATALEN); |
eisd | 12:eec17d54a646 | 413 | } else if (rx_to_snooze) { |
eisd | 13:ab524cb4f768 | 414 | rx_snooze.start(200); |
eisd | 12:eec17d54a646 | 415 | rx_to_snooze = false; |
eisd | 4:c9711f0cd097 | 416 | } |
eisd | 1:de8c34c9ccdf | 417 | if(read) |
eisd | 1:de8c34c9ccdf | 418 | { |
eisd | 7:f720032c2fb9 | 419 | Siren_state(n->C); |
eisd | 8:5f933580b560 | 420 | Headlight_state(n->C, n->Z); |
eisd | 5:7af5760d7e8f | 421 | float x = n->X - 128, y = n->Y - 128; |
eisd | 3:9091adbed369 | 422 | float R = x*x + y*y, p = atan2(y, x) * 4 / M_PI - 0.5; |
eisd | 1:de8c34c9ccdf | 423 | int c = 0; |
eisd | 3:9091adbed369 | 424 | if (p > -4) c = 0; |
eisd | 3:9091adbed369 | 425 | if (p > -3) c = 1; |
eisd | 3:9091adbed369 | 426 | if (p > -2) c = 2; |
eisd | 3:9091adbed369 | 427 | if (p > -1) c = 3; |
eisd | 3:9091adbed369 | 428 | if (p > 0) c = 4; |
eisd | 3:9091adbed369 | 429 | if (p > 1) c = 5; |
eisd | 3:9091adbed369 | 430 | if (p > 2) c = 6; |
eisd | 3:9091adbed369 | 431 | if (p > 3) c = 7; |
eisd | 5:7af5760d7e8f | 432 | direction = c; |
eisd | 1:de8c34c9ccdf | 433 | |
eisd | 14:579137c8ceac | 434 | #if DEBUG > 1 |
eisd | 6:3482f1e19d71 | 435 | pc.printf("%d: ", sizeof(struct nunchuk)); |
eisd | 6:3482f1e19d71 | 436 | pc.printf("x%3d y%3d c%1d z%1d --", n->X, n->Y, n->C, n->Z); |
eisd | 6:3482f1e19d71 | 437 | pc.printf("x%d y%d z%d -- %.3f %s \r\n", n->aX, n->aY, n->aZ, R, direction);//s[c]); |
eisd | 3:9091adbed369 | 438 | |
eisd | 3:9091adbed369 | 439 | //radio.send(GATEWAY_ID, (const void*)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 50, false); |
eisd | 3:9091adbed369 | 440 | #endif |
eisd | 16:3490cdea4986 | 441 | if (sender) |
eisd | 16:3490cdea4986 | 442 | if (central_time.read() < 10) |
eisd | 16:3490cdea4986 | 443 | radio.send(GATEWAY_ID, (const void*)n, sizeof(struct nunchuk), false); |
eisd | 16:3490cdea4986 | 444 | else |
eisd | 16:3490cdea4986 | 445 | radio.sleep(); |
eisd | 3:9091adbed369 | 446 | |
eisd | 1:de8c34c9ccdf | 447 | #ifdef TARGET_KL25Z |
eisd | 3:9091adbed369 | 448 | if (R < 20) { |
eisd | 3:9091adbed369 | 449 | if (!central) { |
eisd | 3:9091adbed369 | 450 | pc.printf("central\r\n"); |
eisd | 3:9091adbed369 | 451 | central = true; |
eisd | 3:9091adbed369 | 452 | } |
eisd | 15:8888e36afe43 | 453 | if (n->C || n->Z) |
eisd | 15:8888e36afe43 | 454 | central_time.reset(); |
eisd | 1:de8c34c9ccdf | 455 | r = 1.0f; |
eisd | 1:de8c34c9ccdf | 456 | g = 1.0f; |
eisd | 1:de8c34c9ccdf | 457 | b = 1.0f; |
eisd | 1:de8c34c9ccdf | 458 | } else { |
eisd | 15:8888e36afe43 | 459 | central_time.reset(); |
eisd | 3:9091adbed369 | 460 | if (central) { |
eisd | 6:3482f1e19d71 | 461 | pc.printf("go %s\r\n", directions[direction]); |
eisd | 3:9091adbed369 | 462 | central = false; |
eisd | 3:9091adbed369 | 463 | } |
eisd | 3:9091adbed369 | 464 | R = R/20000; |
eisd | 1:de8c34c9ccdf | 465 | float pal[8][3] = { |
eisd | 1:de8c34c9ccdf | 466 | { 0, 0, 1 }, |
eisd | 1:de8c34c9ccdf | 467 | { 0, 1, 1 }, |
eisd | 1:de8c34c9ccdf | 468 | { 0, 1, 0 }, |
eisd | 1:de8c34c9ccdf | 469 | { 1, 1, 0 }, |
eisd | 1:de8c34c9ccdf | 470 | { 1, 0.5, 0 }, |
eisd | 1:de8c34c9ccdf | 471 | { 1, 0, 0 }, |
eisd | 1:de8c34c9ccdf | 472 | { 1, 0, 1 }, |
eisd | 1:de8c34c9ccdf | 473 | { 0.5, 0, 1 }, |
eisd | 1:de8c34c9ccdf | 474 | }; |
eisd | 6:3482f1e19d71 | 475 | c = (c + 4) % 8; |
eisd | 1:de8c34c9ccdf | 476 | r = 1.0f - pal[c][0] * R; |
eisd | 1:de8c34c9ccdf | 477 | g = 1.0f - pal[c][1] * R; |
eisd | 1:de8c34c9ccdf | 478 | b = 1.0f - pal[c][2] * R; |
eisd | 1:de8c34c9ccdf | 479 | } |
eisd | 1:de8c34c9ccdf | 480 | #endif |
eisd | 1:de8c34c9ccdf | 481 | } |
eisd | 4:c9711f0cd097 | 482 | else if (sender) |
eisd | 1:de8c34c9ccdf | 483 | { |
eisd | 1:de8c34c9ccdf | 484 | pc.printf("Error\r\n"); |
eisd | 3:9091adbed369 | 485 | return 0; |
eisd | 1:de8c34c9ccdf | 486 | } |
eisd | 4:c9711f0cd097 | 487 | if (sender) |
eisd | 4:c9711f0cd097 | 488 | wait(0.01); |
eisd | 5:7af5760d7e8f | 489 | else |
eisd | 5:7af5760d7e8f | 490 | Thread::wait(10); |
eisd | 1:de8c34c9ccdf | 491 | } |
eisd | 1:de8c34c9ccdf | 492 | } |