Tyler Altenhofen / Mbed 2 deprecated TylerPOVController

Dependencies:   mbed

main.cpp

Committer:
tyleralt
Date:
2015-04-24
Revision:
4:a7442c1faed1
Parent:
3:9e8eb12f831f
Child:
5:54ff826d1897

File content as of revision 4:a7442c1faed1:

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


int offAngles [8] = {0, 180, 45, 225, 90, 270, 135, 315};

//*********************point class************************//
class Point {
    int positionRadian, positionHeight, positionDistance;
    public: 
    Point (int , int , int );
    int getArraySlice(void);
    char getIdentifyingChar(void);
    int getPositionDistance(void);
    void moveUp();
    void moveDown();
    void rotateRight();
    void rotateLeft();
    void moveIn();
    void moveOut();
        
};
Point :: Point (int posRadian, int posHeight, int posDistance){
    positionRadian = posRadian;
    positionHeight = posHeight;
    positionDistance = posDistance;
}     
int Point :: getArraySlice (void){
    return (int) (positionRadian + (offAngles[positionHeight]/3))%120;
}
char Point :: getIdentifyingChar(void){
    return 0x01 << positionHeight;
}
int Point :: getPositionDistance(void){
    return positionDistance;
}
void Point :: moveUp(){
    if (positionHeight < 7){
        positionHeight ++;
    }
}
void Point :: moveDown(){
    if(positionHeight > 0){
        positionHeight --;
    }
}        
void Point :: rotateRight(){
    positionRadian = (positionRadian + 1) % 120;
}
void Point :: rotateLeft(){
    positionRadian = (positionRadian - 1) % 120;
}
void Point :: moveIn(){
    if (positionDistance > 0){
        positionDistance --;
    }
}
void Point :: moveOut(){
    if (positionDistance < 15){
        positionDistance ++;
    }
}

//******************end Point class*******************//
    
enum DrawingMode { line, point, circle };
DrawingMode currentMode;

Point pointer(60, 3, 8);

DigitalIn digitalMode(p15);
DigitalIn digitalUp(p16);
DigitalIn digitalDown(p17);
DigitalIn digitalRotateRight(p18);
DigitalIn digitalRotateLeft(p21);
DigitalIn digitalMoveOut(p22);
DigitalIn digtialMoveIn(p23);
DigitalIn digitalSelect(p24);

InterruptIn buttonMode(p15);
InterruptIn buttonUp(p16);
InterruptIn buttonDown(p17);
InterruptIn buttonRotateRight(p18);
InterruptIn buttonRotateLeft(p21);
InterruptIn buttonMoveOut(p22);
InterruptIn buttonMoveIn(p23);
InterruptIn buttonSelect(p24);

bool registerButtons = true;
Timeout buttonRegisterTimeout;
Timeout unBlink;

Ticker pointerBlinkTicker;

MRF24J40 mrf(p11, p12, p13, p14, p26);
char txBuffer[128];
int rxLen;


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

void sendPointOver (int slice, int distance, char height, char operation){
    ///TODO implement communications
    //printf(" slice %i distance %i height %X operation %c \r\n", (char) slice, (char) distance, height, operation);
    txBuffer [0] = (char) slice;
    txBuffer [1] = (char)distance;
    txBuffer [2] = height;
    txBuffer [3] = operation;

    rf_send(txBuffer, 5);
}

void drawPoint(){
    
    int pointerSlice = pointer.getArraySlice();
    int pointerDistance = pointer.getPositionDistance();
    char height = pointer.getIdentifyingChar();
    sendPointOver(pointerSlice, pointerDistance, height, 'o');
}

void registerButtonsAgain (){
    registerButtons = true;
}

//***************** all of the button reactions *********//
void changeMode(){
    if (!registerButtons){
        return;
    }
    registerButtons = false;
    buttonRegisterTimeout.attach(&registerButtonsAgain, .1);
    printf("changeMode\r\n");
    if (currentMode == point){
        currentMode = line;
        printf("line mode\r\n");
        return;
    }
    if (currentMode == line){
        currentMode = circle;
        printf("circle mode\r\n");
        return;
    }
    if (currentMode == circle){
        currentMode = point;
        printf("point mode\r\n");
        return;
    }
}
void moveUp(){
    if (!registerButtons){
        return;
    }
    registerButtons = false;
    buttonRegisterTimeout.attach(&registerButtonsAgain, .1);
    printf("moveUp\r\n");
    if (currentMode == line){
        drawPoint();
    }
    pointer.moveUp();
}
void moveDown(){
    if (!registerButtons){
        return;
    }
    registerButtons = false;
    buttonRegisterTimeout.attach(&registerButtonsAgain, .1);
    if (currentMode == line){
        drawPoint();
    }
    printf("moveDown\r\n");
    pointer.moveDown();
}
void rotateRight(){
    if (!registerButtons){
        return;
    }
    registerButtons = false;
    buttonRegisterTimeout.attach(&registerButtonsAgain, .1);
    printf("rotaeRight\r\n");
    if (currentMode == line){
        drawPoint();
        pointer.rotateRight();
        wait(.3);
        while (digitalRotateRight){
            drawPoint();
            pointer.rotateRight();
            wait(.03);
        }
        return;
    }
    pointer.rotateRight();
    wait(.3);
    while (digitalRotateRight){
        pointer.rotateRight();
        wait(.03);
    }
}
void rotateLeft(){
    if (!registerButtons){
        return;
    }
    registerButtons = false;
    buttonRegisterTimeout.attach(&registerButtonsAgain, .1);
    printf("rotateLeft\r\n");
    if (currentMode == line){
        drawPoint();
        pointer.rotateLeft();
        wait(.3);
        while (digitalRotateLeft){
            drawPoint();
            pointer.rotateLeft();
            wait(.03);
        }
        return;
    }
    pointer.rotateLeft();
    wait(.3);
    while(digitalRotateLeft){
        pointer.rotateLeft();
        wait(.03);
        
    }
        
}
void moveOut(){
    if (!registerButtons){
        return;
    }
    registerButtons = false;
    buttonRegisterTimeout.attach(&registerButtonsAgain, .1);
    printf("moveOut\r\n");
    if (currentMode == line){
        drawPoint();
    }
    pointer.moveOut();
}
void moveIn(){
    if (!registerButtons){
        return;
    }
    registerButtons = false;
    buttonRegisterTimeout.attach(&registerButtonsAgain, .1);
    if (currentMode == line){
        drawPoint();
    }
    printf("moveIn\r\n");
    pointer.moveIn();
}

void pointerBlink(){
    Point currentBlink = pointer;
    int slice = currentBlink.getArraySlice();
    int distance = currentBlink.getPositionDistance();
    char height = pointer.getIdentifyingChar();
    sendPointOver(slice, distance, height, 'x');
    wait(1.5);
    sendPointOver(slice, distance, height, 'x');
}
void drawWholeCircle(){
    printf("drawing circle");
    int distance = pointer.getPositionDistance();
    char height = pointer.getIdentifyingChar();
    for (int i = 0; i < 120; i ++){
        sendPointOver(i, distance, height, 'o');
    }
        
}

void select(){
        if (!registerButtons){
        return;
    }
    registerButtons = false;
    buttonRegisterTimeout.attach(&registerButtonsAgain, .1);
    printf(" select \r\n");
    if (currentMode == point){
        drawPoint();
    }
    if (currentMode == circle){
        drawWholeCircle();
    }
}
//****************end buttons section ************//

        

int main() {
    printf(" started the program\r\n ");
    
    uint8_t channel = 2;
    
 
    //Set the Channel. 0 is default, 15 is max
    mrf.SetChannel(channel);
    
    currentMode = point;
    buttonMode.rise(&changeMode);
    buttonUp.rise(&moveUp);
    buttonDown.rise(&moveDown);
    buttonRotateRight.rise(&rotateRight);
    buttonRotateLeft.rise(&rotateLeft);
    buttonMoveOut.rise(&moveOut);
    buttonMoveIn.rise(&moveIn);
    buttonSelect.rise(&select);
    pointerBlinkTicker.attach(&pointerBlink, 2.0);
    

    while(1);
    

}