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
- Committer:
- martydd3
- Date:
- 2015-05-01
- Revision:
- 2:4b42ccb9df8d
- Parent:
- 1:4a50c0bbbf9d
- Child:
- 3:7e568908f1c4
File content as of revision 2:4b42ccb9df8d:
#include "mbed.h"
#include "MRF24J40.h"
#include <string>
#include "constants.h"
#include "shape.h"
// RF tranceiver to link with handheld.
MRF24J40 mrf(p11, p12, p13, p14, p26);
// LEDs you can treat these as variables (led2 = 1 will turn led2 on!)
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
// Timer
Timer timer;
// Serial port for showing RX data.
Serial pc(USBTX, USBRX);
// Used for sending and receiving
char txBuffer[128];
char rxBuffer[128];
int rxLen;
//***************** Do not change these methods (please) *****************//
/**
* Receive data from the MRF24J40.
*
* @param data A pointer to a char array to hold the data
* @param maxLength The max amount of data to read.
*/
int rf_receive(char *data, uint8_t maxLength)
{
uint8_t len = mrf.Receive((uint8_t *)data, maxLength);
uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
if(len > 10) {
//Remove the header and footer of the message
for(uint8_t i = 0; i < len-2; i++) {
if(i<8) {
//Make sure our header is valid first
if(data[i] != header[i])
return 0;
} else {
data[i-8] = data[i];
}
}
//pc.printf("Received: %s length:%d\r\n", data, ((int)len)-10);
}
return ((int)len)-10;
}
/**
* Send data to another MRF24J40.
*
* @param data The string to send
* @param maxLength The length of the data to send.
* If you are sending a null-terminated string you can pass strlen(data)+1
*/
void rf_send(char *data, uint8_t len)
{
//We need to prepend the message with a valid ZigBee header
uint8_t header[8]= {1, 8, 0, 0xA1, 0xB2, 0xC3, 0xD4, 0x00};
uint8_t *send_buf = (uint8_t *) malloc( sizeof(uint8_t) * (len+8) );
for(uint8_t i = 0; i < len+8; i++) {
//prepend the 8-byte header
send_buf[i] = (i<8) ? header[i] : data[i-8];
}
//pc.printf("Sent: %s\r\n", send_buf+8);
mrf.Send(send_buf, len+8);
free(send_buf);
}
/*******************************************************************/
// Display interrupt and drivers
int frame_id = 0;
char frame_buffer1[SLICES][WIDTH];
char frame_buffer2[SLICES][WIDTH];
char work_buffer[SLICES][WIDTH];
int slice_i = 100;
BusOut blade(p15, p16, p17, p18, p19, p20, p21, p22);
DigitalOut clk(p23);
DigitalOut disp(p24);
Ticker pixel_ticker;
void push_pixels(){
for(int j = 8; j < WIDTH; j++){
if(frame_id == 0)
blade = frame_buffer1[slice_i][j];
else
blade = frame_buffer2[slice_i][j];
clk = 1;
clk = 0;
}
for(int j = 7; j >= 0; j--){
if(frame_id == 0)
blade = frame_buffer1[slice_i][j];
else
blade = frame_buffer2[slice_i][j];
clk = 1;
clk = 0;
}
disp = 1;
disp = 0;
slice_i = (slice_i + 1)%SLICES;
}
//Hall sensor interupt
double rotate_time;
double slice_time;
Timer hall_timer;
void rotate_sense(){
static bool firstTime = false;
if (firstTime){
hall_timer.reset();
hall_timer.start();
firstTime = false;
return;
}
rotate_time = hall_timer.read_us();
hall_timer.reset();
hall_timer.start();
slice_time = (double) rotate_time/SLICES;
slice_i = 0;
pixel_ticker.attach_us(&push_pixels, slice_time);
}
/***************************************************************/
//code to adjust for offset blades and vertical alignment
int displaces[HEIGHT];
void init_displaces(){
for(int i = 0; i < HEIGHT; i++){
switch(i){
case 0: displaces[i] = 0; break;
case 1: displaces[i] = 4 * SLICES / 8; break;
case 2: displaces[i] = 7 * SLICES / 8 - 5; break;
case 3: displaces[i] = 3 * SLICES / 8 - 8; break;
case 4: displaces[i] = 6 * SLICES / 8 - 6; break;
case 5: displaces[i] = 2 * SLICES / 8 - 7; break;
case 6: displaces[i] = 5 * SLICES / 8 - 7; break;
case 7: displaces[i] = 1 * SLICES / 8 - 8; break;
}
}
}
void convert_array(){
static bool initialized = false;
int array_i;
if(!initialized){
init_displaces();
}
for(int i = 0; i < SLICES; i++){
for(int j = 0; j < WIDTH; j++){
char bit = 0x00;
for(int h = 0; h < HEIGHT; h++){
bit |= (work_buffer[(i + displaces[h]) % SLICES][j] & (0x01 << h));
}
if(frame_id == 0)
frame_buffer2[i][j] = bit;
else
frame_buffer1[i][j] = bit;
}
}
if(frame_id == 0)
frame_id = 1;
else
frame_id = 0;
}
int display_mode = 0;
Ticker animate_ticker;
void animate(){
static WaveCircle *wc1 = NULL;
static WaveCircle *wc2 = NULL;
static WaveCircle *wc3 = NULL;
static WaveCircle *wc4 = NULL;
static WaveCircle *wc5 = NULL;
static WaveCircle *wc6 = NULL;
static WaveCircle *wc7 = NULL;
static WaveCircle *wc8 = NULL;
static WaveCircle *wc9 = NULL;
static WaveCircle *wc10 = NULL;
static WaveCircle *wc11 = NULL;
static WaveCircle *wc12 = NULL;
static WaveCircle *wc13 = NULL;
for(int i = 0; i < SLICES; i++){
for(int j = 0; j < WIDTH; j++){
work_buffer[i][j] = 0x00;
}
}
if(display_mode == 1)
{
if(wc1 == NULL){
wc1 = new WaveCircle(3, 4, &work_buffer);
wc2 = new WaveCircle(4, 4, &work_buffer);
wc3 = new WaveCircle(5, 4, &work_buffer);
wc4 = new WaveCircle(6, 4, &work_buffer);
wc5 = new WaveCircle(7, 4, &work_buffer);
wc6 = new WaveCircle(8, 4, &work_buffer);
wc7 = new WaveCircle(9, 4, &work_buffer);
wc8 = new WaveCircle(10, 4, &work_buffer);
wc9 = new WaveCircle(11, 4, &work_buffer);
wc10 = new WaveCircle(12, 4, &work_buffer);
wc11 = new WaveCircle(13, 4, &work_buffer);
wc12 = new WaveCircle(14, 4, &work_buffer);
wc13 = new WaveCircle(15, 4, &work_buffer);
}
wc1->animate();
wc2->animate();
wc3->animate();
wc4->animate();
wc5->animate();
wc6->animate();
wc7->animate();
wc8->animate();
wc9->animate();
wc10->animate();
wc11->animate();
wc12->animate();
wc13->animate();
wc1->draw();
wc2->draw();
wc3->draw();
wc4->draw();
wc5->draw();
wc6->draw();
wc7->draw();
wc8->draw();
wc9->draw();
wc10->draw();
wc11->draw();
wc12->draw();
wc13->draw();
}
else if(display_mode == 2)
{
// Eight sided star
Line line1(15, 0, 0, 15, 90, 7, &work_buffer);
Line line2(15, 90, 7, 15, 180, 0, &work_buffer);
Line line3(15, 180, 0, 15, 270, 7, &work_buffer);
Line line4(15, 270, 7, 15, 0, 0, &work_buffer);
line1.draw();
line2.draw();
line3.draw();
line4.draw();
Line line5(15, 45, 0, 15, 135, 7, &work_buffer);
Line line6(15, 135, 7, 15, 225, 0, &work_buffer);
Line line7(15, 225, 0, 15, 315, 7, &work_buffer);
Line line8(15, 315, 7, 15, 45, 0, &work_buffer);
line5.draw();
line6.draw();
line7.draw();
line8.draw();
}
else if(display_mode == 3)
{
Line line1(1, 0, 0, 1, 0, 7, &work_buffer);
Line line2(9, 35, 0, 9, 35, 7, &work_buffer);
Line line3(9, 325, 0, 9, 325, 7, &work_buffer);
Line line4(13, 0, 0, 13, 0, 7, &work_buffer);
line1.draw();
line2.draw();
line3.draw();
line4.draw();
Line line5(M_PI * 320 / 180, 0.777030, 0, 0, 35, 0, &work_buffer);
Line line6(M_PI * 40 / 180, 0.777030, 0, 0, 325, 0, &work_buffer);
Line line7(M_PI * 320 / 180, 8.787681, 325, 0, 0, 0, &work_buffer);
Line line8(M_PI * 40 / 180, 8.787681, 35, 0, 0, 0, &work_buffer);
line5.draw();
line6.draw();
line7.draw();
line8.draw();
Line line9(M_PI * 320 / 180, 0.777030, 0, 7, 35, 7, &work_buffer);
Line line10(M_PI * 40 / 180, 0.777030, 0, 7, 325, 7, &work_buffer);
Line line11(M_PI * 320 / 180, 8.787681, 325, 7, 0, 7, &work_buffer);
Line line12(M_PI * 40 / 180, 8.787681, 35, 7, 0, 7, &work_buffer);
line9.draw();
line10.draw();
line11.draw();
line12.draw();
} else if(display_mode == 4){
CartLine line1(0, 16, 0, 0, -16, 0, &work_buffer);
CartLine line2(4, 16, 0, 4, -16, 0, &work_buffer);
CartLine line3(8, 16, 0, 8, -16, 0, &work_buffer);
CartLine line4(12, 16, 0, 12, -16, 0, &work_buffer);
CartLine line5(-4, 16, 0, -4, -16, 0, &work_buffer);
CartLine line6(-8, 16, 0, -8, -16, 0, &work_buffer);
CartLine line7(-12, 16, 0, -12, -16, 0, &work_buffer);
line1.draw();
line2.draw();
line3.draw();
line4.draw();
line5.draw();
line6.draw();
line7.draw();
}
convert_array();
}
bool animate_i = false;
void animate_int(){
animate_i = true;
}
int main (void)
{
display_mode = 4;
InterruptIn hall_pin(p25);
hall_pin.fall(&rotate_sense);
animate_ticker.attach(&animate_int, 3.0);
uint8_t channel = 2;
//Set the Channel. 0 is default, 15 is max
mrf.SetChannel(channel);
while(true) {
if(animate_i){
animate();
}
}
}