Screensaver

Dependencies:   mbed

Committer:
martydd3
Date:
Mon May 11 19:11:56 2015 +0000
Revision:
9:2f23704d4a47
Parent:
8:09efa61ef52c
Parent:
7:2db895370298
5/11/2015

Who changed what in which revision?

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