Files
ACD528xx_Test_Project/tasks/aconnoTasks.cpp
T
2019-03-15 18:04:51 +01:00

437 lines
13 KiB
C++

/**
* @file aconnoTasks.cpp
* @author Jurica Resetar (Jurica@aconno.de)
* @brief
* @version 0.1
* @date 2019-01-24
*
* @copyright Copyright (c) 2019
*
*/
#include "aconnoTasks.h"
#include <typeinfo>
#include "timers.h"
extern TaskHandle_t menuStateMachineTask;
extern TaskHandle_t LDRTask;
extern TaskHandle_t JoystickExampleTask;
extern TaskHandle_t getInterruptSourceTask;
extern AconnoSPI spi;
extern AconnoI2C i2c;
extern AconnoSAADC adc;
extern AconnoGPIO ledRed;
extern AconnoGPIO ledBlue;
extern AconnoGPIO ledGreen;
extern AconnoGPIO irLed;
extern AconnoGPIO sw1;
extern AconnoGPIO ld1;
extern GDEP015OC1 epd;
extern MCP23017 expdr;
extern LSM9DS1 mems;
extern LDR light;
extern Potentiometer pot;
extern MCP9700 ambientTemperature;
extern SemaphoreHandle_t getInterruptSourceSemaphore;
extern State *pMenuState;
IOEXP_INT_SOURCE intSource = IOEXP_INT_SOURCE::NOTHING;
aconnoSensorData_t data;
uint8_t MSD[26] = {0x59, 0x00, 0x17, 0xCF, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
extern uint8_t irFlag;
/**< Definition of Logger thread. */
#if NRF_LOG_ENABLED
TaskHandle_t mLoggerThread;
void loggerThread(void *arg);
#endif
void logTaskFunction(void *arg)
{
#if NRF_LOG_ENABLED
if(pdPASS != xTaskCreate(loggerThread, "LOGGER", 256, NULL, 1,
&mLoggerThread))
{
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
}
NRF_LOG_INFO("Logger task started.");
#endif
while(1)
{
vTaskSuspend(NULL);
}
}
void testCallback()
{
ledRed.toggle();
ledBlue.toggle();
ledGreen.toggle();
}
void interruptCallback()
{
// resume task to get button
xSemaphoreGive(getInterruptSourceSemaphore);
}
void getInterruptSourceFunction(void *arg)
{
while(1)
{
if(xSemaphoreTake(getInterruptSourceSemaphore, portMAX_DELAY) == pdTRUE)
{
uint8_t intSrcPin = expdr.getLastInterruptPin();
if(expdr.readGPIOBit(intSrcPin))
{
intSource = IOEXP_INT_SOURCE::NOTHING;
}
else
{
intSource = (IOEXP_INT_SOURCE)intSrcPin;
vTaskResume(menuStateMachineTask);
}
/* let mainMenu stateMachine task handle the button pressed*/
//todo: consider implementing a message queue for interrupts
// it would enable a buffered response to interrupts
// which for example could be used for letting go of joystick
// in joystick example and centering the ball as a result
}
}
}
void menuStateMachineTaskFunction(void *arg)
{
while(1)
{
vTaskSuspend(NULL);
/* todo: delete could make problems if the same state is returned */
State *newMenuState = pMenuState->handleEvent(intSource);
if(typeid(*pMenuState) != typeid(*newMenuState))
{
delete pMenuState;
pMenuState = newMenuState;
pMenuState->displayOnEpd();
}
else
{
delete pMenuState;
pMenuState = newMenuState;
}
/* todo: consider pre-creating all state objects instead of
dinamically creating and destroying objects */
/* todo: maybe set up a local variable to shadow global if interrupts
make problems */
/* todo: consider dynamically deleting tasks after a certain treshold
in case memory becomes a problem */
}
}
/**
* @brief Light detection function
*
* @param arg no arguments are expected
*/
void ldrFunction(void *arg)
{
vTaskSuspend(NULL);
float voltage = 0; //Leave this zero so the next if statement must be executed at the begining
uint16_t x_coord;
uint16_t y_coord;
vTaskSuspend(NULL);
adc.calibrate();
while(1)
{
if(light.updateData(&data))
{
//TODO: add data representation on epaper
NRF_LOG_INFO("LDR value is: " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.light));
}
if(data.light<voltage-0.05f || data.light>voltage+0.05f)
{
voltage = data.light;
if (voltage<0.10f) { x_coord = 40; y_coord = 135; }
else if(voltage<0.20f) { x_coord = 43; y_coord = 130; }
else if(voltage<0.30f) { x_coord = 49; y_coord = 110; }
else if(voltage<0.40f) { x_coord = 59; y_coord = 95; }
else if(voltage<0.50f) { x_coord = 70; y_coord = 86; }
else if(voltage<0.60f) { x_coord = 90; y_coord = 80; }
else if(voltage<0.70f) { x_coord = 110; y_coord = 80; }
else if(voltage<0.80f) { x_coord = 130; y_coord = 86; }
else if(voltage<0.90f) { x_coord = 141; y_coord = 95; }
else { x_coord = 100; y_coord = 85; }
epd.drawImage(ldr1);
epd.drawLine(100,135,x_coord,y_coord,0);
epd.write();
}
vTaskDelay(EXAMPLE_DELAY_MS);
}
}
void potentiometerFunction(void *arg)
{
vTaskSuspend(NULL);
static float voltage;
adc.calibrate();
pot.updateData(&data);
voltage = data.voltage;
pot.updateData(&data);
voltage = data.voltage;
while(1)
{
pot.updateData(&data);
if(data.voltage>voltage+0.05f)
{
NRF_LOG_INFO("My pot is CW and its voltage is " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.voltage));
epd.drawImage(potCW);
epd.write();
}else if(data.voltage<voltage-0.05f)
{
epd.drawImage(potCCW);
epd.write();
NRF_LOG_INFO("My pot is CCW and its voltage is " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.voltage));
}
voltage = data.voltage;
vTaskDelay(EXAMPLE_DELAY_MS);
}
}
void temperatureFunction(void *arg)
{
vTaskSuspend(NULL);
float temperature=0;
char buffer[10];
int n;
adc.calibrate();
epd.drawImage(temp1);
epd.writeFull();
while(1)
{
ambientTemperature.updateData(&data);
NRF_LOG_INFO("The ambient temperature is " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.temperature));
if(data.temperature>temperature+0.5f || data.temperature<temperature-0.5f)
{
epd.writeString(buffer,110,100,1); // Erase previous data from the buffer
temperature = data.temperature;
char tmpSign = (temperature < 0) ? '-' : ' ';
float tmpVal = (temperature < 0) ? -temperature : temperature;
int tmpInt1 = tmpVal; // Get the integer
float tmpFrac = tmpVal - tmpInt1; // Get fraction
int tmpInt2 = trunc(tmpFrac * 100); // Turn fraction into integer
n = sprintf(buffer, "%s%d.%d\n", tmpSign, tmpInt1, tmpInt2); //Create a string
NRF_LOG_INFO("Now I'm here after sprintf, it is %d chars long", n); //if n is negative, an error appeared
epd.writeString(buffer,110,100,0); //Write new data to the buffer
epd.write();
}
vTaskDelay(EXAMPLE_DELAY_MS);
}
}
void gyroFunction(void *arg)
{
vTaskSuspend(NULL);
i2c.enable();
mems.enable(SENSOR_TYPE::GYROSCOPE);
aconnoSensorVec_t gyroVal;
while(1)
{
mems.updateData(&data, SENSOR_TYPE::GYROSCOPE);
gyroVal.x = data.gyro.x * (245.0f/32768.0f);
gyroVal.y = data.gyro.y * (245.0f/32768.0f);
gyroVal.z = data.gyro.z * (245.0f/32768.0f);
//debugging purpose
NRF_LOG_INFO("Magnitude is x:" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.gyro.x));
NRF_LOG_INFO("Magnitude is y:" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.gyro.y));
NRF_LOG_INFO("Magnitude is z:" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.gyro.z));
if((gyroVal.x>50.0f) || (gyroVal.x<(-50.0f)) ||
(gyroVal.y>50.0f) || (gyroVal.y<(-50.0f)) ||
(gyroVal.z>50.0f) || (gyroVal.z<(-50.0f)))
{
switch(rand()%6+1)
{
#define C(x) epd.fillCircle(100,100,10,x)
#define CL(x) epd.fillCircle( 75,100,10,x)
#define CR(x) epd.fillCircle(125,100,10,x)
#define TL(x) epd.fillCircle( 75, 75,10,x)
#define TR(x) epd.fillCircle(125, 75,10,x)
#define BL(x) epd.fillCircle( 75,125,10,x)
#define BR(x) epd.fillCircle(125,125,10,x)
case 1:
C(0);
epd.write();
C(1);
break;
case 2:
TL(0); BR(0);
epd.write();
TL(1); BR(1);
break;
case 3:
C(0); TL(0); BR(0);
epd.write();
C(1); TL(1); BR(1);
break;
case 4:
TL(0); TR(0); BL(0); BR(0);
epd.write();
TL(1); TR(1); BL(1); BR(1);
break;
case 5:
C(0); TL(0); TR(0); BL(0); BR(0);
epd.write();
C(1); TL(1); TR(1); BL(1); BR(1);
break;
case 6:
CL(0); CR(0); TL(0); TR(0); BL(0); BR(0);
epd.write();
CL(1); CR(1); TL(1); TR(1); BL(1); BR(1);
break;
default:
break;
#undef BR
#undef BL
#undef TR
#undef TL
#undef CR
#undef CL
#undef C
}
}
vTaskDelay(EXAMPLE_DELAY_MS);
}
}
void bleBeaconFunction(void *arg)
{
AconnoBLE &myBLE = *getAconnoBLEInstance();
static advertisementPacket_t advertisementPacket;
myBLE.advRawUpdate((uint8_t*)&advertisementPacket,
sizeof(advertisementPacket_t));
static aconnoSensorData_t memsData;
adc.calibrate();
vTaskDelay(100);
vTaskSuspend(NULL);
/* init i2c */
while(1)
{
if(advertisementPacket.advType == FIRST_PACKET_ID)
{
mems.powerOn();
bool accelEnabled = mems.enable(SENSOR_TYPE::ACCELEROMETER);
NRF_LOG_INFO("Accel enabled: %d", accelEnabled);
mems.enable(SENSOR_TYPE::GYROSCOPE);
mems.enable(SENSOR_TYPE::MAGNETIC_FIELD);
mems.updateData(&memsData, SENSOR_TYPE::ACCELEROMETER);
advertisementPacket.accelerometer[0] =
(int16_t)memsData.acceleration.x;
advertisementPacket.accelerometer[1] =
(int16_t)memsData.acceleration.y;
advertisementPacket.accelerometer[2] =
(int16_t)memsData.acceleration.z;
mems.updateData(&data, SENSOR_TYPE::GYROSCOPE);
advertisementPacket.gyroscope[0] = (int16_t) data.gyro.x;
advertisementPacket.gyroscope[1] = (int16_t) data.gyro.y;
advertisementPacket.gyroscope[2] = (int16_t) data.gyro.z;
mems.updateData(&data, SENSOR_TYPE::MAGNETIC_FIELD);
advertisementPacket.magnetometer[0] = (int16_t) data.magnetic.x;
advertisementPacket.magnetometer[1] = (int16_t) data.magnetic.y;
advertisementPacket.magnetometer[2] = (int16_t) data.magnetic.z;
NRF_LOG_INFO("Accel: x=%d, y=%d, z=%d",
advertisementPacket.accelerometer[0],
advertisementPacket.accelerometer[1],
advertisementPacket.accelerometer[2]
);
NRF_LOG_INFO("Gyro: x=%d, y=%d, z=%d",
advertisementPacket.gyroscope[0],
advertisementPacket.gyroscope[1],
advertisementPacket.gyroscope[2]
);
NRF_LOG_INFO("Mag: x=%d, y=%d, z=%d",
advertisementPacket.magnetometer[0],
advertisementPacket.magnetometer[1],
advertisementPacket.magnetometer[2]
);
}
else
{
light.updateData(&data);
advertisementPacket.light = data.light;
NRF_LOG_INFO("Light: " NRF_LOG_FLOAT_MARKER "\r\n",
NRF_LOG_FLOAT(data.light));
ambientTemperature.updateData(&data);
advertisementPacket.temp = data.temperature;
NRF_LOG_INFO("Temp: " NRF_LOG_FLOAT_MARKER "\r\n",
NRF_LOG_FLOAT(data.temperature));
advertisementPacket.joy = checkState(&expdr, &sw1);
NRF_LOG_INFO("Joy: %d \r\n", checkState(&expdr, &sw1));
pot.updateData(&data);
advertisementPacket.pot = (uint8_t)(data.voltage*255);
NRF_LOG_INFO("Pot: " NRF_LOG_FLOAT_MARKER "\r\n",
NRF_LOG_FLOAT(data.voltage*255));
}
myBLE.advRawUpdate((uint8_t*)&advertisementPacket,
sizeof(advertisementPacket_t));
advertisementPacket.advType = (advertisementPacket.advType++)%2;
vTaskDelay(BEACON_DELAY_MS);
}
}
void imageUpdate(uint8_t const *data, size_t dataLen)
{
static uint16_t epdPointer = 0;
for(uint16_t x = 0; x < dataLen; x++)
{
epd.fill(data[x], epdPointer);
epdPointer++;
}
if(epdPointer == EPD_BUFFER_SIZE)
{
epdPointer = 0;
epd.writeFull();
}
}