Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SX1276GenericLib USBDevice
Fork of NonPingPong_PICO_LoRa_LP0 by
Diff: main.cpp
- Revision:
- 3:85fc843a9d7d
- Parent:
- 2:f6ae69f3bfce
- Child:
- 5:9e751733a6f3
--- a/main.cpp Sun Aug 09 19:41:16 2020 +0000
+++ b/main.cpp Mon Oct 12 21:55:22 2020 +0000
@@ -1,566 +1,615 @@
+
#include "mbed.h"
+
+// Board pins related
#include "PinMap.h"
-//#include "BufferedSerial.h"
+// LORA related
+#include "global_buffers.h"
+#include "GenericPingPong2.h"
+#define FEATURE_LORA
+#include "sx1276-mbed-hal.h"
+
+#include "main.h"
-//#ifdef FEATURE_USBSERIAL
-//#include "USBSerialBuffered.h"
-//#endif
-//
-//#ifdef FEATURE_USBSERIAL
-//extern USBSerialBuffered *usb;
+// OT07 related
+#include "OT07_lib.h"
+#define CONVERT_T_DELAY 30
+
+// MAX44009 related
+#include "MAX44009_lib.h"
+
+// AO32 related
+#include "AO32_lib.h"
+
+//#if defined(TARGET_MAX32630FTHR) // using the RFM95 board
+// #include "max32630fthr.h"
+// MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
//#endif
-#include "GenericPingPong.h"
-
-#define FEATURE_LORA
-#include "sx1276-mbed-hal.h"
-#include "main.h"
-
-
-//#if defined(TARGET_MAX32630FTHR) // using the RFM95 board
-#include "max32630fthr.h"
-MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
-//#endif
-
-#include "USBSerial.h" // for virtual COM
-
+// Virtual COM related
+#include "USBSerial.h" // for virtual COM
#define BS 8 // ASCII Back Space
#define CR 13 // ASCII Carriage Return
// Virtual serial port over USB TODO NEW VID PID NEEDED!!
USBSerial pc(0x0B6A, 0x0042, 0x0001, false);
+///***************************************************************************
+// * Virtual COM Instantiation
+// **************************************************************************/
+//#include "USBSerial.h" // for virtual COM
+//#define BS 8 // ASCII Back Space
+//#define CR 13 // ASCII Carriage Return
+//// Virtual serial port over USB TODO NEW VID PID NEEDED!!
+//USBSerial pc(0x0B6A, 0x0042, 0x0001, false);
+
+/***************************************************************************
+ * LEDs Instantiation
+ **************************************************************************/
DigitalOut myled(LED2); // LED = LED2 green
DigitalOut rLED(LED1); // red LED
DigitalOut bLED(LED3); // blue LED
-
-
-/*
-LoRa initialization
-*/
-
-/* Set this flag to '1' to display debug messages on the console */
-#define DEBUG_MESSAGE 1
-
-/* Set this flag to '1' to use the LoRa modulation or to '0' to use FSK modulation */
-#define USE_MODEM_LORA 1
-#define USE_MODEM_FSK !USE_MODEM_LORA
-#define RF_FREQUENCY RF_FREQUENCY_915_0 // Hz
-#define TX_OUTPUT_POWER 14 // 14 dBm
-
-#if USE_MODEM_LORA == 1
-
-#define LORA_BANDWIDTH 125000 // LoRa default, details in SX1276::BandwidthMap
-#define LORA_SPREADING_FACTOR LORA_SF7
-#define LORA_CODINGRATE LORA_ERROR_CODING_RATE_4_5
+//DigitalOut *led3;
-#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
-#define LORA_SYMBOL_TIMEOUT 5 // Symbols
-#define LORA_FIX_LENGTH_PAYLOAD_ON false
-#define LORA_FHSS_ENABLED false
-#define LORA_NB_SYMB_HOP 4
-#define LORA_IQ_INVERSION_ON false
-#define LORA_CRC_ENABLED true
-
-#elif USE_MODEM_FSK == 1
-
-#define FSK_FDEV 25000 // Hz
-#define FSK_DATARATE 19200 // bps
-#define FSK_BANDWIDTH 50000 // Hz
-#define FSK_AFC_BANDWIDTH 83333 // Hz
-#define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
-#define FSK_FIX_LENGTH_PAYLOAD_ON false
-#define FSK_CRC_ENABLED true
-
-#else
- #error "Please define a modem in the compiler options."
-#endif
+/***************************************************************************
+ * I2C Bus Instantiation
+ **************************************************************************/
+I2C i2cBus0(P1_6, P1_7); // I2C of MAX32625PICO
-#define RX_TIMEOUT_VALUE 3500 // in ms
-
-//#define BUFFER_SIZE 32 // Define the payload size here
-#define BUFFER_SIZE 64 // Define the payload size here
-
-/*
- * Global variables declarations
- */
-typedef enum
-{
- LOWPOWER = 0,
- IDLE,
-
- RX,
- RX_TIMEOUT,
- RX_ERROR,
-
- TX,
- TX_TIMEOUT,
-
- CAD,
- CAD_DONE
-} AppStates_t;
-
-volatile AppStates_t State = LOWPOWER;
-
-/*!
- * Radio events function pointer
- */
-static RadioEvents_t RadioEvents;
-
-/*
- * Global variables declarations
- */
-SX1276Generic *Radio;
+bool get_data_flag = false; // used for data tramission frequency
-const uint8_t PingMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'I', 'N', 'G'};// "PING";
-const uint8_t PongMsg[] = { 0xff, 0xff, 0x00, 0x00, 'P', 'O', 'N', 'G'};// "PONG";
+//Timer setup
+Ticker timer_1; // timer for data tramission frequency
+
-uint16_t BufferSize = BUFFER_SIZE;
-uint8_t *Buffer;
-
-DigitalOut *led3;
+void onTimerInterrupt(){
+ get_data_flag = true;
+}
int main() {
-// This mbed-os is copied from Joey's PICO_I2C_FW_Virtual_COM project: FTHR becomes a virtual serial port.
-
-myled = 0; // green led off
-rLED = 0; // red led off
-bLED = 0; // blue led off
-
-/************ Testing purpose ************
-*/
-//pc.printf("Hello World\n");
-//bLED = 1;
-//wait(3);
-//bLED = 0;
-//
-//while (1) {
-// pc.printf("Testing\n");
-// pc.printf("Haha\n");
-// wait(1);
-// myled = !myled;
-// }
-
- pc.printf("Starting LoRa Example\n");
-// GenericPingPong.SX1276PingPong();
- SX1276PingPong();
-}
+/***************************************************************************
-int SX1276PingPong(void)
-{
-// dprintf("TEST" );
- pc.printf("TEST");
-#if( defined ( TARGET_KL25Z ) || defined ( TARGET_LPC11U6X ) )
- DigitalOut *led = new DigitalOut(LED2);
-#elif defined(TARGET_NUCLEO_L073RZ) || defined(TARGET_DISCO_L072CZ_LRWAN1)
- DigitalOut *led = new DigitalOut(LED4); // RX red
- led3 = new DigitalOut(LED3); // TX blue
-#else
- DigitalOut *led = new DigitalOut(LED1); // led = LED1 red FTHR board
- led3 = led; // led3 = led red FTHR board
-#endif
+
+
+ /* Setup begins here: */
+ #if MASTER == 1 // Master Device
+ pc.printf("MAX32625PICO: MASTER DEVICE\r\n");
+ wait(1);
+ #elif SLAVE == 1 // Slave Device
+ pc.printf("MAX32625PICO: SLAVE DEVICE\r\n");
+ wait(1);
+ #endif
+
+ /* Introduction text: */
+ pc.printf("Welcome to the SX1276GenericLib\r\n");
+// wait(5);
+ pc.printf("Starting a simple LoRa PingPong\r\n");
+// wait(5);
+
+ /***************************************************************************
+ * Combined Payload Buffers for LoRa Communications
+ **************************************************************************/
+ uint8_t BufferTx[BufferSizeTx]; // more info in global_buffers.h
+ uint8_t BufferRx[BufferSizeRx]; // more info in global_buffers.h
+
+ /***************************************************************************
+ * Identification Buffers
+ **************************************************************************/
+ #if MASTER == 1 // Master Device
+ uint8_t ID_of_slave[size_signature];
+ #elif SLAVE == 1 // Slave Device
+ uint8_t ID_of_master[size_signature];
+ #endif
+
+ /***************************************************************************
+ * Dummy Data Buffers
+ **************************************************************************/
+ #if MASTER == 1 // Master Device
+ uint8_t curr_dum_to_slave[size_of_dum];
+ #elif SLAVE == 1 // Slave Device
+ uint8_t curr_dum_from_master[size_of_dum];
+ uint8_t prev_dum_from_master[size_of_dum];
+ #endif
+
+ /***************************************************************************
+ * MAX30208 Data Buffers
+ **************************************************************************/
+ #if MASTER == 1 // Master Device
+// uint8_t curr_MAX77650_from_slave[size_of_MAX77650];
+ uint8_t curr_raw_temp_from_slave[size_of_MAX30208];
+// uint8_t prev_MAX77650_from_slave[size_of_MAX77650];
+ uint8_t prev_raw_temp_from_slave[size_of_MAX30208];
+// bool chrg_status = false;; //True = ON False = OFF
+ #elif SLAVE == 1 // Slave Device
+// uint8_t curr_MAX77650_to_master[size_of_MAX77650];
+// bool chrg_status = false; //True = ON False = OFF
+ uint8_t curr_raw_temp_to_master[size_of_MAX30208];
+ #endif
+
- Buffer = new uint8_t[BUFFER_SIZE];
- *led3 = 1; // red LED on
-
-#ifdef B_L072Z_LRWAN1_LORA
- Radio = new SX1276Generic(NULL, MURATA_SX1276,
- LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
- LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5,
- LORA_ANT_RX, LORA_ANT_TX, LORA_ANT_BOOST, LORA_TCXO);
-#else // RFM95
- Radio = new SX1276Generic(NULL, RFM95_SX1276,
- LORA_SPI_MOSI, LORA_SPI_MISO, LORA_SPI_SCLK, LORA_CS, LORA_RESET,
- LORA_DIO0, LORA_DIO1, LORA_DIO2, LORA_DIO3, LORA_DIO4, LORA_DIO5);
-
-#endif
+ /***************************************************************************
+ * MAX44009 Data Buffers
+ **************************************************************************/
+ #if MASTER == 1 // Master Device
+ uint8_t curr_raw_light_from_slave[size_of_MAX44009];
+ uint8_t prev_raw_light_from_slave[size_of_MAX44009];
+ // static int16_t conv_frame_data_from_slave[64];
+ #elif SLAVE == 1 // Slave Device
+ uint8_t curr_raw_light_to_master[size_of_MAX44009];
+ // static char prev_raw_light_data_to_master[size_of_MAX44009];
+ // static int16_t conv_frame_data_to_master[64];
+ #endif
+
+ /***************************************************************************
+ * MAX20361 Data Buffers
+ **************************************************************************/
+ #if MASTER == 1 // Master Device
+ uint8_t curr_raw_AO32_from_slave[size_of_MAX20361];
+ uint8_t prev_raw_AO32_from_slave[size_of_MAX20361];
+ // static int16_t conv_frame_data_from_slave[64];
+ #elif SLAVE == 1 // Slave Device
+ uint8_t curr_raw_AO32_to_master[size_of_MAX20361];
+ // static char prev_raw_AO32_data_to_master[size_of_MAX20361];
+ // static int16_t conv_frame_data_to_master[64];
+ #endif
- uint8_t i;
- bool isMaster = true;
+ /***************************************************************************
+ * Other Data Buffers
+ **************************************************************************/
+ #if MASTER == 1 // Master Device
+// uint8_t curr_raw_other_from_slave[size_of_other];
+// uint8_t prev_raw_other_from_slave[size_of_other];
+ // static int16_t conv_frame_data_from_slave[64];
+ #elif SLAVE == 1 // Slave Device
+// uint8_t curr_raw_other_to_master[size_of_other];
+ // static char prev_raw_other_data_to_master[size_of_other];
+ // static int16_t conv_frame_data_to_master[64];
+ #endif
+
+ /***************************************************************************
+ * Finish Setting up LoRa Radios: This passes in pointers to Buffers to send
+ **************************************************************************/
+ SX1276PingPongSetup(BufferTx, BufferRx, &pc);
+
+// Pointer Tutorials
+// https://www.tutorialspoint.com/cprogramming/c_pointers.htm
+
+ /***************************************************************************
+ * Create Dummy Data For Master and Slave
+ **************************************************************************/
+// Array Tutorials
+// http://www.cplusplus.com/doc/tutorial/arrays/
+ #if MASTER == 1
+// curr_dum_to_slave[0] = {33, 34};
+ curr_dum_to_slave[0] = 33;
+ curr_dum_to_slave[1] = 34;
+ #endif
-// dprintf("SX1276 Ping Pong Demo Application" );
- pc.printf("SX1276 Ping Pong Demo Application");
-// dprintf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0);
- pc.printf("Freqency: %.1f", (double)RF_FREQUENCY/1000000.0);
-// dprintf("TXPower: %d dBm", TX_OUTPUT_POWER);
- pc.printf("TXPower: %d dBm", TX_OUTPUT_POWER);
-#if USE_MODEM_LORA == 1
-// dprintf("Bandwidth: %d Hz", LORA_BANDWIDTH);
- pc.printf("Bandwidth: %d Hz", LORA_BANDWIDTH);
-// dprintf("Spreading factor: SF%d", LORA_SPREADING_FACTOR);
- pc.printf("Spreading factor: SF%d", LORA_SPREADING_FACTOR);
-#elif USE_MODEM_FSK == 1
-// dprintf("Bandwidth: %d kHz", FSK_BANDWIDTH);
- pc.printf("Bandwidth: %d kHz", FSK_BANDWIDTH);
-// dprintf("Baudrate: %d", FSK_DATARATE);
- pc.printf("Baudrate: %d", FSK_DATARATE);
-#endif
- // Initialize Radio driver
- RadioEvents.TxDone = OnTxDone;
- RadioEvents.RxDone = OnRxDone;
- RadioEvents.RxError = OnRxError;
- RadioEvents.TxTimeout = OnTxTimeout;
- RadioEvents.RxTimeout = OnRxTimeout;
- if (Radio->Init( &RadioEvents ) == false) {
- while(1) {
-// dprintf("Radio could not be detected!");
- pc.printf("Radio could not be detected!");
- wait( 1 );
- }
- }
-
+ #if SLAVE == 1
+// curr_raw_temp_to_master[size_of_MAX30208] = {10, 13, 15, 17, 19};
+ curr_raw_temp_to_master[0] = 99;
+ curr_raw_temp_to_master[1] = 99;
+// curr_raw_temp_to_master[2] = 15;
+// curr_raw_temp_to_master[3] = 17;
+// curr_raw_temp_to_master[4] = 19;
+// curr_raw_light_to_master[size_of_MAX44009] = {25, 26, 27, 28, 29};
+ curr_raw_light_to_master[0] = 25;
+ curr_raw_light_to_master[1] = 26;
+// curr_raw_light_to_master[2] = 27;
+// curr_raw_light_to_master[3] = 28;
+// curr_raw_light_to_master[4] = 29;
+// curr_raw_AO32_to_master[size_of_MAX20361] = {99, 100, 101, 102, 103};
+ curr_raw_AO32_to_master[0] = 99;
+ curr_raw_AO32_to_master[1] = 100;
+ curr_raw_AO32_to_master[2] = 101;
+ curr_raw_AO32_to_master[3] = 102;
+// curr_raw_AO32_to_master[4] = 103;
+// curr_raw_other_to_master[size_of_other] = {20, 30, 40, 50, 60, 70, 80};
+// curr_raw_other_to_master[0] = 20;
+// curr_raw_other_to_master[1] = 30;
+// curr_raw_other_to_master[2] = 40;
+// curr_raw_other_to_master[3] = 50;
+// curr_raw_other_to_master[4] = 60;
+// curr_raw_other_to_master[5] = 70;
+// curr_raw_other_to_master[6] = 80;
+
+
+ #endif
+
+ /***************************************************************************
+ * Loop Counter
+ **************************************************************************/
+ int loopCnt = 0;
+
+ #if SLAVE == 1
+ //************* init ticker timer callbacks ****************
+ timer_1.attach(&onTimerInterrupt, 3); //LED3 toggles every 3 seconds
+
+ //************* OT07 Variables ****************
+// char data[5];
+ char rawtempdata[2];
+ char OT07_i2c_add = 0xA0;
+// TempResponse T;
+
+ //************* MAX44009 Variables ****************
+ char rawluxdata[2];
+ char MAX44009_i2c_add = 0x96; // 0b1001 011x
+
+ //************* AO32 Variables ****************
+ char rawOCVdata[2]; // only needs 1
+ char rawCntdata[2];
+ char AO32_i2c_add = 0x2A; // 0b0010 101x
+
+ #endif
- switch(Radio->DetectBoardType()) { // check where DetectBoardType is!!
- case SX1276MB1LAS:
- if (DEBUG_MESSAGE)
-// dprintf(" > Board Type: SX1276MB1LAS <");
- pc.printf(" > Board Type: SX1276MB1LAS <");
- break;
- case SX1276MB1MAS:
- if (DEBUG_MESSAGE)
-// dprintf(" > Board Type: SX1276MB1LAS <");
- pc.printf(" > Board Type: SX1276MB1LAS <");
- case MURATA_SX1276:
- if (DEBUG_MESSAGE)
-// dprintf(" > Board Type: MURATA_SX1276_STM32L0 <");
- pc.printf(" > Board Type: MURATA_SX1276_STM32L0 <");
- break;
- case RFM95_SX1276:
- if (DEBUG_MESSAGE)
-// dprintf(" > HopeRF RFM95xx <");
- pc.printf(" > HopeRF RFM95xx <");
- break;
- default:
-// dprintf(" > Board Type: unknown <");
- pc.printf(" > Board Type: unknown <");
- }
-
- Radio->SetChannel(RF_FREQUENCY );
-
-#if USE_MODEM_LORA == 1
-
- if (LORA_FHSS_ENABLED)
-// dprintf(" > LORA FHSS Mode <");
- pc.printf(" > LORA FHSS Mode <");
- if (!LORA_FHSS_ENABLED)
-// dprintf(" > LORA Mode <");
- pc.printf(" > LORA Mode <");
-
- Radio->SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
- LORA_SPREADING_FACTOR, LORA_CODINGRATE,
- LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
- LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
- LORA_IQ_INVERSION_ON, 2000 );
-
- Radio->SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
- LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
- LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON, 0,
- LORA_CRC_ENABLED, LORA_FHSS_ENABLED, LORA_NB_SYMB_HOP,
- LORA_IQ_INVERSION_ON, true );
-
-#elif USE_MODEM_FSK == 1
-
-// dprintf(" > FSK Mode <");
- pc.printf(" > FSK Mode <");
- Radio->SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
- FSK_DATARATE, 0,
- FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
- FSK_CRC_ENABLED, 0, 0, 0, 2000 );
-
- Radio->SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
- 0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
- 0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, FSK_CRC_ENABLED,
- 0, 0, false, true );
-
-#else
-
-#error "Please define a modem in the compiler options."
-
-#endif
-
- if (DEBUG_MESSAGE)
- pc.printf("Starting Ping-Pong loop");
-// dprintf("Starting Ping-Pong loop");
-
+ while (1) {
+
+ // Application details:
+ //1. Set a fixed timer.
+ //2. Data sent from Master to Slave.
+ //3. Display data and repeat from 1.
+
+
+ #if SLAVE == 1
+ if(get_data_flag) {
+
+ //reset the flag
+ get_data_flag = false;
+ pc.printf("Timer interval reached!\r\n");
+
+ /***************************************************************************
+ * Temperature Sensor Data Measurement
+ **************************************************************************/
+ // obtain register hex values
+ convert_temperature(&i2cBus0, OT07_i2c_add); //send OW convert selected device
+ wait_ms(CONVERT_T_DELAY); //wait 20 ms for convert temperature to complete
+ int temp_error = OT07_read_register(&i2cBus0, OT07_i2c_add, OT07_FIFO_DATA, rawtempdata, 2);
+ pc.printf("OT07 add[%02X] data[%02X] data[%02X]\r\n", OT07_FIFO_DATA, rawtempdata[0], rawtempdata[1]);
+
+ //calculate temperature from data
+ int count = (int)(rawtempdata[0]*256 + rawtempdata[1]);
+ if (count >= 32768)count = count - 65536; // 2s comp
+ double Temp = (double)count*0.005;
+ pc.printf("OT07 temperature[%.3f] status[%d]\r\n", Temp, temp_error);
+
+ //fill raw temp data into the array
+ curr_raw_temp_to_master[0] = rawtempdata[0];
+ curr_raw_temp_to_master[1] = rawtempdata[1];
+
+ // for (int i = 0; i < 3; i++) {
+// convert_temperature(&i2cBus0, i2c_add); //send OW convert selected device
+// wait_ms(CONVERT_T_DELAY); //wait 20 ms for convert temperature to complete
+// T = get_temperature(&i2cBus0, i2c_add);
+// pc.printf("OT07 temperature[%.3f] status[%d]\r\n", T.tempC, T.status);
+// }
- Radio->Rx( RX_TIMEOUT_VALUE );
-
- while( 1 )
- {
-#ifdef TARGET_STM32L4
- // WatchDogUpdate();
-#endif
+ /***************************************************************************
+ * Light Intensity Sensor Data Measurement
+ **************************************************************************/
+ // obtain register hex values
+// int lux_error = MAX44009_read_lux_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_HI, rawluxdata);
+ int lux_error1 = MAX44009_read_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_HI, &rawluxdata[0]);
+ int lux_error2 = MAX44009_read_register(&i2cBus0, MAX44009_i2c_add, MAX44009_LUX_LO, &rawluxdata[1]);
+ pc.printf("MAX44009 hi_add[%02X] hi_data[%02X] lo_add[%02X] lo_data[%02X]\r\n", MAX44009_LUX_HI, rawluxdata[0], MAX44009_LUX_LO, rawluxdata[1]);
+
+ //calculate temperature from data
+ int exponent = int(rawluxdata[0] >> 4);
+ int mantissa = int(rawluxdata[0] << 4) + int(rawluxdata[1]);
+ double lux = 0.045 * mantissa * pow((double) 2, exponent);
+ pc.printf("MAX44009 exponent[%d] mantissa[%d]\r\n", exponent, mantissa);
+ pc.printf("MAX44009 lux[%f] status[%d] status[%d]\r\n", lux, lux_error1, lux_error2);
+
+ //fill raw lux data into the array
+// curr_raw_light_to_master[0] = rawluxdata[0];
+// curr_raw_light_to_master[1] = rawluxdata[1];
+
+
+ /***************************************************************************
+ * Solar Harvester Data Measurement
+ **************************************************************************/
+ // obtain register hex values
+ char AO32ID[2];
+ int id_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_DEVICE_ID, AO32ID); // testing
+ pc.printf("AO32 add[%02X] data[%02X]\r\n", AO32_DEVICE_ID, AO32ID[0]); // should be 0x00: 0x11
+
+ int ocv_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_VOC, rawOCVdata);
+ pc.printf("AO32 add[%02X] data[%02X]\r\n", AO32_VOC, rawOCVdata[0]);
+ int cnt_error = AO32_read_register(&i2cBus0, AO32_i2c_add, AO32_HARV_H, rawCntdata, 2); // burst read 2 bytes
+ pc.printf("AO32 hi_add[%02X] hi_data[%02X] lo_add[%02X] lo_data[%02X]\r\n", AO32_HARV_H, rawCntdata[0], AO32_HARV_L, rawCntdata[1]);
+
+ //calculate open circuit voltage from data
+ double voltage = int(rawOCVdata[0]) / 100;
+ pc.printf("AO32 OCV[%.2f] status[%d]\r\n", voltage, ocv_error);
+
+ //calculate harvesting counts from data
+ int countHi = int(rawCntdata[0] << 8); // might cause trouble, * 256 instead?
+ int countLo = int(rawCntdata[1]);
+ int harvest_counts = countHi + countLo;
+ pc.printf("AO32 HarvesterCnt[%d] status[%d]\r\n", harvest_counts, cnt_error);
+
+ //fill raw AO32 data into the array
+// curr_raw_AO32_to_master[0] = rawOCVdata[0]; // Fill OCV hex first
+// curr_raw_AO32_to_master[1] = rawCntdata[0]; // Fill Harvesting count high byte
+// curr_raw_AO32_to_master[2] = rawCntdata[1]; // Fill Harvesting count low byte
- switch( State )
- {
- case RX:
- *led3 = 0;
- if( isMaster == true )
- {
- if( BufferSize > 0 )
- {
- if( memcmp(Buffer, PongMsg, sizeof(PongMsg)) == 0 )
- {
- *led = !*led;
-// dprintf( "...Pong" );
- pc.printf( "...Pong" );
- // Send the next PING frame
- memcpy(Buffer, PingMsg, sizeof(PingMsg));
- // We fill the buffer with numbers for the payload
- for( i = sizeof(PingMsg); i < BufferSize; i++ )
- {
- Buffer[i] = i - sizeof(PingMsg);
- }
- wait_ms( 10 );
- Radio->Send( Buffer, BufferSize );
- }
- else if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 )
- { // A master already exists then become a slave
-// dprintf( "...Ping" );
- pc.printf( "...Ping" );
- *led = !*led;
- isMaster = false;
- // Send the next PONG frame
- memcpy(Buffer, PongMsg, sizeof(PongMsg));
- // We fill the buffer with numbers for the payload
- for( i = sizeof(PongMsg); i < BufferSize; i++ )
- {
- Buffer[i] = i - sizeof(PongMsg);
- }
- wait_ms( 10 );
- Radio->Send( Buffer, BufferSize );
- }
- else // valid reception but neither a PING or a PONG message
- { // Set device as master ans start again
- isMaster = true;
- Radio->Rx( RX_TIMEOUT_VALUE );
- }
- }
- }
- else
- {
- if( BufferSize > 0 )
- {
- if( memcmp(Buffer, PingMsg, sizeof(PingMsg)) == 0 )
- {
- *led = !*led;
-// dprintf( "...Ping" );
- pc.printf( "...Ping" );
- // Send the reply to the PING string
- memcpy(Buffer, PongMsg, sizeof(PongMsg));
- // We fill the buffer with numbers for the payload
- for( i = sizeof(PongMsg); i < BufferSize; i++ )
- {
- Buffer[i] = i - sizeof(PongMsg);
- }
- wait_ms( 10 );
- Radio->Send( Buffer, BufferSize );
- }
- else // valid reception but not a PING as expected
- { // Set device as master and start again
- isMaster = true;
- Radio->Rx( RX_TIMEOUT_VALUE );
- }
- }
- }
- State = LOWPOWER;
- break;
- case TX:
- *led3 = 1;
- if( isMaster == true )
- {
-// dprintf("Ping..." );
- pc.printf("Ping..." );
- }
- else
- {
-// dprintf("Pong..." );
- pc.printf("Pong..." );
- }
- Radio->Rx( RX_TIMEOUT_VALUE );
- State = LOWPOWER;
- break;
- case RX_TIMEOUT:
- if( isMaster == true )
- {
- // Send the next PING frame
- memcpy(Buffer, PingMsg, sizeof(PingMsg));
- for( i = sizeof(PingMsg); i < BufferSize; i++ )
- {
- Buffer[i] = i - sizeof(PingMsg);
- }
- wait_ms( 10 );
- Radio->Send( Buffer, BufferSize );
- }
- else
- {
- Radio->Rx( RX_TIMEOUT_VALUE );
- }
- State = LOWPOWER;
- break;
- case RX_ERROR:
- // We have received a Packet with a CRC error, send reply as if packet was correct
- if( isMaster == true )
- {
- // Send the next PING frame
- memcpy(Buffer, PingMsg, sizeof(PingMsg));
- for( i = 4; i < BufferSize; i++ )
- {
- Buffer[i] = i - 4;
- }
- wait_ms( 10 );
- Radio->Send( Buffer, BufferSize );
- }
- else
- {
- // Send the next PONG frame
- memcpy(Buffer, PongMsg, sizeof(PongMsg));
- for( i = sizeof(PongMsg); i < BufferSize; i++ )
- {
- Buffer[i] = i - sizeof(PongMsg);
- }
- wait_ms( 10 );
- Radio->Send( Buffer, BufferSize );
- }
- State = LOWPOWER;
- break;
- case TX_TIMEOUT:
- Radio->Rx( RX_TIMEOUT_VALUE );
- State = LOWPOWER;
- break;
- case LOWPOWER:
- sleep();
- break;
- default:
- State = LOWPOWER;
- break;
- }
+ /***************************************************************************
+ * Fill Payload Buffer With Data From Main Program Buffers for next LoRa Transmition
+ **************************************************************************/
+ memcpy(&BufferTx[tx_idx_signature], PongMsg, size_signature);
+ memcpy(&BufferTx[tx_idx_MAX30208], curr_raw_temp_to_master, size_of_MAX30208);
+ memcpy(&BufferTx[tx_idx_MAX44009], curr_raw_light_to_master, size_of_MAX44009);
+ memcpy(&BufferTx[tx_idx_MAX20361], curr_raw_AO32_to_master, size_of_MAX20361);
+// memcpy(&BufferTx[tx_idx_other], curr_raw_other_to_master, size_of_other);
+
+ /***************************************************************************
+ * In case of OnRxTimeout
+ **************************************************************************/
+ // Slave Device, these are values when LoRa communication did not happen
+ // ID_of_master[0] = 'N'; // 0x4E or 78
+// ID_of_master[1] = 'A'; // 0x41 or 65
+// ID_of_master[2] = 'C'; // 0x43 or 67
+// ID_of_master[3] = 'K'; // 0x4B or 75
+// ID_of_master[4] = 'M'; // 0x4D or 77
+// ID_of_master[5] = 'A'; // 0x41 or 65
+// ID_of_master[6] = 'S'; // 0x53 or 83
+// ID_of_master[7] = '!'; // 0x21 or 33
+//
+// curr_dum_from_master[0] = 39;
+// curr_dum_from_master[1] = 40;
+
+ /***************************************************************************
+ * LoRa Communication: Send Sensor Data
+ **************************************************************************/
+// SX1276SensorSend();
+ SX1276PingPong();
+ int sendTime = TimeOnAirSend();
+ pc.printf("Tx Time on Air: %d \r\n", sendTime);
+
+ /***************************************************************************
+ * Fill Main Program Buffers With Data From Received Payload Buffer
+ **************************************************************************/
+ // Slave Device
+ memcpy(ID_of_master, &BufferRx[rx_idx_signature], size_signature);
+ memcpy(curr_dum_from_master, &BufferRx[rx_idx_dum], size_of_dum);
+
+ /***************************************************************************
+ * Slave Device: Print out Master Data
+ **************************************************************************/
+ // memcpy(ID_of_master, &BufferRx[rx_idx_signature], size_signature);
+ pc.printf("Print ID_of_master\r\n");
+ for(int i = 0; i < sizeof(ID_of_master); i++){
+ pc.printf("%d \r\n", ID_of_master[i]);
+ }
+
+ // memcpy(curr_dum_from_master, &BufferRx[rx_idx_dum], size_of_dum);
+// pc.printf("Print Dum From Master\r\n");
+// for(int i = 0; i < sizeof(curr_dum_from_master); i++){
+// pc.printf("%d \r\n", curr_dum_from_master[i]);
+// }
+
+ // print loop counter
+ pc.printf("Loop Counter Slave: %d \r\n", loopCnt);
+ loopCnt = loopCnt + 1;
+ } // end of transmission frequency for slave
+ #endif
+
+
+ #if MASTER == 1
+ /***************************************************************************
+ * Fill Payload Buffer With Data From Main Program Buffers for next LoRa Transmition
+ **************************************************************************/
+ memcpy(&BufferTx[tx_idx_signature], PingMsg, size_signature);
+ memcpy(&BufferTx[tx_idx_dum], curr_dum_to_slave, size_of_dum);
+
+ /***************************************************************************
+ * LoRa Communication: Gateway Receive Sensor Data
+ **************************************************************************/
+// SX1276GateWayReceive(333);
+ SX1276PingPong();
+ int sendTime = TimeOnAirSend();
+ pc.printf("Tx Time on Air: %d \r\n", sendTime);
+
+ /***************************************************************************
+ * Fill Main Program Buffers With Data From Received Payload Buffer
+ **************************************************************************/
+ memcpy(ID_of_slave, &BufferRx[rx_idx_signature], size_signature);
+ memcpy(curr_raw_temp_from_slave, &BufferRx[rx_idx_MAX30208], size_of_MAX30208);
+ memcpy(curr_raw_light_from_slave, &BufferRx[rx_idx_MAX44009], size_of_MAX44009);
+ memcpy(curr_raw_AO32_from_slave, &BufferRx[rx_idx_MAX20361], size_of_MAX20361);
+// memcpy(curr_raw_other_from_slave, &BufferRx[rx_idx_other], size_of_other);
+
+ /***************************************************************************
+ * Master Device: Print out Slave Data
+ **************************************************************************/
+
+ // memcpy(ID_of_slave, &BufferRx[rx_idx_signature], size_signature);
+ //pc.printf("Print ID_of_slave\r\n");
+// for(int i = 0; i < sizeof(ID_of_slave); i++){
+// pc.printf("%d \r\n", ID_of_slave[i]);
+// }
+
+ // memcpy(curr_raw_temp_from_slave, &BufferRx[rx_idx_MAX30208], size_of_MAX30208);
+ pc.printf("Print MAX30208 data\r\n");
+ for(int i = 0; i < sizeof(curr_raw_temp_from_slave); i++){
+ pc.printf("%d \r\n", curr_raw_temp_from_slave[i]);
}
-}
-
-void dump(const char *title, const void *data, int len, bool dwords)
-{
-// dprintf("dump(\"%s\", 0x%x, %d bytes)", title, data, len);
- pc.printf("dump(\"%s\", 0x%x, %d bytes)", title, data, len);
-
- int i, j, cnt;
- unsigned char *u;
- const int width = 16;
- const int seppos = 7;
+
+
+ int count = curr_raw_temp_from_slave[0] * 256 + curr_raw_temp_from_slave[1];
+ if (count >= 32768)count = count - 65536; // 2s comp
+ double tempResult = count * 0.005;
+// pc.printf("OT07 temperature[%.3f] status[%d]\r\n", Temp, error);
+ pc.printf("MSG: [%.3f] [] [] [] []\r\n", tempResult);
+
+
+ // memcpy(curr_raw_light_from_slave, &BufferRx[rx_idx_MAX44009], size_of_MAX44009);
+ // pc.printf("Print MAX44009 data\r\n");
+// for(int i = 0; i < sizeof(curr_raw_light_from_slave); i++){
+// pc.printf("%d \r\n", curr_raw_light_from_slave[i]);
+// }
+
+ // memcpy(curr_raw_AO32_from_slave, &BufferRx[rx_idx_MAX20361], size_of_MAX20361);
+ //pc.printf("Print MAX20361 data\r\n");
+// for(int i = 0; i < sizeof(curr_raw_AO32_from_slave); i++){
+// pc.printf("%d \r\n", curr_raw_AO32_from_slave[i]);
+// }
+
+ // memcpy(curr_raw_other_from_slave, &BufferRx[rx_idx_other], size_of_other);
+ //pc.printf("Print Other data\r\n");
+// for(int i = 0; i < sizeof(curr_raw_other_from_slave); i++){
+// pc.printf("%d \r\n", curr_raw_other_from_slave[i]);
+// }
+
+ // print loop counter
+ pc.printf("Loop Counter Master: %d \r\n", loopCnt);
+ loopCnt = loopCnt + 1;
+
+// wait(3);
+
+ #endif
+
+ ///***************************************************************************
+// * Fill Payload Buffer With Data From Main Program Buffers for next LoRa Transmition
+// **************************************************************************/
+// #if MASTER == 1 // Master Device
+// memcpy(&BufferTx[tx_idx_signature], PingMsg, size_signature);
+// memcpy(&BufferTx[tx_idx_dum], curr_dum_to_slave, size_of_dum);
+// #elif SLAVE == 1 // Slave Device
+// memcpy(&BufferTx[tx_idx_signature], PongMsg, size_signature);
+// memcpy(&BufferTx[tx_idx_MAX30208], curr_raw_temp_to_master, size_of_MAX30208);
+// memcpy(&BufferTx[tx_idx_MAX44009], curr_raw_light_to_master, size_of_MAX44009);
+// memcpy(&BufferTx[tx_idx_MAX20361], curr_raw_AO32_to_master, size_of_MAX20361);
+// memcpy(&BufferTx[tx_idx_other], curr_raw_other_to_master, size_of_other);
+// #endif
+//
+// /***************************************************************************
+// * In case of OnRxTimeout
+// **************************************************************************/
+// #if MASTER == 1 // Master Device, these are values when LoRa communication did not happen
+// ID_of_slave[0] = 10; //
+// ID_of_slave[1] = 11; //
+// ID_of_slave[2] = 12; //
+// ID_of_slave[3] = 13; //
+// ID_of_slave[4] = 14; //
+// ID_of_slave[5] = 15; //
+// ID_of_slave[6] = 16; //
+// ID_of_slave[7] = 17; //
+//
+// #elif SLAVE == 1 // Slave Device, these are values when LoRa communication did not happen
+// ID_of_master[0] = 'N'; // 0x4E or 78
+// ID_of_master[1] = 'A'; // 0x41 or 65
+// ID_of_master[2] = 'C'; // 0x43 or 67
+// ID_of_master[3] = 'K'; // 0x4B or 75
+// ID_of_master[4] = 'M'; // 0x4D or 77
+// ID_of_master[5] = 'A'; // 0x41 or 65
+// ID_of_master[6] = 'S'; // 0x53 or 83
+// ID_of_master[7] = '!'; // 0x21 or 33
+//
+// curr_dum_from_master[0] = 39;
+// curr_dum_from_master[1] = 40;
+//
+// #endif
+//
+//
+// /***************************************************************************
+// * Lora Communications
+// **************************************************************************/
+// #if MASTER == 1 // Master Device
+// SX1276GateWayReceive();
+//
+// #elif SLAVE == 1 // Slave Device
+// SX1276SensorSend();
+// int sendTime = TimeOnAirSend();
+// pc.printf("Tx Time on Air: %d \r\n", sendTime);
+// #endif
+//
+//// SX1276PingPong(); // what changes here?
+//
+//
+// /***************************************************************************
+// * Fill Main Program Buffers With Data From Received Payload Buffer
+// **************************************************************************/
+// /* The master and slave devices will have different requirements for offloading payload */
+// #if MASTER == 1 // Master Device
+// memcpy(ID_of_slave, &BufferRx[rx_idx_signature], size_signature);
+// memcpy(curr_raw_temp_from_slave, &BufferRx[rx_idx_MAX30208], size_of_MAX30208);
+// memcpy(curr_raw_light_from_slave, &BufferRx[rx_idx_MAX44009], size_of_MAX44009);
+// memcpy(curr_raw_AO32_from_slave, &BufferRx[rx_idx_MAX20361], size_of_MAX20361);
+// memcpy(curr_raw_other_from_slave, &BufferRx[rx_idx_other], size_of_other);
+// #elif SLAVE == 1 // Slave Device
+// memcpy(ID_of_master, &BufferRx[rx_idx_signature], size_signature);
+// memcpy(curr_dum_from_master, &BufferRx[rx_idx_dum], size_of_dum);
+// #endif
+//
+//
+// /***************************************************************************
+// * Print Out Data Received
+// **************************************************************************/
+// #if MASTER == 1 // Master Device
+//
+//// memcpy(ID_of_slave, &BufferRx[rx_idx_signature], size_signature);
+// pc.printf("Print ID_of_slave\r\n");
+// for(int i = 0; i < sizeof(ID_of_slave); i++){
+// pc.printf("%d \r\n", ID_of_slave[i]);
+// }
+//
+//// memcpy(curr_raw_temp_from_slave, &BufferRx[rx_idx_MAX30208], size_of_MAX30208);
+// pc.printf("Print MAX30208 data\r\n");
+// for(int i = 0; i < sizeof(curr_raw_temp_from_slave); i++){
+// pc.printf("%d \r\n", curr_raw_temp_from_slave[i]);
+// }
+//
+//// memcpy(curr_raw_light_from_slave, &BufferRx[rx_idx_MAX44009], size_of_MAX44009);
+// pc.printf("Print MAX44009 data\r\n");
+// for(int i = 0; i < sizeof(curr_raw_light_from_slave); i++){
+// pc.printf("%d \r\n", curr_raw_light_from_slave[i]);
+// }
+//
+//// memcpy(curr_raw_AO32_from_slave, &BufferRx[rx_idx_MAX20361], size_of_MAX20361);
+// pc.printf("Print MAX20361 data\r\n");
+// for(int i = 0; i < sizeof(curr_raw_AO32_from_slave); i++){
+// pc.printf("%d \r\n", curr_raw_AO32_from_slave[i]);
+// }
+//
+//// memcpy(curr_raw_other_from_slave, &BufferRx[rx_idx_other], size_of_other);
+// pc.printf("Print Other data\r\n");
+// for(int i = 0; i < sizeof(curr_raw_other_from_slave); i++){
+// pc.printf("%d \r\n", curr_raw_other_from_slave[i]);
+// }
+//
+// // print loop counter
+// pc.printf("Loop Counter Master: %d \r\n", loopCnt);
+// loopCnt = loopCnt + 1;
+//
+// #elif SLAVE == 1 // Slave Device
+//// memcpy(ID_of_master, &BufferRx[rx_idx_signature], size_signature);
+// pc.printf("Print ID_of_master\r\n");
+// for(int i = 0; i < sizeof(ID_of_master); i++){
+// pc.printf("%d \r\n", ID_of_master[i]);
+// }
+//
+//// memcpy(curr_dum_from_master, &BufferRx[rx_idx_dum], size_of_dum);
+// pc.printf("Print Dum From Master\r\n");
+// for(int i = 0; i < sizeof(curr_dum_from_master); i++){
+// pc.printf("%d \r\n", curr_dum_from_master[i]);
+// }
+//
+// // print loop counter
+// pc.printf("Loop Counter Slave: %d \r\n", loopCnt);
+// loopCnt = loopCnt + 1;
+// #endif
+
+ // add delay to slow down
+ // wait(1);
+
+
+// } //end of get_data_flag
+
+ } // end of while(1) loop
+
+} // end of main()
- cnt = 0;
- u = (unsigned char *)data;
- while (len > 0) {
-// rprintf("%08x: ", (unsigned int)data + cnt);
-// pc.printf("%08x: ", (unsigned int)data + cnt); //comment out
- if (dwords) {
- unsigned int *ip = ( unsigned int *)u;
-// rprintf(" 0x%08x\r\n", *ip);
-// pc.printf(" 0x%08x\r\n", *ip); //comment out
- u+= 4;
- len -= 4;
- cnt += 4;
- continue;
- }
- cnt += width;
- j = len < width ? len : width;
- for (i = 0; i < j; i++) {
-// rprintf("%2.2x ", *(u + i));
-// pc.printf("%2.2x ", *(u + i)); //comment out
- if (i == seppos)
-// rprintf(" ");
- pc.printf(" "); //comment out
- }
-// rprintf(" ");
-// pc.printf(" "); //comment out
- if (j < width) {
- i = width - j;
- if (i > seppos + 1)
-// rprintf(" ");
-// pc.printf(" "); //comment out
- while (i--) {
-// rprintf("%s", " ");
-// pc.printf("%s", " "); //comment out
- }
- }
- for (i = 0; i < j; i++) {
- int c = *(u + i);
- if (c >= ' ' && c <= '~')
-// rprintf("%c", c);
- pc.printf("%c", c); //comment out
- else
-// rprintf(".");
- pc.printf("."); //comment out
- if (i == seppos)
-// rprintf(" ");
- pc.printf(" "); //comment out
- }
- len -= width;
- u += width;
-// rprintf("\r\n");
-// pc.printf("\r\n"); //comment out
- }
-// rprintf("--\r\n");
-// pc.printf("--\r\n"); //comment out
-}
-
-
-void OnTxDone(void *radio, void *userThisPtr, void *userData)
-{
- Radio->Sleep( );
- State = TX;
- if (DEBUG_MESSAGE)
- //dprintf("> OnTxDone");
- pc.printf("> OnTxDone");
-}
-
-void OnRxDone(void *radio, void *userThisPtr, void *userData, uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
-{
- Radio->Sleep( );
- BufferSize = size;
- memcpy( Buffer, payload, BufferSize );
- State = RX;
- if (DEBUG_MESSAGE)
- pc.printf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr);
-// dprintf("> OnRxDone: RssiValue=%d dBm, SnrValue=%d", rssi, snr);
-// dump("Data:", payload, size);
- dump("Data:", payload, size, false);
-}
-
-void OnTxTimeout(void *radio, void *userThisPtr, void *userData)
-{
- *led3 = 0;
- Radio->Sleep( );
- State = TX_TIMEOUT;
- if(DEBUG_MESSAGE)
- pc.printf("> OnTxTimeout");
-// dprintf("> OnTxTimeout");
-}
-
-void OnRxTimeout(void *radio, void *userThisPtr, void *userData)
-{
- *led3 = 0;
- Radio->Sleep( );
- Buffer[BufferSize-1] = 0;
- State = RX_TIMEOUT;
- if (DEBUG_MESSAGE)
- pc.printf("> OnRxTimeout");
-// dprintf("> OnRxTimeout");
-}
-
-void OnRxError(void *radio, void *userThisPtr, void *userData)
-{
- Radio->Sleep( );
- State = RX_ERROR;
- if (DEBUG_MESSAGE)
- pc.printf("> OnRxError");
-// dprintf("> OnRxError");
-}
-
-
-
-//#endif
