Maxim Integrated / Mbed OS MAX1473_RF_Receiver_Demo

Dependencies:   max32630fthr USBDevice

main.cpp

Committer:
tlyp
Date:
2020-08-14
Revision:
10:aec2b8ba06a0
Parent:
9:02c5adb483c8
Child:
11:72363a906638

File content as of revision 10:aec2b8ba06a0:

/*******************************************************************************
* Copyright (C) 2018 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
*******************************************************************************
*/
#include "mbed.h"
#include "max32630fthr.h"
#include "USBSerial.h"
#include "MAX30208.h"
#include "ForwardErrCorr.h"

MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);

// Hardware serial port over DAPLink
Serial daplink(USBTX, USBRX);          // Use USB debug probe for serial link
Serial uart(P2_1, P2_0);
RawSerial uart2(P3_1,P3_0);//tx,rx

I2C i2c(P3_4, P3_5);  //sda,scl

MAX30208 TempSensor(i2c, 0x50); //Constructor takes 7-bit slave adrs
char  TransTable[] = {0x1F,0x18,0x06,0x01};
Translator transRx(uart2, TransTable);

// Virtual serial port over USB
USBSerial microUSB; 
DigitalOut rLED(LED1);
DigitalOut gLED(LED2); 
DigitalOut bLED(LED3);
DigitalIn TakeData(P5_6);
DigitalIn TakeData2(P5_5);
DigitalIn sw1(SW1);

void buildArray(int j, char *input,char *output){
    int i = j+1;
    int k = 0;
    while(input[i] != 'c'){
        output[k] = input[i];
        i++;
        k++;
    }
}
char dataIn[50];

void SerialCallback(){
    //Timer timer;
    //timer.start();
    wait_ms(1);
    int i = strlen(dataIn);
    if (i >= 50){
        i = 0;
    }
    while(uart2.readable()){
        dataIn[i] = uart2.getc();
        i++;
    }
    //wait_ms(10);
}

// main() runs in its own thread in the OS
// (note the calls to Thread::wait below for delays)
int main()
{
    rLED = LED_ON;
    gLED = LED_ON;
    bLED = LED_OFF;
    uart2.baud(9600);
    uart2.attach(&SerialCallback);
    rLED = LED_OFF;
    char dataOut[50];
    uint16_t output[10];
    while(1) {
        if (strlen(dataIn) >= 12){
            int j = 0;
            while(dataIn[j] != 'b' && j < 30){
                j++;
            }
            if (dataIn[j] == 'b'){
                microUSB.printf("\r\n\r\n\r\n\r\n");
                buildArray(j,dataIn,dataOut);
                microUSB.printf("\r\n");
                if (transRx.Reconstruct(dataOut,output) == 1){
                    microUSB.printf("Error reconstructing\r\n");
                }
                
                //Print out the Device Type, ID, and Temperature
                microUSB.printf("Device Type: %c\r\n", dataIn[j+1]);
                microUSB.printf("Device ID: %c\r\n", dataIn[j+2]);
                
                //uint16_t temp1 = (output[0]<<8);
                //uint16_t delivery = (temp1 + output[1]);
                //float clesius = TempSensor.toCelsius(output[0]);
                //float fare = TempSensor.toFahrenheit(clesius);
                microUSB.printf("Data read %u\r\n",output[0]);
                for (int i = 0;i<sizeof(dataIn);i++){
                    dataIn[i] = 0;
                }
            }
            else{
                for (int i = 0;i<sizeof(dataIn);i++){
                    dataIn[i] = 0;
                }
            }
        }
    }
}