/** * @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" #include "acn52832Bsp.h" /*epd*/ #include "aconnoSPI.h" #include "GDEP015OC1.h" #include "pictures.h" /*ioexp*/ #include "aconnoI2C.h" #include "MCP23017.h" TaskHandle_t initTask; TaskHandle_t blinkyTask; TaskHandle_t blinkyTask2; TaskHandle_t batteryServiceTaskMy; TaskHandle_t menuStateMachineTask; TaskHandle_t getInterruptSourceTask; IOEXP_INT_SOURCE intSource = IOEXP_INT_SOURCE::NOTHING; State* pMenuState; SemaphoreHandle_t getInterruptSourceSemaphore; AconnoGPIO ledRed(LED1_PIN,GPIO_DIR::GPIO_OUTPUT); AconnoGPIO ledBlue(LED2_PIN,GPIO_DIR::GPIO_OUTPUT); AconnoGPIO ledGreen(LED3_PIN,GPIO_DIR::GPIO_OUTPUT); /* AconnoGPIO btn1(BTN_1_PIN, GPIO_DIR::GPIO_INTERRUPT, GPIO_PULL::OFF, GPIO_EDGE::FALLING, testCallback); */ AconnoSPI spi(PIN_EPD_MISO, PIN_EPD_MOSI, PIN_EPD_SCK); GDEP015OC1 epd(spi, PIN_EPD_CS, PIN_EPD_DC, PIN_EPD_RST, PIN_EPD_BUSY); AconnoI2C i2c (PIN_EXP_SDA, PIN_EXP_SCL); MCP23017 expdr (i2c, TWI_EXPANDER_ADDR, EXPDR_A_INT, GPIO_DIR::GPIO_INTERRUPT, GPIO_PULL::OFF, GPIO_EDGE::FALLING, mainMenuInterruptCallback); /**< 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 */ } }