154 lines
3.7 KiB
C++
154 lines
3.7 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"
|
|
|
|
|
|
|
|
extern TaskHandle_t menuStateMachineTask;
|
|
TaskHandle_t getInterruptSourceTask;
|
|
|
|
extern AconnoSPI spi;
|
|
extern AconnoI2C i2c;
|
|
|
|
extern AconnoGPIO ledRed;
|
|
extern AconnoGPIO ledBlue;
|
|
extern AconnoGPIO ledGreen;
|
|
|
|
extern GDEP015OC1 epd;
|
|
extern MCP23017 expdr;
|
|
|
|
|
|
IOEXP_INT_SOURCE intSource = IOEXP_INT_SOURCE::NOTHING;
|
|
State* pMenuState;
|
|
SemaphoreHandle_t getInterruptSourceSemaphore;
|
|
|
|
|
|
/**< 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)
|
|
{
|
|
|
|
/* init i2c */
|
|
i2c.init();
|
|
|
|
/* init ioexpander */
|
|
initIoExpander(&expdr);
|
|
expdr.setupInterruptPin(MCP23017_CENTER, INT_MODE::FALLING);
|
|
expdr.setupInterruptPin(MCP23017__UP, INT_MODE::FALLING);
|
|
expdr.setupInterruptPin(MCP23017__DOWN, INT_MODE::FALLING);
|
|
expdr.setupInterruptPin(MCP23017_RIGHT, INT_MODE::FALLING);
|
|
expdr.setupInterruptPin(MCP23017_LEFT, INT_MODE::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);
|
|
delete pMenuState;
|
|
pMenuState = newMenuState;
|
|
pMenuState->displayOnEpd();
|
|
|
|
/* 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 */
|
|
}
|
|
|
|
}
|