//
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include <SPI.h>
#include <SoftwareSerial.h>
#include <BluetoothSerial.h>
#include <ads1262.h>
#define PGA 32 // Programmable Gain = 32
#define VREF 2.50 // Internal reference of 2.5V
#define BUFF_SIZE 32
#define BYTE_LEN 4 // Number of bytes for 32 bit int
ads1262 ADS; // ADS class
BluetoothSerial SerialBT;
int32_t val1;
int32_t val2;
int32_t val3;
int32_t val4;
int32_t val5;
int32_t data1;
int32_t data2;
int32_t data3;
int32_t data4;
int32_t timeData;
int32_t t_start;
int32_t t_finish;
volatile char *data_struct;
volatile char buff_array[6];
volatile long data_array[4];
volatile signed long counts;
volatile int isDataReady = 0;
TaskHandle_t TaskGetData;
TaskHandle_t TasksendPC;
QueueHandle_t Queue1;
QueueHandle_t Queue2;
QueueHandle_t Queue3;
QueueHandle_t Queue4;
QueueHandle_t QueueT;
//SemaphoreHandle_t semaphore;
void setup()
{
portDISABLE_INTERRUPTS();
// initalize the data ready and chip select pins:
pinMode(ADS1262_DRDY_PIN, INPUT); // Data ready input line
pinMode(ADS1262_CS_PIN, OUTPUT); // Chip enable output line
pinMode(ADS1262_START_PIN, OUTPUT); // Start
pinMode(ADS1262_PWDN_PIN, OUTPUT); // Power down output
attachInterrupt(digitalPinToInterrupt(ADS1262_DRDY_PIN), awaitDRDY, FALLING);
SerialBT.begin("PLAKA PLATES");
// Serial.begin(500000); // Max Baud Rate
ADS.ads1262_Init(); // Initialise ADS1262
Queue1 = xQueueCreate(500, sizeof(int32_t));
Queue2 = xQueueCreate(500, sizeof(int32_t));
Queue3 = xQueueCreate(500, sizeof(int32_t));
Queue4 = xQueueCreate(500, sizeof(int32_t));
QueueT = xQueueCreate(500, sizeof(int32_t));
setCpuFrequencyMhz(240);
xTaskCreatePinnedToCore(loopGet, "TaskGetData", 4096, NULL, 3, &TaskGetData, 1); // 0
xTaskCreatePinnedToCore(loopSend, "TasksendPC", 4096, NULL, 3, &TasksendPC, 1); // 1
portENABLE_INTERRUPTS();
}
void loop()
{
vTaskDelete (NULL);
}
void loopGet(void *parameter)
{
for (;;) {
t_start = micros(); // micros()
// Cycle through differential channels
val1 = read_channel(0x01);
val2 = read_channel(0x23);
val3 = read_channel(0x45);
val4 = read_channel(0x67);
val5 = read_channel(0x01); // Dummy read to not affect 4th load cell reading
xQueueSend(Queue1, &val1, portMAX_DELAY);
xQueueSend(Queue2, &val2, portMAX_DELAY);
xQueueSend(Queue3, &val3, portMAX_DELAY);
xQueueSend(Queue4, &val4, portMAX_DELAY);
t_finish = micros() - t_start;
xQueueSend(QueueT, &t_finish, portMAX_DELAY);
}
}
void loopSend(void *parameter)
{
for (;;) {
xQueueReceive(Queue1, &data1, portMAX_DELAY);
xQueueReceive(Queue2, &data2, portMAX_DELAY);
xQueueReceive(Queue3, &data3, portMAX_DELAY);
xQueueReceive(Queue4, &data4, portMAX_DELAY);
xQueueReceive(QueueT, &timeData, portMAX_DELAY);
// Serial Port Debug
// printf("%i,%i,%i,%i,%i\n", data1, data2, data3, data4, timeData);
// Send binary to Bluetooth Serial Port
SerialBT.write((byte*)&data1, BYTE_LEN);
SerialBT.write((byte*)&data2, BYTE_LEN);
SerialBT.write((byte*)&data3, BYTE_LEN);
SerialBT.write((byte*)&data4, BYTE_LEN);
SerialBT.write((byte*)&timeData, BYTE_LEN);
SerialBT.write('\n');
}
}
void awaitDRDY() { // IRAM_ATTR
isDataReady = 1;
// xSemaphoreGive(semaphore);
}
// Read adc binary data
int32_t read_channel(char channel)
{
// Channel multiplexer channel
ADS.ads1262_Reg_Write(INPMUX, channel);
// Wait until ADC is ready for conversion // while (digitalRead(ADS1262_DRDY_PIN) != LOW){
isDataReady = 0;
while (isDataReady != 1) {}
// xSemaphoreTake(semaphore, portMAX_DELAY); // Faster ISR response to DRDY signal
// Dump Binary Data
data_struct = ADS.ads1262_Read_Data();
for (int i = 1; i < 5; ++i)
{
buff_array[i] = *(data_struct + i);
data_array[i] = (unsigned char)buff_array[i];
}
counts = (signed long) (((unsigned long)data_array[0]<<24) | ((unsigned long)data_array[1]<<16) | (data_array[2]<<8) | data_array[3]);
return counts;
}