Files
ACD528xx_Test_Project/tasks/aconnoTasks.cpp
T

211 lines
5.6 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>
extern TaskHandle_t menuStateMachineTask;
TaskHandle_t getInterruptSourceTask;
extern TaskHandle_t LDRTask;
extern AconnoSPI spi;
extern AconnoI2C i2c;
extern AconnoSAADC adc;
extern AconnoGPIO ledRed;
extern AconnoGPIO ledBlue;
extern AconnoGPIO ledGreen;
extern GDEP015OC1 epd;
extern MCP23017 expdr;
extern LDR light;
IOEXP_INT_SOURCE intSource = IOEXP_INT_SOURCE::NOTHING;
State* pMenuState;
SemaphoreHandle_t getInterruptSourceSemaphore;
aconnoSensorData_t data;
/**< Definition of Logger thread. */
#if NRF_LOG_ENABLED
TaskHandle_t m_logger_thread;
void logger_thread(void * arg);
#endif
void logTaskFunction(void *arg)
{
#if NRF_LOG_ENABLED
if (pdPASS != xTaskCreate(logger_thread, "LOGGER", 256, NULL, 1,
&m_logger_thread))
{
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 mainMenuInterruptCallback()
{
// resume task to get button
xSemaphoreGive( getInterruptSourceSemaphore );
}
void getInterruptSourceFunction(void *arg)
{
while(1)
{
if( xSemaphoreTake( getInterruptSourceSemaphore, portMAX_DELAY ) == pdTRUE )
{
waitMs(50);
/* todo: actually return real interrupt source */
uint8_t intSrcPin = expdr.getLastInterruptPin();
if (expdr.readGPIOBit(intSrcPin))
{
//ignore rising edge
intSource = IOEXP_INT_SOURCE::NOTHING;
}
else
{
intSource = (IOEXP_INT_SOURCE)intSrcPin;
/* let mainMenu stateMachine task handle the button pressed*/
vTaskResume(menuStateMachineTask);
}
}
}
}
void menuStateMachineTaskFunction(void *arg)
{
epd.empty();
epd.writeFull();
/* init i2c */
i2c.init();
/* init ioexpander */
initIoExpander(&expdr);
expdr.setupInterruptPin(MCP23017_CENTER, MCP23017_INTMODE::FALLING);
expdr.setupInterruptPin(MCP23017__UP, MCP23017_INTMODE::FALLING);
expdr.setupInterruptPin(MCP23017__DOWN, MCP23017_INTMODE::FALLING);
expdr.setupInterruptPin(MCP23017_RIGHT, MCP23017_INTMODE::FALLING);
expdr.setupInterruptPin(MCP23017_LEFT, MCP23017_INTMODE::FALLING);
/* end ioexpander init */
/* Turn off leds */
ledRed.set();
ledBlue.set();
ledGreen.set();
/* Initialize state machine*/
pMenuState = new Intro1State();
pMenuState->displayOnEpd();
/* Create all tasks that will be used */
xTaskCreate(getInterruptSourceFunction, "GetInterruptSourceTask", 256, NULL,
1, &getInterruptSourceTask);
/* Create all semaphores that will be used */
getInterruptSourceSemaphore= xSemaphoreCreateBinary();
if( getInterruptSourceSemaphore == NULL )
{
NRF_LOG_ERROR("ERROR, not enough space available to create semaphore!");
}
/* Main loop*/
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)
{
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;
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);
}
}