Sami Kanderian / Mbed 2 deprecated TestAnalogInPins

Dependencies:   mbed

main.cpp

Committer:
skanderian
Date:
2016-05-11
Revision:
2:f2583b56777e
Parent:
1:610dc7763656
Child:
3:3b45e830ebe1

File content as of revision 2:f2583b56777e:

//Chesapeake Embedded code by Sami Kanderian. Date of last update (version) shown below.
#include "mbed.h"
#include "string"
#include "iostream"
#include "math.h"
//#include "stdio.h"
//#include "rtos.h"

//Declare hardware inputs
Timer timer;
Serial serial(USBTX, USBRX);
//IMPORTANT NOTE: PTA1 and PTA2 ARE RESERVED FOR SERIAL COMMUNICATION!!! DO NOT USE FOR SENSOR/ACTUATOR I/O!!!

//Analog inputs : 0-3.3V
AnalogIn pin1(PTE20);
AnalogIn pin2(PTB0);
AnalogIn pin3(PTE21);//should be 16bit but doesnt work on mbed
AnalogIn pin4(PTB1);
AnalogIn pin5(PTE22);
AnalogIn pin6(PTB2);
AnalogIn pin7(PTE23);
AnalogIn pin8(PTB3);
AnalogIn pin9(PTE29);
AnalogIn pin10(PTC2);
AnalogIn pin11(PTE30);
AnalogIn pin12(PTC1);
AnalogIn pin13(PTC0);
AnalogIn pin14(PTD1);// doesnt work. Seems to be stuck at 3.3
AnalogIn pin15(PTD5);
AnalogIn pin16(PTD6);

//Built in LEDs
PwmOut rLed(LED_RED);
PwmOut gLed(LED_GREEN);
PwmOut bLed(LED_BLUE);

//Date version as global
string dateVersion = "#Microcontroller Detected! uC code ver.: 04.12.2016";

//New globals for RxInterrupt routine
const int bufferSize = 256;
char rxBuffer[bufferSize];
char txBuffer[bufferSize];
volatile int rxIn = 0;
volatile int rxOut = 0;
bool rxFlag = 0;

//Declare globals
char hexCCIn[2];
char hexDDDDIn[4];
int decCCIn;
int decDDDDIn;


void ledConfirmSent()//Light up blue LED 10%
{
    rLed = 1;
    gLed = 1;
    bLed = 0.9;
}

void ledConfirmReceive()//Light up green LED 10%
{
    rLed = 1;
    gLed = 0.9;
    bLed = 1;
}

void sendAnalogIn(int pinNum) //send Analog input in V
{
    
    //serial.printf("\r\n");
    if (pinNum == 0 || pinNum == 1) {
        serial.printf("%s%03.1f%s\r\n", "#PTE20: ", 3.3f*pin1.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 2) {
        serial.printf("%s%03.1f%s\r\n", "#PTB0:  ", 3.3f*pin2.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 3) {
        serial.printf("%s%03.1f%s\r\n", "#PTE21: ", 3.3f*pin3.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 4) {
        serial.printf("%s%03.1f%s\r\n", "#PTB1:  ", 3.3f*pin4.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 5) {
        serial.printf("%s%03.1f%s\r\n", "#PTE22: ", 3.3f*pin5.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 6) {
        serial.printf("%s%03.1f%s\r\n", "#PTB2:  ", 3.3f*pin6.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 7) {
        serial.printf("%s%03.1f%s\r\n", "#PTE23: ", 3.3f*pin7.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 8) {
        serial.printf("%s%03.1f%s\r\n", "#PTB3:  ", 3.3f*pin8.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 9) {
        serial.printf("%s%03.1f%s\r\n", "#PTE29: ", 3.3f*pin9.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 10) {
        serial.printf("%s%03.1f%s\r\n", "#PTC2:  ", 3.3f*pin10.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 11) {
        serial.printf("%s%03.1f%s\r\n", "#PTE30: ", 3.3f*pin11.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 12) {
        serial.printf("%s%03.1f%s\r\n", "#PTC1:  ", 3.3f*pin12.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 13) {
        serial.printf("%s%03.1f%s\r\n", "#PTC0:  ", 3.3f*pin13.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 14) {
        serial.printf("%s%03.1f%s\r\n", "#PTD1:  ", 3.3f*pin14.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 15) {
        serial.printf("%s%03.1f%s\r\n", "#PTD5:  ", 3.3f*pin15.read(), "V^");
    }
    if (pinNum == 0 || pinNum == 16) {
        serial.printf("%s%03.1f%s\r\n", "#PTD6:  ", 3.3f*pin16.read(), "V^");
    }

    ledConfirmSent();
}

string decToHexDDDD(int i) {
    //Do twos complement to handle negative numbers
    if (i < 0) {
        i = 32767 - i;
    }
    char hex_string[10];
    sprintf(hex_string, "0x%02X", i);
    return string(hex_string);
}

int hexDDDDToDec(char hex[]) {
    int decValue = strtol(hex, NULL, 16);
    if (decValue >= 32768) { //Do twos complement to handle negative numbers
        decValue = 32767 - decValue;
    }
    return decValue;
}

int hexToDec(char hex[]) {
    int decValue = strtol(hex, NULL, 16);
    return decValue;
}

int hexToDecSub(char hex[], int st, int ed) {
    int n = ed - st + 1;
    char hexSubset[n];
    for (int i = st; i < ed + 1; i++) {
        hexSubset[i - st] = hex[i];
    }
    int decValue = strtol(hexSubset, NULL, 16);
    return decValue;
}

void sendHexDDDDValTimes100(float val) {
    int valTimes100 = (int) ((100 * val));
    string hexDDDD = decToHexDDDD(valTimes100);
    int nLeadingZeros = 4 - hexDDDD.length();

    char hexDDDDout[hexDDDD.length()];
    for (int i = 0; i < hexDDDD.length(); i++) {
        hexDDDDout[i] = hexDDDD[i];
    }
    char leadingZeros[nLeadingZeros];
    for (int i = 0; i < nLeadingZeros; i++) {
        leadingZeros[i] = '0';
    }
    //serial.printf("#%s%saa^\r\n", leadingZeros, hexDDDDout);
    serial.printf("#%s%saa", leadingZeros, hexDDDDout);
    //ledConfirmSent();
}

void runSingleCommand(int comInd) {
    int endBuffer = (comInd + 1)*8;
    int startBuffer = endBuffer - 7;

    //get hexCCIn
    for (int i = 0; i < 2; i++) {
        hexCCIn[i] = rxBuffer[i + startBuffer];
    }
    //serial.printf("hexCCIn= is %s\r\n", hexCCIn);

    for (int i = 0; i < 4; i++) {
        hexDDDDIn[i] = rxBuffer[i + startBuffer + 2];
    }

    decCCIn = hexToDec(hexCCIn);
    decDDDDIn = hexDDDDToDec(hexDDDDIn);

    //Make sure incoming checksum pans out: DO THIS LATER

    if (comInd == 0) { //include # if first command
        startBuffer = startBuffer - 1;
    }
    int i2 = 0;
    for (int i = startBuffer; i <= endBuffer; i++) { // duplicate bytes 0 to 9 for now;
        txBuffer[i2] = rxBuffer[i];
        i2++;
    }

    //Do action based on CC: already in while loop
   

}

void runWhenNewSerialIn() {
    int nCommands = (rxIn - 1) / 8; //find out how many commands are sent in one serial message
    //serial.printf("%s%d%s\r\n", "#nCommands: ", nCommands, "commands");
    for (int comInd = 0; comInd < nCommands; comInd++) { // duplicate bytes 0 to 9 for now;
        runSingleCommand(comInd);
    }
    serial.printf("^\r\n"); //end line after responses to all commands returned
}

void Rx_interrupt() {
    // Loop just in case more than one character is in UART's receive FIFO buffer
    // Stop if buffer full
    //while ((serial.readable()) && (((rxIn + 1) % bufferSize) != 0)) {
    while (serial.readable()) {
        rxBuffer[rxIn] = serial.getc();
        if (rxBuffer[rxIn] == '\r') { //looking for character not string (string is double quotes) \r is CR, \n is LF
            rxFlag = 1;
            //Turn built in LED blue (at half intensity) to confirm command recieved
            ledConfirmReceive();
            //Execute runWhenNewSerialIn when new Rx recieved (ending with \r)
            runWhenNewSerialIn();
            rxFlag = 0; //reset flag to listen for next message
            rxIn = 0; // reset position index to 0

        } else {
            rxIn = (rxIn + 1) % bufferSize;
        }
    }
}

int main() {

    serial.baud(9600);
    serial.attach(&Rx_interrupt, Serial::RxIrq);

    //rLed.period(0.001f);
    //gLed.period(0.001f);
    //bLed.period(0.001f);
   

    //flash LED blue then green on startup or system reset
    //serial.printf("Before Blue on Startup^\r\n");
    ledConfirmSent();
    ledConfirmReceive();
    //serial.printf("After Green on Startup^\r\n");
    timer.start();

    while (1) {
        //scanf("%10s", rxBuffer);// NO! Use Rx_interrupt instead!
        //Always keep running computePressureCommand when pump is enabled (runPump=true)
        sendAnalogIn(decDDDDIn);
        wait(1);
    }
}