Tyler Altenhofen / Mbed 2 deprecated TylerPOV

Dependencies:   mbed

main.cpp

Committer:
tyleralt
Date:
2015-04-22
Revision:
6:1d7d769b398d
Parent:
5:6d8f9165021e
Child:
7:7a73065d2d92

File content as of revision 6:1d7d769b398d:

#include "mbed.h"
#include <vector>
#define BUFFER_SIZE 16
#define NUMBER_OF_SLICES 120
#include "MRF24J40.h"

//push data pins
DigitalOut pushRegister(p23);
DigitalOut pushBit(p24);

//write to arm pins
DigitalOut dataArmOne(p15);
DigitalOut dataArmTwo(p16);
DigitalOut dataArmThree(p17);
DigitalOut dataArmFour(p18);
DigitalOut dataArmFive(p19);
DigitalOut dataArmSix(p20);
DigitalOut dataArmSeven(p21);
DigitalOut dataArmEight(p22);

//hass sensor interupt
InterruptIn hallSensor(p25);

//Set Up Timer and ticker
Timer rotationTime;
Ticker updateLeds;

//Declare global vars
double slice_time;
double rotate_time; 
int current_slice;
char slice_data [NUMBER_OF_SLICES][16]; //[slice][specific led distance] (0 is closest) & with approppriate bit for each arm 
int firstTime;

//serial port for pc and bluetooth
Serial pc(USBTX, USBRX); // tx, rx
Serial bt(p9, p10);// tx, rx

//define variables for ziggbee
MRF24J40 mrf(p11, p12, p13, p14, p26);
char txBuffer[128];
char rxBuffer[128];
int rxLen;


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;
}
void recievePoint (int slice, int distance, char addChar){
    //TODO make this edit the other thing
}

void pushData (char bits [16]){
  bt.printf("pushing data \r\n");
  for (int i = 8; i < 16; i ++){
    dataArmOne = bits [i] & 0x01;
    dataArmTwo = bits [i] & 0x02;
    dataArmThree = bits [i]& 0x04;
    dataArmFour = bits [i]& 0x08;
    dataArmFive = bits [i] & 0x10;
    dataArmSix = bits [i] & 0x20;
    dataArmSeven = bits [i] & 0x40;
    dataArmEight = bits [i] & 0x80;
    
    pushBit = 1;
    pushBit = 0;
  }
  for (int i = 7; i >= 0; i --){
    dataArmOne = bits [i] & 0x01;
    dataArmTwo = bits [i] & 0x02;
    dataArmThree = bits [i]& 0x04;
    dataArmFour = bits [i]& 0x08;
    dataArmFive = bits [i] & 0x10;
    dataArmSix = bits [i] & 0x20;
    dataArmSeven = bits [i] & 0x40;
    dataArmEight = bits [i] & 0x80;
    
    pushBit = 1;
    pushBit = 0;
    }
      
  pushRegister = 1;
  pushRegister = 0;
}


//periodic display interrupt
void nextLedPush(){
  if (current_slice < NUMBER_OF_SLICES){
    pushData(slice_data[current_slice]);
    current_slice ++;
  } else {
      updateLeds.detach();
  }
}

//Hall sensor interupt
void rotate_sense(){
  bt.printf("interupt");
  if (firstTime){
    rotationTime.reset();
    rotationTime.start();
    firstTime = false;
    pc.printf("first time");
    return;
  } else if(current_slice < NUMBER_OF_SLICES / 4){
      return;
      }
  bt.printf("seconod time \n");
    rotate_time = rotationTime.read_us();
    rotationTime.reset();    
    rotationTime.start();
    
    slice_time = (double) rotate_time/NUMBER_OF_SLICES;
    current_slice = 0;
    updateLeds.attach_us(&nextLedPush, slice_time);
}

void didRecievePoint(){
}


int main() {
    bt.printf("started");
    firstTime = true;
    current_slice = 0;
    
    uint8_t channel = 6;
    //mrf.SetChannel(channel);
    
    hallSensor.fall(&rotate_sense);
    
    for (int i = 0; i < 60; i ++){
        slice_data [i][0] = 0x01; // bit 0 is high on arm 0
        slice_data [i][1] = 0x01;
        slice_data [i][2] = 0x01;
        slice_data [i][3] = 0x01;
        slice_data [i][4] = 0x01;
        slice_data [i][5] = 0x01;
        slice_data [i][6] = 0x01;
        slice_data [i][7] = 0x01; 
        slice_data [i][8] = 0x01;
        slice_data [i][9] = 0x01;
        slice_data [i][10] = 0x01;
        slice_data [i][11] = 0x01;
        slice_data [i][12] = 0x01;
        slice_data [i][13] = 0x01;
        slice_data [i][14] = 0x01;
        slice_data [i][15] = 0x01;
    }
    for (int i = 60; i < 120; i++){
        slice_data [i][0] = 0x00; // bit 0 is high on arm 0
        slice_data [i][1] = 0x01;
        slice_data [i][2] = 0x00;
        slice_data [i][3] = 0x01;
        slice_data [i][4] = 0x00;
        slice_data [i][5] = 0x01;
        slice_data [i][6] = 0x00;
        slice_data [i][7] = 0x01;
        slice_data [i][8] = 0x00;
        slice_data [i][9] = 0x01;
        slice_data [i][10] = 0x00;
        slice_data [i][11] = 0x01;
        slice_data [i][12] = 0x00;
        slice_data [i][13] = 0x01;
        slice_data [i][14] = 0x00;
        slice_data [i][15] = 0x01;
    }
    
    pushData(slice_data[80]);
    while(true) {
        //Try to receive some data
        
        //rxLen = rf_receive(rxBuffer, 24);
        //if(rxLen > 24) {
            
        //}
    }
}