872 lines
26 KiB
C++
872 lines
26 KiB
C++
/**
|
|
* @file aconnoTasks.cpp
|
|
* @author Jurica Resetar (Jurica@aconno.de)
|
|
* @brief
|
|
* @version 0.1
|
|
* @date 2019-01-24
|
|
*
|
|
* @copyright Copyright (c) 2019
|
|
*
|
|
*/
|
|
//------------------------------ INCLUDES -------------------------------------
|
|
#include "aconnoTasks.h"
|
|
#include <typeinfo>
|
|
#include "timers.h"
|
|
#include "aconnoBeeper.h"
|
|
#ifdef NRF52840_XXAA
|
|
#include "aconnoUSBMouse.h"
|
|
#endif
|
|
//-------------------------------- MACROS -------------------------------------
|
|
//----------------------------- DATA TYPES ------------------------------------
|
|
//--------------------- PRIVATE FUNCTION PROTOTYPES ---------------------------
|
|
//----------------------- STATIC DATA & CONSTANTS -----------------------------
|
|
//------------------------------ GLOBAL DATA ----------------------------------
|
|
extern TaskHandle_t menuStateMachineTask;
|
|
extern SemaphoreHandle_t getInterruptSourceSemaphore;
|
|
extern SemaphoreHandle_t epdWriteMutex;
|
|
extern SemaphoreHandle_t i2cWriteMutex;
|
|
extern State *pMenuState;
|
|
extern QueueHandle_t interruptSourceQueue;
|
|
extern QueueHandle_t interruptSourceQueueSecondary;
|
|
|
|
uint8_t intSrcPin;
|
|
INT_SOURCE intSourceSend = INT_SOURCE::NOTHING;
|
|
INT_SOURCE intSourceRecv = INT_SOURCE::NOTHING;
|
|
INT_SOURCE intSourceRecvSecondary = INT_SOURCE::NOTHING;
|
|
|
|
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;
|
|
extern Servo servo;
|
|
extern AconnoBeeper beeper;
|
|
|
|
extern bool gBleTaskSuspend;
|
|
extern uint8_t gJoyFlag;
|
|
extern uint8_t gIrFlag;
|
|
extern uint8_t gLdrFlag;
|
|
extern uint8_t gTempFlag;
|
|
/**< Definition of Logger thread. */
|
|
#if NRF_LOG_ENABLED
|
|
TaskHandle_t mLoggerThread;
|
|
void loggerThread(void *arg);
|
|
#endif
|
|
|
|
//--------------------- PUBLIC FUNCTIONS AND OBJECTS --------------------------
|
|
void logTaskFunction(void *arg)
|
|
{
|
|
#if NRF_LOG_ENABLED
|
|
if(pdPASS != xTaskCreate(loggerThread, "LOGGER", 128, NULL, 1,
|
|
&mLoggerThread))
|
|
{
|
|
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
|
|
}
|
|
NRF_LOG_INFO("Logger task started.");
|
|
#endif
|
|
while(1)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
}
|
|
}
|
|
|
|
void getInterruptSourceFunction(void *arg)
|
|
{
|
|
while(1)
|
|
{
|
|
|
|
if(xSemaphoreTake(getInterruptSourceSemaphore, portMAX_DELAY) == pdTRUE)
|
|
{
|
|
xSemaphoreTake(i2cWriteMutex, portMAX_DELAY);
|
|
expdr.interruptPinDisable();
|
|
uint8_t intSrcPin = expdr.getLastInterruptPin();
|
|
intSourceSend = (INT_SOURCE)intSrcPin;
|
|
|
|
xQueueSendToBack(interruptSourceQueue,
|
|
(void *) &intSourceSend,
|
|
((TickType_t)TICKS_TO_BLOCK_DEFAULT) != pdPASS);
|
|
|
|
xQueueSendToBack(interruptSourceQueueSecondary,
|
|
(void *) &intSourceSend,
|
|
((TickType_t)TICKS_TO_BLOCK_DEFAULT) != pdPASS);
|
|
|
|
// Wait until the button is not being pressed
|
|
while(expdr.readGPIOBit(intSrcPin) == 0)
|
|
{
|
|
//NRF_LOG_INFO("Waiting till the button is nto being pressed.");
|
|
}
|
|
|
|
NRF_LOG_INFO("Joystick interrupt.");
|
|
expdr.interruptPinEnable();
|
|
expdr.readRegister(MCP23017_INTCAP);
|
|
xSemaphoreGive(i2cWriteMutex);
|
|
}
|
|
}
|
|
}
|
|
|
|
void menuStateMachineTaskFunction(void *arg)
|
|
{
|
|
while(1)
|
|
{
|
|
if(xQueueReceive(interruptSourceQueue,
|
|
&(intSourceRecv),
|
|
portMAX_DELAY))
|
|
{
|
|
xSemaphoreTake(epdWriteMutex, portMAX_DELAY);
|
|
State *newMenuState = pMenuState->handleEvent(intSourceRecv);
|
|
if(typeid(*pMenuState) != typeid(*newMenuState))
|
|
{
|
|
delete pMenuState;
|
|
pMenuState = newMenuState;
|
|
pMenuState->displayOnEpd();
|
|
}
|
|
else
|
|
{
|
|
delete pMenuState;
|
|
pMenuState = newMenuState;
|
|
}
|
|
xSemaphoreGive(epdWriteMutex);
|
|
}
|
|
/* todo: consider pre-creating all state objects instead of
|
|
dinamically creating and destroying objects */
|
|
/* todo: consider dynamically deleting tasks after a certain treshold
|
|
in case memory becomes a problem */
|
|
}
|
|
}
|
|
|
|
void ldrFunction(void *arg)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
|
|
aconnoSensorData_t data;
|
|
float voltage = 0;
|
|
uint16_t x_coord;
|
|
uint16_t y_coord;
|
|
gLdrFlag = 0;
|
|
|
|
adc.calibrate();
|
|
vTaskDelay(25); // Small delay required by adc to calibrate
|
|
|
|
while(1)
|
|
{
|
|
light.updateData(&data);
|
|
if(data.light < voltage - 0.05f || data.light > voltage + 0.05f || gLdrFlag == 1 )
|
|
{
|
|
gLdrFlag = 0;
|
|
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; }
|
|
|
|
xSemaphoreTake(epdWriteMutex, portMAX_DELAY);
|
|
epd.drawImage(ldr1);
|
|
epd.drawLine(100,135,x_coord,y_coord,0);
|
|
epd.write();
|
|
xSemaphoreGive(epdWriteMutex);
|
|
}
|
|
vTaskDelay(EXAMPLE_DELAY_MS);
|
|
}
|
|
}
|
|
|
|
void potentiometerFunction(void *arg)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
|
|
aconnoSensorData_t data;
|
|
static float voltage;
|
|
|
|
adc.calibrate();
|
|
vTaskDelay(25); // Small delay required by adc to calibrate
|
|
pot.updateData(&data);
|
|
voltage = data.voltage;
|
|
|
|
while(1)
|
|
{
|
|
xSemaphoreTake(epdWriteMutex, portMAX_DELAY);
|
|
pot.updateData(&data);
|
|
if(data.voltage > voltage + 0.02f)
|
|
{
|
|
epd.drawImage(potCW);
|
|
epd.write();
|
|
}
|
|
else if(data.voltage < voltage - 0.02f)
|
|
{
|
|
epd.drawImage(potCCW);
|
|
epd.write();
|
|
}
|
|
voltage = data.voltage;
|
|
xSemaphoreGive(epdWriteMutex);
|
|
vTaskDelay(POT_DELAY_MS);
|
|
}
|
|
}
|
|
|
|
void temperatureFunction(void *arg)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
|
|
aconnoSensorData_t data;
|
|
float temperature = 0;
|
|
char buffer[10];
|
|
gTempFlag = 0;
|
|
adc.calibrate();
|
|
vTaskDelay(25); // Small delay required by adc to calibrate
|
|
|
|
while(1)
|
|
{
|
|
ambientTemperature.updateData(&data);
|
|
if(data.temperature > temperature + 0.5f ||
|
|
data.temperature < temperature - 0.5f || gTempFlag == 1)
|
|
{
|
|
xSemaphoreTake(epdWriteMutex, portMAX_DELAY);
|
|
gTempFlag = 0;
|
|
// Erase previous data from the buffer
|
|
epd.writeString(buffer,110,100,1);
|
|
|
|
temperature = data.temperature;
|
|
|
|
char tmpSign = (temperature < 0) ? '-' : '+';
|
|
float tmpVal = (temperature < 0) ? -temperature : temperature;
|
|
|
|
int tmpInt1 = tmpVal; // Get the integer
|
|
float tmpFrac = tmpVal - tmpInt1; // Get fraction
|
|
int tmpInt2 = trunc(tmpFrac * 100); // Turn fraction into integer
|
|
sprintf(buffer, "%c%d.%d", tmpSign, tmpInt1, tmpInt2); //Create a string
|
|
|
|
epd.writeString(buffer, 110, 100, 0); // Write new data to the buffer
|
|
epd.write();
|
|
xSemaphoreGive(epdWriteMutex);
|
|
}
|
|
vTaskDelay(EXAMPLE_DELAY_MS);
|
|
}
|
|
}
|
|
|
|
void gyroFunction(void *arg)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
|
|
aconnoSensorData_t data;
|
|
aconnoSensorVec_t gyroVal;
|
|
i2c.enable();
|
|
mems.enable(SENSOR_TYPE::GYROSCOPE);
|
|
|
|
while(1)
|
|
{
|
|
mems.updateData(&data, SENSOR_TYPE::GYROSCOPE);
|
|
|
|
gyroVal.x = data.gyro.x * (245.0f/32768.0f);
|
|
gyroVal.y = data.gyro.y * (245.0f/32768.0f);
|
|
gyroVal.z = data.gyro.z * (245.0f/32768.0f);
|
|
|
|
if((gyroVal.x > 50.0f) || (gyroVal.x < (-50.0f)) ||
|
|
(gyroVal.y > 50.0f) || (gyroVal.y < (-50.0f)) ||
|
|
(gyroVal.z > 50.0f) || (gyroVal.z < (-50.0f)))
|
|
{
|
|
xSemaphoreTake(epdWriteMutex, portMAX_DELAY);
|
|
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
|
|
}
|
|
xSemaphoreGive(epdWriteMutex);
|
|
}
|
|
vTaskDelay(EXAMPLE_DELAY_MS);
|
|
}
|
|
}
|
|
|
|
void bleBeaconFunction(void *arg)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
|
|
AconnoBLE &myBLE = *getAconnoBLEInstance();
|
|
static advertisementPacket_t advertisementPacket;
|
|
myBLE.advRawUpdate((uint8_t*)&advertisementPacket,
|
|
sizeof(advertisementPacket_t));
|
|
static aconnoSensorData_t data;
|
|
advertisementPacket.settingsFlag = SETTINGS_SUPPORT_FLAG;
|
|
advertisementPacket.versionNumber = PRODUCT_VERSION;
|
|
|
|
adc.calibrate();
|
|
vTaskDelay(25); // Small delay required by adc to calibrate
|
|
while(1)
|
|
{
|
|
myBLE.startAdvertising(APP_ADV_INTERVAL, APP_ADV_DURATION);
|
|
NRF_LOG_INFO("Advertising started.");
|
|
while(!gBleTaskSuspend)
|
|
{
|
|
if(advertisementPacket.advType == FIRST_PACKET_ID)
|
|
{
|
|
mems.powerOn();
|
|
bool accelEnabled = mems.enable(SENSOR_TYPE::ACCELEROMETER);
|
|
mems.enable(SENSOR_TYPE::GYROSCOPE);
|
|
mems.enable(SENSOR_TYPE::MAGNETIC_FIELD);
|
|
|
|
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;
|
|
|
|
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;
|
|
}
|
|
else
|
|
{
|
|
light.updateData(&data);
|
|
advertisementPacket.light = data.light;
|
|
|
|
ambientTemperature.updateData(&data);
|
|
advertisementPacket.temp = data.temperature;
|
|
|
|
advertisementPacket.joy = checkState(&expdr, &sw1);
|
|
|
|
pot.updateData(&data);
|
|
advertisementPacket.pot = (uint8_t)(data.voltage*255);
|
|
}
|
|
myBLE.advRawUpdate((uint8_t*)&advertisementPacket,
|
|
sizeof(advertisementPacket_t));
|
|
advertisementPacket.advType = (advertisementPacket.advType+1)%2;
|
|
vTaskDelay(BEACON_DELAY_MS);
|
|
}
|
|
gBleTaskSuspend = false;
|
|
AconnoBLE &myBLE = *getAconnoBLEInstance();
|
|
myBLE.stopAdvertising();
|
|
NRF_LOG_INFO("Advertising stopped.");
|
|
vTaskSuspend(NULL);
|
|
}
|
|
}
|
|
|
|
void imageUpdate(uint8_t const *data, size_t dataLen)
|
|
{
|
|
static uint16_t epdPointer = 0;
|
|
|
|
xSemaphoreTake(epdWriteMutex, portMAX_DELAY);
|
|
for(uint16_t x = 0; x < dataLen; x++)
|
|
{
|
|
epd.fill(data[x], epdPointer);
|
|
epdPointer++;
|
|
}
|
|
if(epdPointer == EPD_BUFFER_SIZE)
|
|
{
|
|
epdPointer = 0;
|
|
epd.writeFull();
|
|
NRF_LOG_INFO("Image updated.");
|
|
}
|
|
xSemaphoreGive(epdWriteMutex);
|
|
}
|
|
|
|
void infraredFunction(void *arg)
|
|
{
|
|
gIrFlag = 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 // Pause
|
|
};
|
|
uint8_t sosSize = sizeof(SOS);
|
|
uint8_t irIndex = 0;
|
|
|
|
while(1)
|
|
{
|
|
if(gIrFlag == 1)
|
|
{
|
|
irIndex = 0;
|
|
gIrFlag = 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)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
|
|
while(1)
|
|
{
|
|
xSemaphoreTake(i2cWriteMutex, portMAX_DELAY);
|
|
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();
|
|
}
|
|
xSemaphoreGive(i2cWriteMutex);
|
|
vTaskDelay(EXAMPLE_DELAY_MS);
|
|
}
|
|
}
|
|
|
|
void relayFunction(void *arg)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
|
|
NRF_LOG_INFO("I'm in relay function.");
|
|
|
|
while(1)
|
|
{
|
|
xSemaphoreTake(i2cWriteMutex, portMAX_DELAY);
|
|
|
|
if(expdr.readGPIOBit(MCP23017_SW2) == 0)
|
|
{
|
|
if(expdr.readGPIOBit(MCP23017_REL2) == 0)
|
|
{
|
|
expdr.writeGPIOBit(1, MCP23017_REL2);
|
|
}
|
|
else
|
|
{
|
|
expdr.writeGPIOBit(0, MCP23017_REL2);
|
|
}
|
|
}
|
|
else if(sw1.read() == GPIO_STATE::LOW)
|
|
{
|
|
if(expdr.readGPIOBit(MCP23017_REL1) == 0)
|
|
{
|
|
expdr.writeGPIOBit(1, MCP23017_REL1);
|
|
}
|
|
else
|
|
{
|
|
expdr.writeGPIOBit(0, MCP23017_REL1);
|
|
}
|
|
}
|
|
|
|
xSemaphoreGive(i2cWriteMutex);
|
|
vTaskDelay(EXAMPLE_DELAY_MS);
|
|
}
|
|
|
|
}
|
|
|
|
void joyFunction(void *arg)
|
|
{
|
|
//center coordinates
|
|
uint16_t xCoordCenter = 100;
|
|
uint16_t yCoordCenter = 103;
|
|
uint16_t centerOffset = 20;
|
|
uint16_t ballRadius = 7;
|
|
bool color = 0;
|
|
|
|
enum class BALL_POSITION
|
|
{
|
|
UP,
|
|
DOWN,
|
|
LEFT,
|
|
RIGHT,
|
|
CENTER
|
|
};
|
|
|
|
BALL_POSITION ballPosition = BALL_POSITION::CENTER;
|
|
BALL_POSITION ballPositionPrev = BALL_POSITION::CENTER;
|
|
|
|
vTaskSuspend(NULL);
|
|
|
|
while(1)
|
|
{
|
|
if(xQueueReceive(interruptSourceQueueSecondary,
|
|
&(intSourceRecvSecondary),
|
|
(TickType_t) 10))
|
|
{
|
|
if (gJoyFlag)
|
|
{
|
|
xSemaphoreTake(epdWriteMutex, portMAX_DELAY);
|
|
gJoyFlag = 0;
|
|
epd.drawImage(joystick1);
|
|
epd.fillCircle(xCoordCenter,
|
|
yCoordCenter,
|
|
ballRadius,
|
|
color);
|
|
epd.write();
|
|
xSemaphoreGive(epdWriteMutex);
|
|
}
|
|
ballPositionPrev = ballPosition;
|
|
switch(intSourceRecvSecondary)
|
|
{
|
|
case INT_SOURCE::JOY_UP:
|
|
{
|
|
switch(ballPosition)
|
|
{
|
|
case BALL_POSITION::CENTER:
|
|
{
|
|
ballPosition = BALL_POSITION::UP;
|
|
break;
|
|
}
|
|
case BALL_POSITION::DOWN:
|
|
{
|
|
ballPosition = BALL_POSITION::CENTER;
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case INT_SOURCE::JOY_DOWN:
|
|
{
|
|
switch(ballPosition)
|
|
{
|
|
case BALL_POSITION::CENTER:
|
|
{
|
|
ballPosition = BALL_POSITION::DOWN;
|
|
break;
|
|
}
|
|
case BALL_POSITION::UP:
|
|
{
|
|
ballPosition = BALL_POSITION::CENTER;
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case INT_SOURCE::JOY_LEFT:
|
|
{
|
|
switch(ballPosition)
|
|
{
|
|
case BALL_POSITION::CENTER:
|
|
{
|
|
ballPosition = BALL_POSITION::LEFT;
|
|
break;
|
|
}
|
|
case BALL_POSITION::RIGHT:
|
|
{
|
|
ballPosition = BALL_POSITION::CENTER;
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case INT_SOURCE::JOY_RIGHT:
|
|
{
|
|
switch(ballPosition)
|
|
{
|
|
case BALL_POSITION::CENTER:
|
|
{
|
|
ballPosition = BALL_POSITION::RIGHT;
|
|
break;
|
|
}
|
|
case BALL_POSITION::LEFT:
|
|
{
|
|
ballPosition = BALL_POSITION::CENTER;
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
case INT_SOURCE::JOY_CENTER:
|
|
{
|
|
ballPosition = BALL_POSITION::CENTER;
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
if(ballPositionPrev != ballPosition)
|
|
{
|
|
xSemaphoreTake(epdWriteMutex, portMAX_DELAY);
|
|
epd.drawImage(joystick1);
|
|
switch(ballPosition)
|
|
{
|
|
case BALL_POSITION::UP:
|
|
{
|
|
epd.fillCircle(xCoordCenter,
|
|
yCoordCenter-centerOffset,
|
|
ballRadius,
|
|
color);
|
|
epd.write();
|
|
break;
|
|
}
|
|
case BALL_POSITION::DOWN:
|
|
{
|
|
epd.fillCircle(xCoordCenter,
|
|
yCoordCenter+centerOffset,
|
|
ballRadius,
|
|
color);
|
|
epd.write();
|
|
break;
|
|
}
|
|
case BALL_POSITION::LEFT:
|
|
{
|
|
epd.fillCircle(xCoordCenter-centerOffset,
|
|
yCoordCenter,
|
|
ballRadius,
|
|
color);
|
|
epd.write();
|
|
break;
|
|
}
|
|
case BALL_POSITION::RIGHT:
|
|
{
|
|
epd.fillCircle(xCoordCenter+centerOffset,
|
|
yCoordCenter,
|
|
ballRadius,
|
|
color);
|
|
epd.write();
|
|
break;
|
|
}
|
|
case BALL_POSITION::CENTER:
|
|
{
|
|
epd.fillCircle(xCoordCenter,
|
|
yCoordCenter,
|
|
ballRadius,
|
|
color);
|
|
epd.write();
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
xSemaphoreGive(epdWriteMutex);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void rgbFunction(void *arg)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
while(1)
|
|
{
|
|
// Randomly generate a state for the rgb leds to be in
|
|
uint8_t x = rand() % 7;
|
|
ledRed.write((GPIO_STATE)((x >> 2) & 1));
|
|
ledGreen.write((GPIO_STATE)((x >> 1) & 1));
|
|
ledBlue.write((GPIO_STATE)((x >> 0) & 1));
|
|
vTaskDelay(rand() % 250);
|
|
}
|
|
}
|
|
|
|
void servoFunction(void *arg)
|
|
{
|
|
float percentage;
|
|
vTaskSuspend(NULL);
|
|
|
|
servo.init();
|
|
servo.calibrate(0.00095, 90);
|
|
servo.enable();
|
|
servo.write(0.5f);
|
|
waitMs(SERVO_DELAY_MS);
|
|
servo.disable();
|
|
|
|
while(1)
|
|
{
|
|
switch(intSourceSend)
|
|
{
|
|
case INT_SOURCE::JOY_LEFT:
|
|
{
|
|
if(servo.read() > 0.1)
|
|
{
|
|
intSourceSend = INT_SOURCE::NOTHING;
|
|
servo.enable();
|
|
percentage = servo.read() - 0.1f;
|
|
servo.write(percentage);
|
|
waitMs(SERVO_DELAY_MS);
|
|
servo.disable();
|
|
}
|
|
break;
|
|
}
|
|
case INT_SOURCE::JOY_RIGHT:
|
|
{
|
|
if(servo.read() < 0.90)
|
|
{
|
|
intSourceSend = INT_SOURCE::NOTHING;
|
|
servo.enable();
|
|
percentage = servo.read() + 0.1f;
|
|
servo.write(percentage);
|
|
waitMs(SERVO_DELAY_MS);
|
|
servo.disable();
|
|
}
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
vTaskDelay(EXAMPLE_DELAY_MS);
|
|
}
|
|
}
|
|
|
|
void soundFunction(void *arg)
|
|
{
|
|
while(1)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
char *song = "Greensleaves:d=4,o=5,b=140:g,2a#,c6,d.6,8d#6,d6,2c6,a,f.,8g,a,2a#,g,g.,8f#,g,2a,f#,2d,g,2a#,c6,d.6,8d#6,d6,2c6,a,f.,8g,a,a#.,8a,g,f#.,8e,f#,2g";
|
|
beeper.playRttl(song);
|
|
}
|
|
}
|
|
#ifdef NRF52840_XXAA
|
|
void usbFunction(void *arg)
|
|
{
|
|
vTaskSuspend(NULL);
|
|
ret_code_t ret;
|
|
|
|
usbSetup();
|
|
|
|
while(1)
|
|
{
|
|
|
|
while(1)
|
|
{
|
|
while (app_usbd_event_queue_process())
|
|
{
|
|
/* Nothing to do */
|
|
}
|
|
|
|
if(xQueueReceive(interruptSourceQueueSecondary,
|
|
&(intSourceRecvSecondary),
|
|
(TickType_t) 10))
|
|
{
|
|
switch(intSourceRecvSecondary)
|
|
{
|
|
case INT_SOURCE::JOY_UP:
|
|
{
|
|
NRF_LOG_INFO("INT_SOURCE::JOY_UP");
|
|
hidGenericMouseProcessState(USB_DIR::UP);
|
|
|
|
break;
|
|
}
|
|
case INT_SOURCE::JOY_DOWN:
|
|
{
|
|
NRF_LOG_INFO("INT_SOURCE::JOY_DOWN");
|
|
hidGenericMouseProcessState(USB_DIR::DOWN);
|
|
|
|
break;
|
|
}
|
|
case INT_SOURCE::JOY_LEFT:
|
|
{
|
|
NRF_LOG_INFO("INT_SOURCE::JOY_LEFT");
|
|
hidGenericMouseProcessState(USB_DIR::LEFT);
|
|
break;
|
|
}
|
|
case INT_SOURCE::JOY_RIGHT:
|
|
{
|
|
NRF_LOG_INFO("INT_SOURCE::JOY_RIGHT");
|
|
hidGenericMouseProcessState(USB_DIR::RIGHT);
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else if(sw1.read() == GPIO_STATE::LOW)
|
|
{
|
|
hidGenericMouseStopState();
|
|
}
|
|
|
|
UNUSED_RETURN_VALUE(NRF_LOG_PROCESS());
|
|
}
|
|
|
|
}
|
|
};
|
|
#endif
|
|
//--------------------------- INTERRUPT HANDLERS ------------------------------
|
|
void interruptCallback()
|
|
{
|
|
xSemaphoreGiveFromISR(getInterruptSourceSemaphore, NULL);
|
|
}
|