Files
ACD528xx_Test_Project/tasks/aconnoTasks.cpp
T

792 lines
23 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"
#include "aconnoBeeper.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 Servo servo;
extern SemaphoreHandle_t getInterruptSourceSemaphore;
extern State *pMenuState;
extern uint8_t irFlag;
extern QueueHandle_t interruptSourceQueue;
IOEXP_INT_SOURCE intSourceSend = IOEXP_INT_SOURCE::NOTHING;
IOEXP_INT_SOURCE intSourceRecv = IOEXP_INT_SOURCE::NOTHING;
IOEXP_INT_SOURCE intSourceRecvSecondary = IOEXP_INT_SOURCE::NOTHING;
extern AconnoBeeper beeper;
extern bool gBleTaskSuspend;
/**
* @brief A flag to help with initial drawing of the joystick example
*
*/
extern uint8_t joyFlag;
/**
* @brief secondary interrupt Source queue meant to be used by tasks that
* require interrupts from ioexpander
*/
extern QueueHandle_t interruptSourceQueueSecondary;
/**
* @brief Flag which is used to prevent interrupts that are sent by ioexpander
* while a button is continuously pressed
*/
bool getInterruptSourceFlag = true;
extern TimerHandle_t intFlagTimer;
/**< 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)TICKS_TO_BLOCK_DEFAULT) != pdPASS);
xQueueSendToBack(interruptSourceQueueSecondary,
(void *) &intSourceSend,
((TickType_t)TICKS_TO_BLOCK_DEFAULT) != pdPASS);
// TODO: This part of the code still needs to be done!
//xTimerReset(intFlagTimer, TICKS_TO_BLOCK_DEFAULT);
}
// give other code a chance to execute since we have a higher
// priority
vTaskDelay(GET_INTERRUPT_DELAY_MS);
//todo: Consider enclosing epd writing in a critical section
// as is done in the joystick example
}
}
}
void menuStateMachineTaskFunction(void *arg)
{
while(1)
{
if(xQueueReceive(interruptSourceQueue,
&(intSourceRecv),
portMAX_DELAY))
{
/* 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;
adc.calibrate();
while(1)
{
light.updateData(&data);
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;
while(1)
{
pot.updateData(&data);
if(data.voltage>voltage+0.05f)
{
epd.drawImage(potCW);
epd.write();
}
else if(data.voltage<voltage-0.05f)
{
epd.drawImage(potCCW);
epd.write();
}
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();
adc.updateData(); //to clear the ADC buffer
while(1)
{
ambientTemperature.updateData(&data);
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
n = sprintf(buffer, "%c%d.%d", tmpSign, tmpInt1, tmpInt2); //Create a string
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);
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;
adc.calibrate();
vTaskDelay(25); // Small delay required by adc to calibrate
while(1)
{
myBLE.startAdvertising(APP_ADV_INTERVAL, APP_ADV_DURATION);
while(!gBleTaskSuspend)
{
if(advertisementPacket.advType == FIRST_PACKET_ID)
{
mems.powerOn();
bool accelEnabled = mems.enable(SENSOR_TYPE::ACCELEROMETER);
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;
}
else
{
light.updateData(&data);
advertisementPacket.light = data.light;
ambientTemperature.updateData(&data);
advertisementPacket.temp = data.temperature;
advertisementPacket.joy = checkState(&expdr, &sw1);
pot.updateData(&data);
advertisementPacket.pot = (uint8_t)(data.voltage*255);
}
myBLE.advRawUpdate((uint8_t*)&advertisementPacket,
sizeof(advertisementPacket_t));
advertisementPacket.advType = (advertisementPacket.advType+1)%2;
vTaskDelay(BEACON_DELAY_MS);
}
gBleTaskSuspend = false;
AconnoBLE &myBLE = *getAconnoBLEInstance();
myBLE.stopAdvertising();
NRF_LOG_INFO("Advertising stopped.");
vTaskSuspend(NULL);
}
}
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);
while(1)
{
if(xQueueReceive(interruptSourceQueueSecondary,
&(intSourceRecvSecondary),
(TickType_t) 10))
{
if (joyFlag)
{
joyFlag = 0;
epd.drawImage(joystick1);
epd.fillCircle(xCoordCenter,
yCoordCenter,
ballRadius,
color);
// do not allow task suspending while epd writing
vTaskSuspendAll();
epd.write();
xTaskResumeAll();
}
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;
}
}
vTaskSuspend(menuStateMachineTask);
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;
}
}
vTaskResume(menuStateMachineTask);
}
}
}
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
}
}
void servoFunction(void *arg)
{
float percentage;
vTaskSuspend(NULL);
servo.init();
servo.calibrate(0.00095, 90);
servo.write(0.5f);
while(1)
{
switch (intSourceSend)
{
case IOEXP_INT_SOURCE::JOY_LEFT:
{
if(servo.read()>0.1)
{
percentage = servo.read() - 0.1f;
servo.write(percentage);
waitMs(SERVO_DELAY_MS);
}
break;
}
case IOEXP_INT_SOURCE::JOY_RIGHT:
{
if(servo.read()<0.90)
{
percentage = servo.read() + 0.1f;
servo.write(percentage);
waitMs(SERVO_DELAY_MS);
}
break;
}
default:
{
servo.write(servo.read());
waitMs(SERVO_DELAY_MS);
break;
}
}
NRF_LOG_INFO("Percentage is" NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(percentage));
vTaskDelay(EXAMPLE_DELAY_MS);
}
}
void soundFunction(void *arg)
{
while(1)
{
vTaskSuspend(NULL);
char *song = "Greensleaves:d=4,o=5,b=140:g,2a#,c6,d.6,8d#6,d6,2c6,a,f.,8g,a,2a#,g,g.,8f#,g,2a,f#,2d,g,2a#,c6,d.6,8d#6,d6,2c6,a,f.,8g,a,a#.,8a,g,f#.,8e,f#,2g";
beeper.playRttl(song);
}
}