Martin Deng
/
mbed_shapedriver
Screensaver
main.cpp@5:495d64d8934d, 2015-05-01 (annotated)
- Committer:
- martydd3
- Date:
- Fri May 01 05:25:19 2015 +0000
- Revision:
- 5:495d64d8934d
- Parent:
- 4:75498bd2e742
- Child:
- 6:baece3338bfe
Moved all drawing to primative functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martydd3 | 0:b654a906fb40 | 1 | #include "mbed.h" |
martydd3 | 0:b654a906fb40 | 2 | #include "MRF24J40.h" |
martydd3 | 0:b654a906fb40 | 3 | |
martydd3 | 0:b654a906fb40 | 4 | #include <string> |
martydd3 | 0:b654a906fb40 | 5 | #include "constants.h" |
martydd3 | 5:495d64d8934d | 6 | #include "shape_drawer.h" |
martydd3 | 0:b654a906fb40 | 7 | |
martydd3 | 0:b654a906fb40 | 8 | // RF tranceiver to link with handheld. |
martydd3 | 0:b654a906fb40 | 9 | MRF24J40 mrf(p11, p12, p13, p14, p26); |
martydd3 | 0:b654a906fb40 | 10 | |
martydd3 | 0:b654a906fb40 | 11 | // LEDs you can treat these as variables (led2 = 1 will turn led2 on!) |
martydd3 | 0:b654a906fb40 | 12 | DigitalOut led1(LED1); |
martydd3 | 0:b654a906fb40 | 13 | DigitalOut led2(LED2); |
martydd3 | 0:b654a906fb40 | 14 | DigitalOut led3(LED3); |
martydd3 | 0:b654a906fb40 | 15 | DigitalOut led4(LED4); |
martydd3 | 0:b654a906fb40 | 16 | |
martydd3 | 0:b654a906fb40 | 17 | // Timer |
martydd3 | 0:b654a906fb40 | 18 | Timer timer; |
martydd3 | 0:b654a906fb40 | 19 | |
martydd3 | 0:b654a906fb40 | 20 | // Serial port for showing RX data. |
martydd3 | 0:b654a906fb40 | 21 | Serial pc(USBTX, USBRX); |
martydd3 | 0:b654a906fb40 | 22 | |
martydd3 | 0:b654a906fb40 | 23 | // Used for sending and receiving |
martydd3 | 0:b654a906fb40 | 24 | char txBuffer[128]; |
martydd3 | 0:b654a906fb40 | 25 | char rxBuffer[128]; |
martydd3 | 0:b654a906fb40 | 26 | int rxLen; |
martydd3 | 0:b654a906fb40 | 27 | |
martydd3 | 0:b654a906fb40 | 28 | //***************** Do not change these methods (please) *****************// |
martydd3 | 0:b654a906fb40 | 29 | |
martydd3 | 0:b654a906fb40 | 30 | /** |
martydd3 | 0:b654a906fb40 | 31 | * Receive data from the MRF24J40. |
martydd3 | 0:b654a906fb40 | 32 | * |
martydd3 | 0:b654a906fb40 | 33 | * @param data A pointer to a char array to hold the data |
martydd3 | 0:b654a906fb40 | 34 | * @param maxLength The max amount of data to read. |
martydd3 | 0:b654a906fb40 | 35 | */ |
martydd3 | 0:b654a906fb40 | 36 | int rf_receive(char *data, uint8_t maxLength) |
martydd3 | 0:b654a906fb40 | 37 | { |
martydd3 | 0:b654a906fb40 | 38 | uint8_t len = mrf.Receive((uint8_t *)data, maxLength); |
martydd3 | 0:b654a906fb40 | 39 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
martydd3 | 0:b654a906fb40 | 40 | |
martydd3 | 0:b654a906fb40 | 41 | if(len > 10) { |
martydd3 | 0:b654a906fb40 | 42 | //Remove the header and footer of the message |
martydd3 | 0:b654a906fb40 | 43 | for(uint8_t i = 0; i < len-2; i++) { |
martydd3 | 0:b654a906fb40 | 44 | if(i<8) { |
martydd3 | 0:b654a906fb40 | 45 | //Make sure our header is valid first |
martydd3 | 0:b654a906fb40 | 46 | if(data[i] != header[i]) |
martydd3 | 0:b654a906fb40 | 47 | return 0; |
martydd3 | 0:b654a906fb40 | 48 | } else { |
martydd3 | 0:b654a906fb40 | 49 | data[i-8] = data[i]; |
martydd3 | 0:b654a906fb40 | 50 | } |
martydd3 | 0:b654a906fb40 | 51 | } |
martydd3 | 0:b654a906fb40 | 52 | |
martydd3 | 0:b654a906fb40 | 53 | //pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10); |
martydd3 | 0:b654a906fb40 | 54 | } |
martydd3 | 0:b654a906fb40 | 55 | return ((int)len)-10; |
martydd3 | 0:b654a906fb40 | 56 | } |
martydd3 | 0:b654a906fb40 | 57 | |
martydd3 | 0:b654a906fb40 | 58 | /** |
martydd3 | 0:b654a906fb40 | 59 | * Send data to another MRF24J40. |
martydd3 | 0:b654a906fb40 | 60 | * |
martydd3 | 0:b654a906fb40 | 61 | * @param data The string to send |
martydd3 | 0:b654a906fb40 | 62 | * @param maxLength The length of the data to send. |
martydd3 | 0:b654a906fb40 | 63 | * If you are sending a null-terminated string you can pass strlen(data)+1 |
martydd3 | 0:b654a906fb40 | 64 | */ |
martydd3 | 0:b654a906fb40 | 65 | void rf_send(char *data, uint8_t len) |
martydd3 | 0:b654a906fb40 | 66 | { |
martydd3 | 0:b654a906fb40 | 67 | //We need to prepend the message with a valid ZigBee header |
martydd3 | 0:b654a906fb40 | 68 | uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00}; |
martydd3 | 0:b654a906fb40 | 69 | uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) ); |
martydd3 | 0:b654a906fb40 | 70 | |
martydd3 | 0:b654a906fb40 | 71 | for(uint8_t i = 0; i < len+8; i++) { |
martydd3 | 0:b654a906fb40 | 72 | //prepend the 8-byte header |
martydd3 | 0:b654a906fb40 | 73 | send_buf[i] = (i<8) ? header[i] : data[i-8]; |
martydd3 | 0:b654a906fb40 | 74 | } |
martydd3 | 0:b654a906fb40 | 75 | //pc.printf("Sent: %s\r\n", send_buf+8); |
martydd3 | 0:b654a906fb40 | 76 | |
martydd3 | 0:b654a906fb40 | 77 | mrf.Send(send_buf, len+8); |
martydd3 | 0:b654a906fb40 | 78 | free(send_buf); |
martydd3 | 0:b654a906fb40 | 79 | } |
martydd3 | 0:b654a906fb40 | 80 | |
martydd3 | 2:4b42ccb9df8d | 81 | /*******************************************************************/ |
martydd3 | 2:4b42ccb9df8d | 82 | // Display interrupt and drivers |
martydd3 | 2:4b42ccb9df8d | 83 | |
martydd3 | 2:4b42ccb9df8d | 84 | int frame_id = 0; |
martydd3 | 2:4b42ccb9df8d | 85 | char frame_buffer1[SLICES][WIDTH]; |
martydd3 | 2:4b42ccb9df8d | 86 | char frame_buffer2[SLICES][WIDTH]; |
martydd3 | 0:b654a906fb40 | 87 | char work_buffer[SLICES][WIDTH]; |
martydd3 | 0:b654a906fb40 | 88 | |
martydd3 | 2:4b42ccb9df8d | 89 | int slice_i = 100; |
martydd3 | 0:b654a906fb40 | 90 | BusOut blade(p15, p16, p17, p18, p19, p20, p21, p22); |
martydd3 | 0:b654a906fb40 | 91 | DigitalOut clk(p23); |
martydd3 | 0:b654a906fb40 | 92 | DigitalOut disp(p24); |
martydd3 | 0:b654a906fb40 | 93 | Ticker pixel_ticker; |
martydd3 | 0:b654a906fb40 | 94 | |
martydd3 | 2:4b42ccb9df8d | 95 | void push_pixels(){ |
martydd3 | 2:4b42ccb9df8d | 96 | for(int j = 8; j < WIDTH; j++){ |
martydd3 | 2:4b42ccb9df8d | 97 | if(frame_id == 0) |
martydd3 | 2:4b42ccb9df8d | 98 | blade = frame_buffer1[slice_i][j]; |
martydd3 | 2:4b42ccb9df8d | 99 | else |
martydd3 | 2:4b42ccb9df8d | 100 | blade = frame_buffer2[slice_i][j]; |
martydd3 | 2:4b42ccb9df8d | 101 | |
martydd3 | 2:4b42ccb9df8d | 102 | clk = 1; |
martydd3 | 2:4b42ccb9df8d | 103 | clk = 0; |
martydd3 | 2:4b42ccb9df8d | 104 | } |
martydd3 | 2:4b42ccb9df8d | 105 | |
martydd3 | 2:4b42ccb9df8d | 106 | for(int j = 7; j >= 0; j--){ |
martydd3 | 2:4b42ccb9df8d | 107 | if(frame_id == 0) |
martydd3 | 2:4b42ccb9df8d | 108 | blade = frame_buffer1[slice_i][j]; |
martydd3 | 2:4b42ccb9df8d | 109 | else |
martydd3 | 2:4b42ccb9df8d | 110 | blade = frame_buffer2[slice_i][j]; |
martydd3 | 2:4b42ccb9df8d | 111 | |
martydd3 | 2:4b42ccb9df8d | 112 | clk = 1; |
martydd3 | 2:4b42ccb9df8d | 113 | clk = 0; |
martydd3 | 2:4b42ccb9df8d | 114 | } |
martydd3 | 2:4b42ccb9df8d | 115 | |
martydd3 | 2:4b42ccb9df8d | 116 | disp = 1; |
martydd3 | 2:4b42ccb9df8d | 117 | disp = 0; |
martydd3 | 2:4b42ccb9df8d | 118 | |
martydd3 | 2:4b42ccb9df8d | 119 | slice_i = (slice_i + 1)%SLICES; |
martydd3 | 2:4b42ccb9df8d | 120 | } |
martydd3 | 2:4b42ccb9df8d | 121 | |
martydd3 | 2:4b42ccb9df8d | 122 | //Hall sensor interupt |
martydd3 | 2:4b42ccb9df8d | 123 | double rotate_time; |
martydd3 | 2:4b42ccb9df8d | 124 | double slice_time; |
martydd3 | 2:4b42ccb9df8d | 125 | |
martydd3 | 2:4b42ccb9df8d | 126 | Timer hall_timer; |
martydd3 | 2:4b42ccb9df8d | 127 | |
martydd3 | 2:4b42ccb9df8d | 128 | void rotate_sense(){ |
martydd3 | 2:4b42ccb9df8d | 129 | static bool firstTime = false; |
martydd3 | 2:4b42ccb9df8d | 130 | |
martydd3 | 2:4b42ccb9df8d | 131 | if (firstTime){ |
martydd3 | 2:4b42ccb9df8d | 132 | hall_timer.reset(); |
martydd3 | 2:4b42ccb9df8d | 133 | hall_timer.start(); |
martydd3 | 2:4b42ccb9df8d | 134 | firstTime = false; |
martydd3 | 2:4b42ccb9df8d | 135 | return; |
martydd3 | 2:4b42ccb9df8d | 136 | } |
martydd3 | 2:4b42ccb9df8d | 137 | |
martydd3 | 2:4b42ccb9df8d | 138 | rotate_time = hall_timer.read_us(); |
martydd3 | 2:4b42ccb9df8d | 139 | hall_timer.reset(); |
martydd3 | 2:4b42ccb9df8d | 140 | hall_timer.start(); |
martydd3 | 2:4b42ccb9df8d | 141 | |
martydd3 | 2:4b42ccb9df8d | 142 | slice_time = (double) rotate_time/SLICES; |
martydd3 | 2:4b42ccb9df8d | 143 | slice_i = 0; |
martydd3 | 2:4b42ccb9df8d | 144 | pixel_ticker.attach_us(&push_pixels, slice_time); |
martydd3 | 2:4b42ccb9df8d | 145 | } |
martydd3 | 2:4b42ccb9df8d | 146 | |
martydd3 | 2:4b42ccb9df8d | 147 | /***************************************************************/ |
martydd3 | 2:4b42ccb9df8d | 148 | //code to adjust for offset blades and vertical alignment |
martydd3 | 2:4b42ccb9df8d | 149 | |
martydd3 | 0:b654a906fb40 | 150 | int displaces[HEIGHT]; |
martydd3 | 0:b654a906fb40 | 151 | |
martydd3 | 0:b654a906fb40 | 152 | void convert_array(){ |
martydd3 | 5:495d64d8934d | 153 | if(frame_id == 0){ |
martydd3 | 5:495d64d8934d | 154 | move_buffer(&frame_buffer2); |
martydd3 | 5:495d64d8934d | 155 | frame_id = 1; |
martydd3 | 5:495d64d8934d | 156 | } else { |
martydd3 | 5:495d64d8934d | 157 | move_buffer(&frame_buffer1); |
martydd3 | 5:495d64d8934d | 158 | frame_id = 0; |
martydd3 | 2:4b42ccb9df8d | 159 | } |
martydd3 | 2:4b42ccb9df8d | 160 | } |
martydd3 | 2:4b42ccb9df8d | 161 | |
martydd3 | 2:4b42ccb9df8d | 162 | int display_mode = 0; |
martydd3 | 2:4b42ccb9df8d | 163 | Ticker animate_ticker; |
martydd3 | 2:4b42ccb9df8d | 164 | void animate(){ |
martydd3 | 2:4b42ccb9df8d | 165 | |
martydd3 | 2:4b42ccb9df8d | 166 | for(int i = 0; i < SLICES; i++){ |
martydd3 | 2:4b42ccb9df8d | 167 | for(int j = 0; j < WIDTH; j++){ |
martydd3 | 2:4b42ccb9df8d | 168 | work_buffer[i][j] = 0x00; |
martydd3 | 2:4b42ccb9df8d | 169 | } |
martydd3 | 2:4b42ccb9df8d | 170 | } |
martydd3 | 2:4b42ccb9df8d | 171 | |
martydd3 | 2:4b42ccb9df8d | 172 | if(display_mode == 1) |
martydd3 | 2:4b42ccb9df8d | 173 | { |
martydd3 | 5:495d64d8934d | 174 | static int disp = 0; |
martydd3 | 5:495d64d8934d | 175 | for(int i = 3; i < WIDTH; i++){ |
martydd3 | 5:495d64d8934d | 176 | draw_wavecircle(i, 4, disp); |
martydd3 | 5:495d64d8934d | 177 | } |
martydd3 | 2:4b42ccb9df8d | 178 | |
martydd3 | 5:495d64d8934d | 179 | disp += 10; |
martydd3 | 5:495d64d8934d | 180 | if(disp >= SLICES){ |
martydd3 | 5:495d64d8934d | 181 | disp = 0; |
martydd3 | 5:495d64d8934d | 182 | } |
martydd3 | 2:4b42ccb9df8d | 183 | } |
martydd3 | 2:4b42ccb9df8d | 184 | else if(display_mode == 2) |
martydd3 | 2:4b42ccb9df8d | 185 | { |
martydd3 | 5:495d64d8934d | 186 | draw_line(10, 10, 0, 10, -10, 0); |
martydd3 | 5:495d64d8934d | 187 | draw_line(10, -10, 0, -10, -10, 0); |
martydd3 | 5:495d64d8934d | 188 | draw_line(-10, -10, 0, -10, 10, 0); |
martydd3 | 5:495d64d8934d | 189 | draw_line(-10, 10, 0, 10, 10, 0); |
martydd3 | 5:495d64d8934d | 190 | |
martydd3 | 5:495d64d8934d | 191 | draw_line(10, 10, 7, 10, -10, 7); |
martydd3 | 5:495d64d8934d | 192 | draw_line(10, -10, 7, -10, -10, 7); |
martydd3 | 5:495d64d8934d | 193 | draw_line(-10, -10, 7, -10, 10, 7); |
martydd3 | 5:495d64d8934d | 194 | draw_line(-10, 10, 7, 10, 10, 7); |
martydd3 | 2:4b42ccb9df8d | 195 | |
martydd3 | 5:495d64d8934d | 196 | draw_line(10, 10, 0, 10, 10, 7); |
martydd3 | 5:495d64d8934d | 197 | draw_line(10, -10, 0, 10, -10, 7); |
martydd3 | 5:495d64d8934d | 198 | draw_line(-10, -10, 0, -10, -10, 7); |
martydd3 | 5:495d64d8934d | 199 | draw_line(-10, 10, 0, -10, 10, 7); |
martydd3 | 5:495d64d8934d | 200 | |
martydd3 | 2:4b42ccb9df8d | 201 | } |
martydd3 | 2:4b42ccb9df8d | 202 | else if(display_mode == 3) |
martydd3 | 2:4b42ccb9df8d | 203 | { |
martydd3 | 4:75498bd2e742 | 204 | |
martydd3 | 2:4b42ccb9df8d | 205 | } else if(display_mode == 4){ |
martydd3 | 5:495d64d8934d | 206 | for(int i = -12; i < 13; i += 4){ |
martydd3 | 5:495d64d8934d | 207 | draw_line(i, 16, 0, i, -16, 0); |
martydd3 | 5:495d64d8934d | 208 | draw_line(16, i, 0, -16, i, 0); |
martydd3 | 5:495d64d8934d | 209 | } |
martydd3 | 2:4b42ccb9df8d | 210 | } |
martydd3 | 2:4b42ccb9df8d | 211 | |
martydd3 | 2:4b42ccb9df8d | 212 | convert_array(); |
martydd3 | 2:4b42ccb9df8d | 213 | } |
martydd3 | 2:4b42ccb9df8d | 214 | |
martydd3 | 2:4b42ccb9df8d | 215 | bool animate_i = false; |
martydd3 | 2:4b42ccb9df8d | 216 | void animate_int(){ |
martydd3 | 2:4b42ccb9df8d | 217 | animate_i = true; |
martydd3 | 0:b654a906fb40 | 218 | } |
martydd3 | 0:b654a906fb40 | 219 | |
martydd3 | 0:b654a906fb40 | 220 | int main (void) |
martydd3 | 0:b654a906fb40 | 221 | { |
martydd3 | 5:495d64d8934d | 222 | display_mode = 1; |
martydd3 | 2:4b42ccb9df8d | 223 | |
martydd3 | 0:b654a906fb40 | 224 | InterruptIn hall_pin(p25); |
martydd3 | 0:b654a906fb40 | 225 | hall_pin.fall(&rotate_sense); |
martydd3 | 2:4b42ccb9df8d | 226 | animate_ticker.attach(&animate_int, 3.0); |
martydd3 | 0:b654a906fb40 | 227 | |
martydd3 | 0:b654a906fb40 | 228 | uint8_t channel = 2; |
martydd3 | 0:b654a906fb40 | 229 | |
martydd3 | 0:b654a906fb40 | 230 | //Set the Channel. 0 is default, 15 is max |
martydd3 | 0:b654a906fb40 | 231 | mrf.SetChannel(channel); |
martydd3 | 0:b654a906fb40 | 232 | |
martydd3 | 0:b654a906fb40 | 233 | while(true) { |
martydd3 | 2:4b42ccb9df8d | 234 | if(animate_i){ |
martydd3 | 2:4b42ccb9df8d | 235 | animate(); |
martydd3 | 2:4b42ccb9df8d | 236 | } |
martydd3 | 0:b654a906fb40 | 237 | } |
martydd3 | 0:b654a906fb40 | 238 | } |