Screensaver

Dependencies:   mbed

Committer:
martydd3
Date:
Mon May 11 19:08:56 2015 +0000
Revision:
8:09efa61ef52c
Parent:
6:baece3338bfe
Child:
9:2f23704d4a47
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 2:4b42ccb9df8d 175 void animate(){
martydd3 2:4b42ccb9df8d 176
martydd3 8:09efa61ef52c 177 if(display_mode == 0)
martydd3 2:4b42ccb9df8d 178 {
martydd3 6:baece3338bfe 179 set_hfunc(&wave_height);
martydd3 8:09efa61ef52c 180 freq = 4;
martydd3 6:baece3338bfe 181
martydd3 5:495d64d8934d 182 for(int i = 3; i < WIDTH; i++){
martydd3 6:baece3338bfe 183 draw_circle(i, 4);
martydd3 5:495d64d8934d 184 }
martydd3 2:4b42ccb9df8d 185
martydd3 6:baece3338bfe 186 freq_disp += 0.1;
martydd3 6:baece3338bfe 187 if(freq_disp >= 1.0){
martydd3 6:baece3338bfe 188 freq_disp = 0.0;
martydd3 5:495d64d8934d 189 }
martydd3 2:4b42ccb9df8d 190 }
martydd3 8:09efa61ef52c 191 else if(display_mode == 1)
martydd3 2:4b42ccb9df8d 192 {
martydd3 6:baece3338bfe 193 set_hfunc(NULL);
martydd3 5:495d64d8934d 194 draw_line(10, 10, 0, 10, -10, 0);
martydd3 5:495d64d8934d 195 draw_line(10, -10, 0, -10, -10, 0);
martydd3 5:495d64d8934d 196 draw_line(-10, -10, 0, -10, 10, 0);
martydd3 5:495d64d8934d 197 draw_line(-10, 10, 0, 10, 10, 0);
martydd3 5:495d64d8934d 198
martydd3 5:495d64d8934d 199 draw_line(10, 10, 7, 10, -10, 7);
martydd3 5:495d64d8934d 200 draw_line(10, -10, 7, -10, -10, 7);
martydd3 5:495d64d8934d 201 draw_line(-10, -10, 7, -10, 10, 7);
martydd3 5:495d64d8934d 202 draw_line(-10, 10, 7, 10, 10, 7);
martydd3 2:4b42ccb9df8d 203
martydd3 5:495d64d8934d 204 draw_line(10, 10, 0, 10, 10, 7);
martydd3 5:495d64d8934d 205 draw_line(10, -10, 0, 10, -10, 7);
martydd3 5:495d64d8934d 206 draw_line(-10, -10, 0, -10, -10, 7);
martydd3 5:495d64d8934d 207 draw_line(-10, 10, 0, -10, 10, 7);
martydd3 8:09efa61ef52c 208
martydd3 8:09efa61ef52c 209 draw_line(5, 5, 2, 5, -5, 2);
martydd3 8:09efa61ef52c 210 draw_line(5, -5, 2, -5, -5, 2);
martydd3 8:09efa61ef52c 211 draw_line(-5, -5, 2, -5, 5, 2);
martydd3 8:09efa61ef52c 212 draw_line(-5, 5, 2, 5, 5, 2);
martydd3 8:09efa61ef52c 213
martydd3 8:09efa61ef52c 214 draw_line(5, 5, 5, 5, -5, 5);
martydd3 8:09efa61ef52c 215 draw_line(5, -5, 5, -5, -5, 5);
martydd3 8:09efa61ef52c 216 draw_line(-5, -5, 5, -5, 5, 5);
martydd3 8:09efa61ef52c 217 draw_line(-5, 5, 5, 5, 5, 5);
martydd3 8:09efa61ef52c 218
martydd3 8:09efa61ef52c 219 draw_line(5, 5, 2, 5, 5, 5);
martydd3 8:09efa61ef52c 220 draw_line(5, -5, 2, 5, -5, 5);
martydd3 8:09efa61ef52c 221 draw_line(-5, -5, 2, -5, -5, 5);
martydd3 8:09efa61ef52c 222 draw_line(-5, 5, 2, -5, 5, 5);
martydd3 5:495d64d8934d 223
martydd3 2:4b42ccb9df8d 224 }
martydd3 8:09efa61ef52c 225
martydd3 8:09efa61ef52c 226 else if(display_mode == 2){
martydd3 6:baece3338bfe 227 freq = 1.5;
martydd3 6:baece3338bfe 228 set_hfunc(&wave_height);
martydd3 6:baece3338bfe 229
martydd3 6:baece3338bfe 230
martydd3 6:baece3338bfe 231 freq_disp += 0.05;
martydd3 6:baece3338bfe 232 if(freq_disp >= 1.0){
martydd3 6:baece3338bfe 233 freq_disp = 0.0;
martydd3 6:baece3338bfe 234 }
martydd3 6:baece3338bfe 235
martydd3 6:baece3338bfe 236 for(int i = -12; i < 13; i += 1){
martydd3 5:495d64d8934d 237 draw_line(i, 16, 0, i, -16, 0);
martydd3 6:baece3338bfe 238 //draw_line(16, i, 0, -16, i, 0);
martydd3 5:495d64d8934d 239 }
martydd3 2:4b42ccb9df8d 240 }
martydd3 2:4b42ccb9df8d 241
martydd3 2:4b42ccb9df8d 242 convert_array();
martydd3 2:4b42ccb9df8d 243 }
martydd3 2:4b42ccb9df8d 244
martydd3 2:4b42ccb9df8d 245 bool animate_i = false;
martydd3 2:4b42ccb9df8d 246 void animate_int(){
martydd3 2:4b42ccb9df8d 247 animate_i = true;
martydd3 0:b654a906fb40 248 }
martydd3 0:b654a906fb40 249
martydd3 8:09efa61ef52c 250 Ticker switch_ticker;
martydd3 8:09efa61ef52c 251
martydd3 8:09efa61ef52c 252 void switch_mode(){
martydd3 8:09efa61ef52c 253 display_mode = (display_mode + 1) % 3;
martydd3 8:09efa61ef52c 254 }
martydd3 8:09efa61ef52c 255
martydd3 0:b654a906fb40 256 int main (void)
martydd3 0:b654a906fb40 257 {
martydd3 6:baece3338bfe 258 display_mode = 4;
martydd3 2:4b42ccb9df8d 259
martydd3 0:b654a906fb40 260 InterruptIn hall_pin(p25);
martydd3 0:b654a906fb40 261 hall_pin.fall(&rotate_sense);
martydd3 8:09efa61ef52c 262 animate_ticker.attach(&animate_int, 1.0);
martydd3 8:09efa61ef52c 263 switch_ticker.attach(&switch_mode, 15.0);
martydd3 0:b654a906fb40 264
martydd3 0:b654a906fb40 265 uint8_t channel = 2;
martydd3 0:b654a906fb40 266
martydd3 0:b654a906fb40 267 //Set the Channel. 0 is default, 15 is max
martydd3 0:b654a906fb40 268 mrf.SetChannel(channel);
martydd3 0:b654a906fb40 269
martydd3 0:b654a906fb40 270 while(true) {
martydd3 2:4b42ccb9df8d 271 if(animate_i){
martydd3 2:4b42ccb9df8d 272 animate();
martydd3 2:4b42ccb9df8d 273 }
martydd3 0:b654a906fb40 274 }
martydd3 0:b654a906fb40 275 }