/** * @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 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 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; IOEXP_INT_SOURCE intSource = IOEXP_INT_SOURCE::NOTHING; State* pMenuState; SemaphoreHandle_t getInterruptSourceSemaphore; aconnoSensorData_t data; uint8_t MSD[26] = {0x59, 0x00, 0x17, 0xCF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; extern uint8_t irFlag; /**< 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 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) { vTaskSuspend(NULL); 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.lightvoltage+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); static float voltage; adc.calibrate(); pot.updateData(&data); voltage = data.voltage; pot.updateData(&data); voltage = data.voltage; while(1) { pot.updateData(&data); if(data.voltage>voltage+0.05f) { NRF_LOG_INFO("My pot is CW and its voltage is " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.voltage)); epd.drawImage(potCW); epd.write(); }else if(data.voltagetemperature+0.5f || data.temperature50.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); static uint8_t advertismentType = 0; AconnoBLE &myBLE = *getAconnoBLEInstance(); advertisementPacket_t advertisementPacket; //BRDR_NOT_SUPPORTED FLAG and MANUFACTURER_SPECIFIC_DATA uint8_t tmpInitData[MAX_ADV_PACKET_LENGTH] = { 2, 0x01, 0x04, sizeof(MSD)+1, 0xFF, }; memcpy(&tmpInitData[5], &MSD, sizeof(MSD)); myBLE.advRawUpdate(tmpInitData, sizeof(MSD)+5); vTaskDelay(EXAMPLE_DELAY_MS); uint8_t tmpData[MAX_ADV_PACKET_LENGTH] = { 0x02, 0x01, 0x04, sizeof(advertisementPacket_t)+1, 0xFF, }; advertisementPacket.header = APPLICATION_ID; adc.calibrate(); vTaskDelay(100); while(1) { NRF_LOG_INFO("Beacon"); if(advertismentType < 1) { mems.enable(SENSOR_TYPE::ACCELEROMETER); mems.enable(SENSOR_TYPE::GYROSCOPE); mems.enable(SENSOR_TYPE::MAGNETIC_FIELD); NRF_LOG_INFO("Sensors enabled"); advertisementPacket.type = FIRST_PACKET_ID; if(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; } else { NRF_LOG_INFO("No update"); } 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; NRF_LOG_INFO("Advertisement one sent"); NRF_LOG_INFO(""); NRF_LOG_INFO("Packet type: 0x%d", (int)advertisementPacket.type); NRF_LOG_INFO("Accel: x=%d, y=%d, z=%d", advertisementPacket.accelerometer[0], advertisementPacket.accelerometer[1], advertisementPacket.accelerometer[2] ); NRF_LOG_INFO("Gyro: x=%d, y=%d, z=%d", advertisementPacket.gyroscope[0], advertisementPacket.gyroscope[1], advertisementPacket.gyroscope[2] ); NRF_LOG_INFO("Mag: x=%d, y=%d, z=%d", advertisementPacket.magnetometer[0], advertisementPacket.magnetometer[1], advertisementPacket.magnetometer[2] ); } else { /* code */ advertisementPacket.type = SECOND_PACKET_ID; light.updateData(&data); advertisementPacket.light = data.light; NRF_LOG_INFO("Light: " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.light)); ambientTemperature.updateData(&data); advertisementPacket.temp = data.temperature; NRF_LOG_INFO("Temp: " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.temperature)); advertisementPacket.joy = checkState(&expdr, &sw1); NRF_LOG_INFO("Joy: %d \r\n", checkState(&expdr, &sw1)); pot.updateData(&data); advertisementPacket.pot = (uint8_t)(data.voltage*255); NRF_LOG_INFO("Pot: " NRF_LOG_FLOAT_MARKER "\r\n", NRF_LOG_FLOAT(data.voltage*255)); } // change packet type advertismentType = advertismentType^1; // fill adv packet memcpy(&tmpData[5], &advertisementPacket, sizeof(advertisementPacket)); myBLE.advRawUpdate(tmpData, sizeof(advertisementPacket)+5); vTaskDelay(BEACON_DELAY_MS); } } 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 //Short 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) { ld1.set(); vTaskSuspend(NULL); NRF_LOG_INFO("I'm in leds task"); 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); } }