Martin Deng / Mbed 2 deprecated mbed_shapedriver

Dependencies:   mbed

main.cpp

Committer:
martydd3
Date:
2015-04-29
Revision:
0:b654a906fb40
Child:
1:4a50c0bbbf9d

File content as of revision 0:b654a906fb40:

#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);
}

char (*frame)[SLICES][WIDTH];
char frame_buffer[2][SLICES][WIDTH];
char work_buffer[SLICES][WIDTH];

char hall_i = 0;
int hall_t = 0;

int rot_t_samples[5] = {0};
char rot_i = 0;
int rot_t = 100;

void hall_int(){
    hall_i = 1;
}

int slice_i = 0;
BusOut blade(p15, p16, p17, p18, p19, p20, p21, p22);
DigitalOut clk(p23);
DigitalOut disp(p24);
Ticker pixel_ticker;

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 push_pixels(){
    for(int j = 8; j < WIDTH; j++){
        blade = (*frame)[slice_i][j];
        
        clk = 1;
        clk = 0;    
    }
    
    for(int j = 7; j >= 0; j--){
        blade = (*frame)[slice_i][j];
        
        clk = 1;
        clk = 0;     
    }
    
    disp = 1;
    disp = 0;
    
    slice_i = (slice_i + 1)%SLICES;
}

//Hall sensor interupt
bool firstTime;
double rotate_time;
double slice_time;

Timer hall_timer;

void rotate_sense(){
    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);
}

void convert_array(){
    int array_i;
    
    if(frame == &frame_buffer[0])
        array_i = 1;
    else
        array_i = 0;
    
    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)); 
            }
            
            frame_buffer[array_i][i][j] = bit;
        }    
    } 
    
    frame = &frame_buffer[array_i];
}

int main (void)
{
    firstTime = true;
    slice_i = 100;
    init_displaces();
    
    frame = &frame_buffer[0];
    
    /*
    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();
    */
    
    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(1, 0, 0, 9, 35, 0, &work_buffer);
    Line line6(1, 0, 0, 9, 325, 0, &work_buffer);
    Line line7(15, 315, 0, 15, 0, 0, &work_buffer);
    Line line8(15, 45, 0, 15, 0, 0, &work_buffer);
    
    line5.draw();
    line6.draw();  
    line7.draw();
    line8.draw();
    
    Line line9(1, 0, 7, 9, 35, 7, &work_buffer);
    Line line10(1, 0, 7, 9, 325, 7, &work_buffer);
    Line line11(9, 325, 7, 15, 0, 7, &work_buffer);
    Line line12(9, 35, 7, 15, 0, 7, &work_buffer);
    
    line9.draw();
    line10.draw();
    line11.draw();
    line12.draw();
    
    convert_array();
    
    InterruptIn hall_pin(p25);
    hall_pin.fall(&rotate_sense);
    
    uint8_t channel = 2;

    //Set the Channel. 0 is default, 15 is max
    mrf.SetChannel(channel);

    while(true) {
        
    }
}