Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp.orig@9:2f23704d4a47, 2015-05-11 (annotated)
- Committer:
- martydd3
- Date:
- Mon May 11 19:11:56 2015 +0000
- Revision:
- 9:2f23704d4a47
5/11/2015
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| martydd3 | 9:2f23704d4a47 | 1 | #include "mbed.h" |
| martydd3 | 9:2f23704d4a47 | 2 | #include "MRF24J40.h" |
| martydd3 | 9:2f23704d4a47 | 3 | |
| martydd3 | 9:2f23704d4a47 | 4 | #include <string> |
| martydd3 | 9:2f23704d4a47 | 5 | #include <time.h> |
| martydd3 | 9:2f23704d4a47 | 6 | |
| martydd3 | 9:2f23704d4a47 | 7 | #include "constants.h" |
| martydd3 | 9:2f23704d4a47 | 8 | #include "shape_drawer.h" |
| martydd3 | 9:2f23704d4a47 | 9 | |
| martydd3 | 9:2f23704d4a47 | 10 | // RF tranceiver to link with handheld. |
| martydd3 | 9:2f23704d4a47 | 11 | MRF24J40 mrf(p11, p12, p13, p14, p26); |
| martydd3 | 9:2f23704d4a47 | 12 | |
| martydd3 | 9:2f23704d4a47 | 13 | // LEDs you can treat these as variables (led2 = 1 will turn led2 on!) |
| martydd3 | 9:2f23704d4a47 | 14 | DigitalOut led1(LED1); |
| martydd3 | 9:2f23704d4a47 | 15 | DigitalOut led2(LED2); |
| martydd3 | 9:2f23704d4a47 | 16 | DigitalOut led3(LED3); |
| martydd3 | 9:2f23704d4a47 | 17 | DigitalOut led4(LED4); |
| martydd3 | 9:2f23704d4a47 | 18 | |
| martydd3 | 9:2f23704d4a47 | 19 | // Timer |
| martydd3 | 9:2f23704d4a47 | 20 | Timer timer; |
| martydd3 | 9:2f23704d4a47 | 21 | |
| martydd3 | 9:2f23704d4a47 | 22 | // Serial port for showing RX data. |
| martydd3 | 9:2f23704d4a47 | 23 | Serial pc(USBTX, USBRX); |
| martydd3 | 9:2f23704d4a47 | 24 | |
| martydd3 | 9:2f23704d4a47 | 25 | // Used for sending and receiving |
| martydd3 | 9:2f23704d4a47 | 26 | char txBuffer[128]; |
| martydd3 | 9:2f23704d4a47 | 27 | char rxBuffer[128]; |
| martydd3 | 9:2f23704d4a47 | 28 | int rxLen; |
| martydd3 | 9:2f23704d4a47 | 29 | |
| martydd3 | 9:2f23704d4a47 | 30 | //***************** Do not change these methods (please) *****************// |
| martydd3 | 9:2f23704d4a47 | 31 | |
| martydd3 | 9:2f23704d4a47 | 32 | /** |
| martydd3 | 9:2f23704d4a47 | 33 | * Receive data from the MRF24J40. |
| martydd3 | 9:2f23704d4a47 | 34 | * |
| martydd3 | 9:2f23704d4a47 | 35 | * @param data A pointer to a char array to hold the data |
| martydd3 | 9:2f23704d4a47 | 36 | * @param maxLength The max amount of data to read. |
| martydd3 | 9:2f23704d4a47 | 37 | */ |
| martydd3 | 9:2f23704d4a47 | 38 | int rf_receive(char *data, uint8_t maxLength) |
| martydd3 | 9:2f23704d4a47 | 39 | { |
| martydd3 | 9:2f23704d4a47 | 40 | uint8_t len = mrf.Receive((uint8_t *)data, maxLength); |
| martydd3 | 9:2f23704d4a47 | 41 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
| martydd3 | 9:2f23704d4a47 | 42 | |
| martydd3 | 9:2f23704d4a47 | 43 | if(len > 10) { |
| martydd3 | 9:2f23704d4a47 | 44 | //Remove the header and footer of the message |
| martydd3 | 9:2f23704d4a47 | 45 | for(uint8_t i = 0; i < len-2; i++) { |
| martydd3 | 9:2f23704d4a47 | 46 | if(i<8) { |
| martydd3 | 9:2f23704d4a47 | 47 | //Make sure our header is valid first |
| martydd3 | 9:2f23704d4a47 | 48 | if(data[i] != header[i]) |
| martydd3 | 9:2f23704d4a47 | 49 | return 0; |
| martydd3 | 9:2f23704d4a47 | 50 | } else { |
| martydd3 | 9:2f23704d4a47 | 51 | data[i-8] = data[i]; |
| martydd3 | 9:2f23704d4a47 | 52 | } |
| martydd3 | 9:2f23704d4a47 | 53 | } |
| martydd3 | 9:2f23704d4a47 | 54 | |
| martydd3 | 9:2f23704d4a47 | 55 | //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); |
| martydd3 | 9:2f23704d4a47 | 56 | } |
| martydd3 | 9:2f23704d4a47 | 57 | return ((int)len)-10; |
| martydd3 | 9:2f23704d4a47 | 58 | } |
| martydd3 | 9:2f23704d4a47 | 59 | |
| martydd3 | 9:2f23704d4a47 | 60 | /** |
| martydd3 | 9:2f23704d4a47 | 61 | * Send data to another MRF24J40. |
| martydd3 | 9:2f23704d4a47 | 62 | * |
| martydd3 | 9:2f23704d4a47 | 63 | * @param data The string to send |
| martydd3 | 9:2f23704d4a47 | 64 | * @param maxLength The length of the data to send. |
| martydd3 | 9:2f23704d4a47 | 65 | * If you are sending a null-terminated string you can pass strlen(data)+1 |
| martydd3 | 9:2f23704d4a47 | 66 | */ |
| martydd3 | 9:2f23704d4a47 | 67 | void rf_send(char *data, uint8_t len) |
| martydd3 | 9:2f23704d4a47 | 68 | { |
| martydd3 | 9:2f23704d4a47 | 69 | //We need to prepend the message with a valid ZigBee header |
| martydd3 | 9:2f23704d4a47 | 70 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
| martydd3 | 9:2f23704d4a47 | 71 | uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) ); |
| martydd3 | 9:2f23704d4a47 | 72 | |
| martydd3 | 9:2f23704d4a47 | 73 | for(uint8_t i = 0; i < len+8; i++) { |
| martydd3 | 9:2f23704d4a47 | 74 | //prepend the 8-byte header |
| martydd3 | 9:2f23704d4a47 | 75 | send_buf[i] = (i<8) ? header[i] : data[i-8]; |
| martydd3 | 9:2f23704d4a47 | 76 | } |
| martydd3 | 9:2f23704d4a47 | 77 | //pc.printf("Sent: %s\r\n", send_buf+8); |
| martydd3 | 9:2f23704d4a47 | 78 | |
| martydd3 | 9:2f23704d4a47 | 79 | mrf.Send(send_buf, len+8); |
| martydd3 | 9:2f23704d4a47 | 80 | free(send_buf); |
| martydd3 | 9:2f23704d4a47 | 81 | } |
| martydd3 | 9:2f23704d4a47 | 82 | |
| martydd3 | 9:2f23704d4a47 | 83 | /*******************************************************************/ |
| martydd3 | 9:2f23704d4a47 | 84 | // Display interrupt and drivers |
| martydd3 | 9:2f23704d4a47 | 85 | |
| martydd3 | 9:2f23704d4a47 | 86 | int frame_id = 0; |
| martydd3 | 9:2f23704d4a47 | 87 | char frame_buffer1[SLICES][WIDTH]; |
| martydd3 | 9:2f23704d4a47 | 88 | char frame_buffer2[SLICES][WIDTH]; |
| martydd3 | 9:2f23704d4a47 | 89 | |
| martydd3 | 9:2f23704d4a47 | 90 | int slice_i = 100; |
| martydd3 | 9:2f23704d4a47 | 91 | BusOut blade(p15, p16, p17, p18, p19, p20, p21, p22); |
| martydd3 | 9:2f23704d4a47 | 92 | DigitalOut clk(p23); |
| martydd3 | 9:2f23704d4a47 | 93 | DigitalOut disp(p24); |
| martydd3 | 9:2f23704d4a47 | 94 | Ticker pixel_ticker; |
| martydd3 | 9:2f23704d4a47 | 95 | |
| martydd3 | 9:2f23704d4a47 | 96 | void push_pixels(){ |
| martydd3 | 9:2f23704d4a47 | 97 | for(int j = 8; j < WIDTH; j++){ |
| martydd3 | 9:2f23704d4a47 | 98 | if(frame_id == 0) |
| martydd3 | 9:2f23704d4a47 | 99 | blade = frame_buffer1[slice_i][j]; |
| martydd3 | 9:2f23704d4a47 | 100 | else |
| martydd3 | 9:2f23704d4a47 | 101 | blade = frame_buffer2[slice_i][j]; |
| martydd3 | 9:2f23704d4a47 | 102 | |
| martydd3 | 9:2f23704d4a47 | 103 | clk = 1; |
| martydd3 | 9:2f23704d4a47 | 104 | clk = 0; |
| martydd3 | 9:2f23704d4a47 | 105 | } |
| martydd3 | 9:2f23704d4a47 | 106 | |
| martydd3 | 9:2f23704d4a47 | 107 | for(int j = 7; j >= 0; j--){ |
| martydd3 | 9:2f23704d4a47 | 108 | if(frame_id == 0) |
| martydd3 | 9:2f23704d4a47 | 109 | blade = frame_buffer1[slice_i][j]; |
| martydd3 | 9:2f23704d4a47 | 110 | else |
| martydd3 | 9:2f23704d4a47 | 111 | blade = frame_buffer2[slice_i][j]; |
| martydd3 | 9:2f23704d4a47 | 112 | |
| martydd3 | 9:2f23704d4a47 | 113 | clk = 1; |
| martydd3 | 9:2f23704d4a47 | 114 | clk = 0; |
| martydd3 | 9:2f23704d4a47 | 115 | } |
| martydd3 | 9:2f23704d4a47 | 116 | |
| martydd3 | 9:2f23704d4a47 | 117 | disp = 1; |
| martydd3 | 9:2f23704d4a47 | 118 | disp = 0; |
| martydd3 | 9:2f23704d4a47 | 119 | |
| martydd3 | 9:2f23704d4a47 | 120 | slice_i = (slice_i + 1)%SLICES; |
| martydd3 | 9:2f23704d4a47 | 121 | } |
| martydd3 | 9:2f23704d4a47 | 122 | |
| martydd3 | 9:2f23704d4a47 | 123 | //Hall sensor interupt |
| martydd3 | 9:2f23704d4a47 | 124 | double rotate_time; |
| martydd3 | 9:2f23704d4a47 | 125 | double slice_time; |
| martydd3 | 9:2f23704d4a47 | 126 | |
| martydd3 | 9:2f23704d4a47 | 127 | Timer hall_timer; |
| martydd3 | 9:2f23704d4a47 | 128 | |
| martydd3 | 9:2f23704d4a47 | 129 | void rotate_sense(){ |
| martydd3 | 9:2f23704d4a47 | 130 | static bool firstTime = false; |
| martydd3 | 9:2f23704d4a47 | 131 | |
| martydd3 | 9:2f23704d4a47 | 132 | if (firstTime){ |
| martydd3 | 9:2f23704d4a47 | 133 | hall_timer.reset(); |
| martydd3 | 9:2f23704d4a47 | 134 | hall_timer.start(); |
| martydd3 | 9:2f23704d4a47 | 135 | firstTime = false; |
| martydd3 | 9:2f23704d4a47 | 136 | return; |
| martydd3 | 9:2f23704d4a47 | 137 | } |
| martydd3 | 9:2f23704d4a47 | 138 | |
| martydd3 | 9:2f23704d4a47 | 139 | rotate_time = hall_timer.read_us(); |
| martydd3 | 9:2f23704d4a47 | 140 | hall_timer.reset(); |
| martydd3 | 9:2f23704d4a47 | 141 | hall_timer.start(); |
| martydd3 | 9:2f23704d4a47 | 142 | |
| martydd3 | 9:2f23704d4a47 | 143 | slice_time = (double) rotate_time/SLICES; |
| martydd3 | 9:2f23704d4a47 | 144 | slice_i = 0; |
| martydd3 | 9:2f23704d4a47 | 145 | pixel_ticker.attach_us(&push_pixels, slice_time); |
| martydd3 | 9:2f23704d4a47 | 146 | } |
| martydd3 | 9:2f23704d4a47 | 147 | |
| martydd3 | 9:2f23704d4a47 | 148 | /***************************************************************/ |
| martydd3 | 9:2f23704d4a47 | 149 | //code to adjust for offset blades and vertical alignment |
| martydd3 | 9:2f23704d4a47 | 150 | |
| martydd3 | 9:2f23704d4a47 | 151 | void convert_array(){ |
| martydd3 | 9:2f23704d4a47 | 152 | if(frame_id == 0){ |
| martydd3 | 9:2f23704d4a47 | 153 | move_buffer(&frame_buffer2); |
| martydd3 | 9:2f23704d4a47 | 154 | frame_id = 1; |
| martydd3 | 9:2f23704d4a47 | 155 | } else { |
| martydd3 | 9:2f23704d4a47 | 156 | move_buffer(&frame_buffer1); |
| martydd3 | 9:2f23704d4a47 | 157 | frame_id = 0; |
| martydd3 | 9:2f23704d4a47 | 158 | } |
| martydd3 | 9:2f23704d4a47 | 159 | } |
| martydd3 | 9:2f23704d4a47 | 160 | |
| martydd3 | 9:2f23704d4a47 | 161 | int display_mode = 0; |
| martydd3 | 9:2f23704d4a47 | 162 | Ticker animate_ticker; |
| martydd3 | 9:2f23704d4a47 | 163 | |
| martydd3 | 9:2f23704d4a47 | 164 | int height(float d){ |
| martydd3 | 9:2f23704d4a47 | 165 | return 0; |
| martydd3 | 9:2f23704d4a47 | 166 | } |
| martydd3 | 9:2f23704d4a47 | 167 | |
| martydd3 | 9:2f23704d4a47 | 168 | float freq = 4; |
| martydd3 | 9:2f23704d4a47 | 169 | float freq_disp = 0.0; |
| martydd3 | 9:2f23704d4a47 | 170 | int wave_height(float d){ |
| martydd3 | 9:2f23704d4a47 | 171 | float h = 3.5*sin(2.0 * M_PI * (d + freq_disp) * freq) + 3.5; |
| martydd3 | 9:2f23704d4a47 | 172 | return (int)rint(h); |
| martydd3 | 9:2f23704d4a47 | 173 | } |
| martydd3 | 9:2f23704d4a47 | 174 | |
| martydd3 | 9:2f23704d4a47 | 175 | void animate(){ |
| martydd3 | 9:2f23704d4a47 | 176 | |
| martydd3 | 9:2f23704d4a47 | 177 | if(display_mode == 0) |
| martydd3 | 9:2f23704d4a47 | 178 | { |
| martydd3 | 9:2f23704d4a47 | 179 | set_hfunc(&wave_height); |
| martydd3 | 9:2f23704d4a47 | 180 | freq = 4; |
| martydd3 | 9:2f23704d4a47 | 181 | |
| martydd3 | 9:2f23704d4a47 | 182 | for(int i = 3; i < WIDTH; i++){ |
| martydd3 | 9:2f23704d4a47 | 183 | draw_circle(i, 4); |
| martydd3 | 9:2f23704d4a47 | 184 | } |
| martydd3 | 9:2f23704d4a47 | 185 | |
| martydd3 | 9:2f23704d4a47 | 186 | freq_disp += 0.1; |
| martydd3 | 9:2f23704d4a47 | 187 | if(freq_disp >= 1.0){ |
| martydd3 | 9:2f23704d4a47 | 188 | freq_disp = 0.0; |
| martydd3 | 9:2f23704d4a47 | 189 | } |
| martydd3 | 9:2f23704d4a47 | 190 | } |
| martydd3 | 9:2f23704d4a47 | 191 | else if(display_mode == 1) |
| martydd3 | 9:2f23704d4a47 | 192 | { |
| martydd3 | 9:2f23704d4a47 | 193 | set_hfunc(NULL); |
| martydd3 | 9:2f23704d4a47 | 194 | draw_line(10, 10, 0, 10, -10, 0); |
| martydd3 | 9:2f23704d4a47 | 195 | draw_line(10, -10, 0, -10, -10, 0); |
| martydd3 | 9:2f23704d4a47 | 196 | draw_line(-10, -10, 0, -10, 10, 0); |
| martydd3 | 9:2f23704d4a47 | 197 | draw_line(-10, 10, 0, 10, 10, 0); |
| martydd3 | 9:2f23704d4a47 | 198 | |
| martydd3 | 9:2f23704d4a47 | 199 | draw_line(10, 10, 7, 10, -10, 7); |
| martydd3 | 9:2f23704d4a47 | 200 | draw_line(10, -10, 7, -10, -10, 7); |
| martydd3 | 9:2f23704d4a47 | 201 | draw_line(-10, -10, 7, -10, 10, 7); |
| martydd3 | 9:2f23704d4a47 | 202 | draw_line(-10, 10, 7, 10, 10, 7); |
| martydd3 | 9:2f23704d4a47 | 203 | |
| martydd3 | 9:2f23704d4a47 | 204 | draw_line(10, 10, 0, 10, 10, 7); |
| martydd3 | 9:2f23704d4a47 | 205 | draw_line(10, -10, 0, 10, -10, 7); |
| martydd3 | 9:2f23704d4a47 | 206 | draw_line(-10, -10, 0, -10, -10, 7); |
| martydd3 | 9:2f23704d4a47 | 207 | draw_line(-10, 10, 0, -10, 10, 7); |
| martydd3 | 9:2f23704d4a47 | 208 | |
| martydd3 | 9:2f23704d4a47 | 209 | draw_line(5, 5, 2, 5, -5, 2); |
| martydd3 | 9:2f23704d4a47 | 210 | draw_line(5, -5, 2, -5, -5, 2); |
| martydd3 | 9:2f23704d4a47 | 211 | draw_line(-5, -5, 2, -5, 5, 2); |
| martydd3 | 9:2f23704d4a47 | 212 | draw_line(-5, 5, 2, 5, 5, 2); |
| martydd3 | 9:2f23704d4a47 | 213 | |
| martydd3 | 9:2f23704d4a47 | 214 | draw_line(5, 5, 5, 5, -5, 5); |
| martydd3 | 9:2f23704d4a47 | 215 | draw_line(5, -5, 5, -5, -5, 5); |
| martydd3 | 9:2f23704d4a47 | 216 | draw_line(-5, -5, 5, -5, 5, 5); |
| martydd3 | 9:2f23704d4a47 | 217 | draw_line(-5, 5, 5, 5, 5, 5); |
| martydd3 | 9:2f23704d4a47 | 218 | |
| martydd3 | 9:2f23704d4a47 | 219 | draw_line(5, 5, 2, 5, 5, 5); |
| martydd3 | 9:2f23704d4a47 | 220 | draw_line(5, -5, 2, 5, -5, 5); |
| martydd3 | 9:2f23704d4a47 | 221 | draw_line(-5, -5, 2, -5, -5, 5); |
| martydd3 | 9:2f23704d4a47 | 222 | draw_line(-5, 5, 2, -5, 5, 5); |
| martydd3 | 9:2f23704d4a47 | 223 | |
| martydd3 | 9:2f23704d4a47 | 224 | } |
| martydd3 | 9:2f23704d4a47 | 225 | |
| martydd3 | 9:2f23704d4a47 | 226 | else if(display_mode == 2){ |
| martydd3 | 9:2f23704d4a47 | 227 | freq = 1.5; |
| martydd3 | 9:2f23704d4a47 | 228 | set_hfunc(&wave_height); |
| martydd3 | 9:2f23704d4a47 | 229 | |
| martydd3 | 9:2f23704d4a47 | 230 | |
| martydd3 | 9:2f23704d4a47 | 231 | freq_disp += 0.05; |
| martydd3 | 9:2f23704d4a47 | 232 | if(freq_disp >= 1.0){ |
| martydd3 | 9:2f23704d4a47 | 233 | freq_disp = 0.0; |
| martydd3 | 9:2f23704d4a47 | 234 | } |
| martydd3 | 9:2f23704d4a47 | 235 | |
| martydd3 | 9:2f23704d4a47 | 236 | for(int i = -12; i < 13; i += 1){ |
| martydd3 | 9:2f23704d4a47 | 237 | draw_line(i, 16, 0, i, -16, 0); |
| martydd3 | 9:2f23704d4a47 | 238 | //draw_line(16, i, 0, -16, i, 0); |
| martydd3 | 9:2f23704d4a47 | 239 | } |
| martydd3 | 9:2f23704d4a47 | 240 | } |
| martydd3 | 9:2f23704d4a47 | 241 | |
| martydd3 | 9:2f23704d4a47 | 242 | convert_array(); |
| martydd3 | 9:2f23704d4a47 | 243 | } |
| martydd3 | 9:2f23704d4a47 | 244 | |
| martydd3 | 9:2f23704d4a47 | 245 | bool animate_i = false; |
| martydd3 | 9:2f23704d4a47 | 246 | void animate_int(){ |
| martydd3 | 9:2f23704d4a47 | 247 | animate_i = true; |
| martydd3 | 9:2f23704d4a47 | 248 | } |
| martydd3 | 9:2f23704d4a47 | 249 | |
| martydd3 | 9:2f23704d4a47 | 250 | Ticker switch_ticker; |
| martydd3 | 9:2f23704d4a47 | 251 | |
| martydd3 | 9:2f23704d4a47 | 252 | void switch_mode(){ |
| martydd3 | 9:2f23704d4a47 | 253 | display_mode = (display_mode + 1) % 3; |
| martydd3 | 9:2f23704d4a47 | 254 | } |
| martydd3 | 9:2f23704d4a47 | 255 | |
| martydd3 | 9:2f23704d4a47 | 256 | int main (void) |
| martydd3 | 9:2f23704d4a47 | 257 | { |
| martydd3 | 9:2f23704d4a47 | 258 | display_mode = 4; |
| martydd3 | 9:2f23704d4a47 | 259 | |
| martydd3 | 9:2f23704d4a47 | 260 | InterruptIn hall_pin(p25); |
| martydd3 | 9:2f23704d4a47 | 261 | hall_pin.fall(&rotate_sense); |
| martydd3 | 9:2f23704d4a47 | 262 | animate_ticker.attach(&animate_int, 1.0); |
| martydd3 | 9:2f23704d4a47 | 263 | switch_ticker.attach(&switch_mode, 15.0); |
| martydd3 | 9:2f23704d4a47 | 264 | |
| martydd3 | 9:2f23704d4a47 | 265 | uint8_t channel = 2; |
| martydd3 | 9:2f23704d4a47 | 266 | |
| martydd3 | 9:2f23704d4a47 | 267 | //Set the Channel. 0 is default, 15 is max |
| martydd3 | 9:2f23704d4a47 | 268 | mrf.SetChannel(channel); |
| martydd3 | 9:2f23704d4a47 | 269 | |
| martydd3 | 9:2f23704d4a47 | 270 | while(true) { |
| martydd3 | 9:2f23704d4a47 | 271 | if(animate_i){ |
| martydd3 | 9:2f23704d4a47 | 272 | animate(); |
| martydd3 | 9:2f23704d4a47 | 273 | } |
| martydd3 | 9:2f23704d4a47 | 274 | } |
| martydd3 | 9:2f23704d4a47 | 275 | } |