Files
ACD528xx_Test_Project/tasks/aconnoTasks.cpp
T
Dominik Kotarski 937e762857 Changed interrupt architecture and improved joystick task
Now supports buffered interrupts on ioexpander and ignores constantly pressed buttons. Joystick task is also changed to support buffered messages.
2019-03-18 15:47:12 +01:00

748 lines
22 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 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;
extern uint8_t irFlag;
extern QueueHandle_t interruptSourceQueue;
extern QueueHandle_t interruptSourceQueueSecondary;
IOEXP_INT_SOURCE intSourceSend = IOEXP_INT_SOURCE::NOTHING;
IOEXP_INT_SOURCE intSourceRecv = IOEXP_INT_SOURCE::NOTHING;
IOEXP_INT_SOURCE intSourceSendSecondary = IOEXP_INT_SOURCE::NOTHING;
IOEXP_INT_SOURCE intSourceRecvSecondary = IOEXP_INT_SOURCE::NOTHING;
bool getInterruptSourceFlag = true;
/**< 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 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))
{
// last interrupt reads nothing pressed
intSourceSend = IOEXP_INT_SOURCE::NOTHING;
getInterruptSourceFlag = true;
}
// only send a message if this is the first interrupt after a while
else if(getInterruptSourceFlag)
{
getInterruptSourceFlag=false;
intSourceSend = (IOEXP_INT_SOURCE)intSrcPin;
xQueueSendToBack(interruptSourceQueue,
(void *) &intSourceSend,
((TickType_t)10) != pdPASS);
}
//todo: Tasks are currently resumed from their while loop. There
// Is no consistent starting point and if returning to an
// example, it will not be started from the beginning
}
}
}
void menuStateMachineTaskFunction(void *arg)
{
while(1)
{
if(xQueueReceive(interruptSourceQueue,
&(intSourceRecv),
portMAX_DELAY))
{
// send interrupt source into secondary queue to potentially be
// used by states
intSourceSendSecondary = intSourceRecv;
xQueueSendToBack(interruptSourceQueueSecondary,
(void *) &intSourceSendSecondary,
((TickType_t)10) != pdPASS);
/* Todo: delete could make problems if the same state is returned */
State *newMenuState = pMenuState->handleEvent(intSourceRecv);
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: consider dynamically deleting tasks after a certain treshold
in case memory becomes a problem */
}
}
void ldrFunction(void *arg)
{
vTaskSuspend(NULL);
aconnoSensorData_t data;
//Leave this zero so the next if statement must be executed at the begining
float voltage = 0;
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);
aconnoSensorData_t data;
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);
aconnoSensorData_t data;
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)
{
// Erase previous data from the buffer
epd.writeString(buffer,110,100,1);
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
//Create a string
n = sprintf(buffer, "%s%d.%d\n", tmpSign, tmpInt1, tmpInt2);
// If n is negative, an error appeared
NRF_LOG_INFO("Now I'm here after sprintf, it is %d chars long", n);
epd.writeString(buffer,110,100,0); // Write new data to the buffer
epd.write();
}
vTaskDelay(EXAMPLE_DELAY_MS);
}
}
void gyroFunction(void *arg)
{
vTaskSuspend(NULL);
aconnoSensorData_t data;
aconnoSensorVec_t gyroVal;
i2c.enable();
mems.enable(SENSOR_TYPE::GYROSCOPE);
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)
{
vTaskSuspend(NULL);
AconnoBLE &myBLE = *getAconnoBLEInstance();
static advertisementPacket_t advertisementPacket;
myBLE.advRawUpdate((uint8_t*)&advertisementPacket,
sizeof(advertisementPacket_t));
static aconnoSensorData_t data;
advertisementPacket.settingsFlag = SETTINGS_SUPPORT_FLAG;
advertisementPacket.versionNumber = PRODUCT_VERSION;
myBLE.startAdvertising(APP_ADV_INTERVAL, APP_ADV_DURATION);
adc.calibrate();
vTaskDelay(25); // Small delay required by adc to calibrate
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(&data, SENSOR_TYPE::ACCELEROMETER);
advertisementPacket.accelerometer[0] =
(int16_t)data.acceleration.x;
advertisementPacket.accelerometer[1] =
(int16_t)data.acceleration.y;
advertisementPacket.accelerometer[2] =
(int16_t)data.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+1)%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();
NRF_LOG_INFO("Image updated.");
}
}
void infraredFunction(void *arg)
{
irFlag = 0;
vTaskSuspend(NULL);
uint8_t SOS[] = {
0, 1, 0, 1, 0, 1, // S
0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, // O
0, 1, 0, 1, 0, 1, // S
0, 0, 0, 0, 0, 0, 0, 0, 0 // Pause
};
uint8_t sosSize = sizeof(SOS);
uint8_t irIndex = 0;
while(1)
{
if(irFlag==1)
{
irIndex = 0;
irFlag = 0;
}
if(SOS[irIndex]==0)
{
irLed.write(GPIO_STATE::LOW);
}
else
{
irLed.write(GPIO_STATE::HIGH);
}
vTaskDelay(INFRARED_DELAY_MS);
irIndex = (irIndex+1)%sosSize;
}
}
void ledsFunction(void *arg)
{
vTaskSuspend(NULL);
while(1)
{
if(expdr.readGPIOBit(MCP23017_SW2) == 0)
{
if(expdr.readGPIOBit(MCP23017_LED2)==0)
{
expdr.writeGPIOBit(1, MCP23017_LED2);
}
else
{
expdr.writeGPIOBit(0, MCP23017_LED2);
}
}
else if(sw1.read()==GPIO_STATE::LOW)
{
ld1.toggle();
}
vTaskDelay(EXAMPLE_DELAY_MS);
}
}
void relayFunction(void *arg)
{
vTaskSuspend(NULL);
while(1)
{
if(expdr.readGPIOBit(MCP23017_SW2) == 0)
{
if(expdr.readGPIOBit(MCP23017_REL2)==0)
{
expdr.writeGPIOBit(1, MCP23017_REL2);
}
else
{
expdr.writeGPIOBit(0, MCP23017_REL2);
}
}
else if(sw1.read()==GPIO_STATE::LOW)
{
if(expdr.readGPIOBit(MCP23017_REL1)==0)
{
expdr.writeGPIOBit(1, MCP23017_REL1);
}
else
{
expdr.writeGPIOBit(0, MCP23017_REL1);
}
}
vTaskDelay(EXAMPLE_DELAY_MS);
}
}
void joystickTaskFunction(void *arg)
{
//center coordinates
uint16_t xCoordCenter = 100;
uint16_t yCoordCenter = 103;
uint16_t centerOffset = 20;
uint16_t ballRadius = 7;
bool color = 0;
enum class BALL_POSITION
{
UP,
DOWN,
LEFT,
RIGHT,
CENTER
};
BALL_POSITION ballPosition = BALL_POSITION::CENTER;
vTaskSuspend(NULL);
epd.drawImage(joystick1);
epd.fillCircle(xCoordCenter,
yCoordCenter,
ballRadius,
color);
epd.writeFull();
while(1)
{
if(xQueueReceive(interruptSourceQueueSecondary,
&(intSourceRecvSecondary),
portMAX_DELAY))
{
epd.drawImage(joystick1);
switch (intSourceRecvSecondary)
{
case IOEXP_INT_SOURCE::JOY_UP:
{
switch (ballPosition)
{
case BALL_POSITION::CENTER:
{
ballPosition = BALL_POSITION::UP;
break;
}
case BALL_POSITION::DOWN:
{
ballPosition = BALL_POSITION::CENTER;
break;
}
default:
{
break;
}
}
break;
}
case IOEXP_INT_SOURCE::JOY_DOWN:
{
switch (ballPosition)
{
case BALL_POSITION::CENTER:
{
ballPosition = BALL_POSITION::DOWN;
break;
}
case BALL_POSITION::UP:
{
ballPosition = BALL_POSITION::CENTER;
break;
}
default:
{
break;
}
}
break;
}
case IOEXP_INT_SOURCE::JOY_LEFT:
{
switch (ballPosition)
{
case BALL_POSITION::CENTER:
{
ballPosition = BALL_POSITION::LEFT;
break;
}
case BALL_POSITION::RIGHT:
{
ballPosition = BALL_POSITION::CENTER;
break;
}
default:
{
break;
}
}
break;
}
case IOEXP_INT_SOURCE::JOY_RIGHT:
{
switch (ballPosition)
{
case BALL_POSITION::CENTER:
{
ballPosition = BALL_POSITION::RIGHT;
break;
}
case BALL_POSITION::LEFT:
{
ballPosition = BALL_POSITION::CENTER;
break;
}
default:
{
break;
}
}
break;
}
case IOEXP_INT_SOURCE::JOY_CENTER:
{
ballPosition = BALL_POSITION::CENTER;
break;
}
default:
{
break;
}
}
switch (ballPosition)
{
case BALL_POSITION::UP:
{
epd.fillCircle(xCoordCenter,
yCoordCenter-centerOffset,
ballRadius,
color);
epd.write();
break;
}
case BALL_POSITION::DOWN:
{
epd.fillCircle(xCoordCenter,
yCoordCenter+centerOffset,
ballRadius,
color);
epd.write();
break;
}
case BALL_POSITION::LEFT:
{
epd.fillCircle(xCoordCenter-centerOffset,
yCoordCenter,
ballRadius,
color);
epd.write();
break;
}
case BALL_POSITION::RIGHT:
{
epd.fillCircle(xCoordCenter+centerOffset,
yCoordCenter,
ballRadius,
color);
epd.write();
break;
}
case BALL_POSITION::CENTER:
{
epd.fillCircle(xCoordCenter,
yCoordCenter,
ballRadius,
color);
epd.write();
break;
}
default:
{
break;
}
}
}
}
}
void rgbLedTaskFunction(void *arg)
{
vTaskSuspend(NULL);
while(1)
{
uint8_t x = rand()%7;
ledRed.write((GPIO_STATE)((x>>2)&1));
ledGreen.write((GPIO_STATE)((x>>1)&1));
ledBlue.write((GPIO_STATE)((x>>0)&1));
vTaskDelay(rand()%250); // Wait some random time
}
}