184 lines
4.7 KiB
C++
184 lines
4.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 "FreeRTOS.h"
|
|
#include "aconnoTasks.h"
|
|
#include "aconnoBLE.h"
|
|
#include "nrf_gpio.h"
|
|
#include "aconnoGPIO.h"
|
|
#include "semphr.h"
|
|
#include "aconnoHelpers.h"
|
|
#include "menuStates.h"
|
|
|
|
TaskHandle_t initTask;
|
|
TaskHandle_t blinkyTask;
|
|
TaskHandle_t blinkyTask2;
|
|
TaskHandle_t batteryServiceTaskMy;
|
|
TaskHandle_t menuStateMachineTask;
|
|
|
|
TaskHandle_t welcomeTask;
|
|
TaskHandle_t jumperTask;
|
|
TaskHandle_t joystickTask;
|
|
|
|
BTN_PRESSED btnPressed = BTN_PRESSED::NOTHING;
|
|
State* pMenuState;
|
|
|
|
|
|
SemaphoreHandle_t stateMachineSemaphore;
|
|
|
|
AconnoGPIO led1(LED1_PIN,GPIO_DIR::GPIO_OUTPUT);
|
|
AconnoGPIO led2(LED2_PIN,GPIO_DIR::GPIO_OUTPUT);
|
|
AconnoGPIO led3(LED3_PIN,GPIO_DIR::GPIO_OUTPUT);
|
|
|
|
AconnoGPIO btn1(BTN_1_PIN,
|
|
GPIO_DIR::GPIO_INTERRUPT,
|
|
GPIO_PULL::OFF,
|
|
GPIO_EDGE::FALLING,
|
|
mainMenuInterruptCallback);
|
|
|
|
void mainMenuInterruptCallback()
|
|
{
|
|
/* todo: possibly no debounce */
|
|
NRF_LOG_DEBUG("button pressed");
|
|
/* todo: get real button pressed when the joystick is ported */
|
|
btnPressed = BTN_PRESSED::DOWN ;
|
|
xSemaphoreGive( stateMachineSemaphore );
|
|
}
|
|
|
|
/**< 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 blinkyFunction(void *arg)
|
|
{
|
|
nrf_gpio_cfg_output(LED1_PIN);
|
|
while(1)
|
|
{
|
|
nrf_gpio_pin_toggle(LED1_PIN);
|
|
vTaskDelay(BLINKY_1_DELAY_MS);
|
|
NRF_LOG_INFO("Blinky 1 =) ");
|
|
}
|
|
}
|
|
|
|
void blinkyFunction2(void *arg)
|
|
{
|
|
nrf_gpio_cfg_output(LED1_PIN);
|
|
while(1)
|
|
{
|
|
nrf_gpio_pin_toggle(LED1_PIN);
|
|
vTaskDelay(BLINKY_2_DELAY_MS);
|
|
NRF_LOG_INFO("Blinky 2 =) ");
|
|
}
|
|
}
|
|
|
|
void welcomeTaskFunction(void *arg)
|
|
{
|
|
while(1)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
led1.toggle();
|
|
}
|
|
}
|
|
|
|
void jumperTaskFunction(void *arg)
|
|
{
|
|
while(1)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
led2.toggle();
|
|
}
|
|
}
|
|
|
|
void joystickTaskFunction(void *arg)
|
|
{
|
|
while(1)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
led3.toggle();
|
|
}
|
|
}
|
|
|
|
void menuStateMachineTaskFunction(void *arg)
|
|
{
|
|
NRF_LOG_INFO("StateMachineTaskStart");
|
|
pMenuState = new WelcomeState();
|
|
|
|
/* Create all tasks that will be used */
|
|
xTaskCreate(welcomeTaskFunction, "WelcomeTask", 256, NULL, 1, &welcomeTask);
|
|
xTaskCreate(jumperTaskFunction, "WelcomeTask", 256, NULL, 1, &jumperTask);
|
|
xTaskCreate(joystickTaskFunction, "WelcomeTask", 256, NULL, 1, &joystickTask);
|
|
|
|
/* Attempt to create a semaphore. */
|
|
stateMachineSemaphore = xSemaphoreCreateBinary();
|
|
if( stateMachineSemaphore == NULL )
|
|
{
|
|
NRF_LOG_ERROR("ERROR, not enough space available to create semaphore!");
|
|
}
|
|
/* End semaphore creation */
|
|
|
|
while(1)
|
|
{
|
|
if( xSemaphoreTake( stateMachineSemaphore, portMAX_DELAY ) == pdTRUE )
|
|
{
|
|
|
|
/* todo: delete could make problems if the same state is returned
|
|
*/
|
|
|
|
State* newMenuState = pMenuState->buttonPressed(btnPressed);
|
|
delete pMenuState;
|
|
pMenuState = newMenuState;
|
|
NRF_LOG_INFO("StateMachineLoopEnd\n");
|
|
|
|
}
|
|
|
|
/* 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 */
|
|
|
|
/* todo: main menu task waits at semaphore until a button is pressed,
|
|
then go into a switch case that will decide which state is next from
|
|
the button last pressed and the current state, alternatively,
|
|
instead of switching states it can do nothing (in case of a button
|
|
pressed that does nothing), or remain in
|
|
the same menu state to start the example task and remain blocked at the
|
|
semaphore until the task is finished (that task will redefine
|
|
the button callback vector to tailor the joystick inputs
|
|
towards its example, defining one button as an exit point that
|
|
will renew the original menu callback vector and return the user
|
|
to the menu) */
|
|
|
|
}
|
|
|
|
}
|