Merge branch 'master' into feat_add_MSA301

This commit is contained in:
Ben V. Brown
2021-01-04 21:03:45 +11:00
38 changed files with 329 additions and 258 deletions

View File

@@ -34,6 +34,7 @@ extern osThreadId MOVTaskHandle;
extern osThreadId PIDTaskHandle;
static bool shouldBeSleeping(bool inAutoStart = false);
static bool shouldShutdown();
void showWarnings();
#define MOVEMENT_INACTIVITY_TIME (60 * configTICK_RATE_HZ)
#define BUTTON_INACTIVITY_TIME (60 * configTICK_RATE_HZ)
static TickType_t lastHallEffectSleepStart = 0;
@@ -43,6 +44,14 @@ static uint16_t min(uint16_t a, uint16_t b) {
else
return a;
}
void warnUser(const char *warning, const int font, const int timeout) {
OLED::setFont(font);
OLED::clearScreen();
OLED::setCursor(0, 0);
OLED::print(warning);
OLED::refresh();
waitForButtonPressOrTimeout(timeout);
}
void printVoltage() {
uint32_t volt = getInputVoltageX10(systemSettings.voltageDiv, 0);
@@ -473,38 +482,26 @@ static void gui_solderingMode(uint8_t jumpToSleep) {
if (buttonsLocked && (systemSettings.lockingMode != 0)) { // If buttons locked
switch (buttons) {
case BUTTON_NONE:
// stay
boostModeOn = false;
break;
case BUTTON_BOTH_LONG:
// Unlock buttons
buttonsLocked = false;
OLED::setCursor(0, 0);
OLED::clearScreen();
OLED::setFont(0);
OLED::print(UnlockingKeysString);
OLED::refresh();
waitForButtonPressOrTimeout(1000);
warnUser(UnlockingKeysString, 0, TICKS_SECOND);
break;
case BUTTON_F_LONG:
// if boost mode is enabled turn it on
if (systemSettings.BoostTemp && (systemSettings.lockingMode == 1)) {
boostModeOn = true;
break;
}
;
break;
// fall through
case BUTTON_BOTH:
case BUTTON_B_LONG:
case BUTTON_F_SHORT:
case BUTTON_B_SHORT:
// Do nothing and display a lock warming
OLED::setCursor(0, 0);
OLED::clearScreen();
OLED::setFont(0);
OLED::print(WarningKeysLockedString);
OLED::refresh();
waitForButtonPressOrTimeout(500);
warnUser(WarningKeysLockedString, 0, TICKS_SECOND / 2);
break;
default:
break;
@@ -540,12 +537,7 @@ static void gui_solderingMode(uint8_t jumpToSleep) {
if (systemSettings.lockingMode != 0) {
// Lock buttons
buttonsLocked = true;
OLED::setCursor(0, 0);
OLED::clearScreen();
OLED::setFont(0);
OLED::print(LockingKeysString);
OLED::refresh();
waitForButtonPressOrTimeout(1000);
warnUser(LockingKeysString, 0, TICKS_SECOND);
}
break;
default:
@@ -706,7 +698,7 @@ void showDebugMenu(void) {
break;
case 10:
// Print PCB ID number
OLED::printNumber(PCBVersion, 2);
OLED::printNumber(DetectedAccelerometerVersion, 2);
break;
case 11:
// Power negotiation status
@@ -752,6 +744,40 @@ void showDebugMenu(void) {
GUIDelay();
}
}
void showWarnings() {
// Display alert if settings were reset
if (settingsWereReset) {
warnUser(SettingsResetMessage, 1, 10 * TICKS_SECOND);
}
#ifndef NO_WARN_MISSING
//We also want to alert if accel or pd is not detected / not responding
// In this case though, we dont want to nag the user _too_ much
// So only show first 2 times
while (DetectedAccelerometerVersion == ACCELEROMETERS_SCANNING) {
osDelay(1);
}
// Display alert if accelerometer is not detected
if (DetectedAccelerometerVersion == NO_DETECTED_ACCELEROMETER) {
if (systemSettings.accelMissingWarningCounter < 2) {
systemSettings.accelMissingWarningCounter++;
saveSettings();
warnUser(NoAccelerometerMessage, 1, 10 * TICKS_SECOND);
}
}
#ifdef POW_PD
//We expect pd to be present
if (!usb_pd_detect()) {
if (systemSettings.pdMissingWarningCounter < 2) {
systemSettings.pdMissingWarningCounter++;
saveSettings();
warnUser(NoPowerDeliveryMessage, 1, 10 * TICKS_SECOND);
}
}
#endif
#endif
}
uint8_t idleScreenBGF[sizeof(idleScreenBG)];
/* StartGUITask function */
void startGUITask(void const *argument __unused) {
@@ -783,15 +809,8 @@ void startGUITask(void const *argument __unused) {
GUIDelay();
}
if (settingsWereReset) {
// Display alert settings were reset
OLED::clearScreen();
OLED::setFont(1);
OLED::setCursor(0, 0);
OLED::print(SettingsResetMessage);
OLED::refresh();
waitForButtonPressOrTimeout(10000);
}
showWarnings();
if (systemSettings.autoStartMode) {
// jump directly to the autostart mode
gui_solderingMode(systemSettings.autoStartMode - 1);
@@ -925,7 +944,7 @@ void startGUITask(void const *argument __unused) {
//Draw in missing tip symbol
#ifdef OLED_FLIP
if (!OLED::getRotation()) {
if (!OLED::getRotation()) {
#else
if (OLED::getRotation()) {
#endif

230
workspace/TS100/Core/Threads/MOVThread.cpp Executable file → Normal file
View File

@@ -5,6 +5,7 @@
* Author: Ralim
*/
#include "BMA223.hpp"
#include "BSP.h"
#include "FreeRTOS.h"
#include "I2C_Wrapper.hpp"
@@ -19,153 +20,142 @@
#include "main.hpp"
#include "power.hpp"
#include "stdlib.h"
#include "BMA223.hpp"
#include "task.h"
#define MOVFilter 8
uint8_t accelInit = 0;
TickType_t lastMovementTime = 0;
void detectAccelerometerVersion() {
PCBVersion = 99;
PCBVersion = 99;
#ifdef ACCEL_MMA
if (MMA8652FC::detect()) {
if (MMA8652FC::detect()) {
if (MMA8652FC::initalize()) {
PCBVersion = 1;
}
} else
if (MMA8652FC::initalize()) {
PCBVersion = 1;
}
} else
#endif
#ifdef ACCEL_LIS
if (LIS2DH12::detect()) {
// Setup the ST Accelerometer
if (LIS2DH12::initalize()) {
PCBVersion = 2;
}
} else
if (LIS2DH12::detect()) {
// Setup the ST Accelerometer
if (LIS2DH12::initalize()) {
PCBVersion = 2;
}
} else
#endif
#ifdef ACCEL_BMA
if (BMA223::detect()) {
// Setup the ST Accelerometer
if (BMA223::initalize()) {
PCBVersion = 3;
}
} else
if (BMA223::detect()) {
// Setup the ST Accelerometer
if (BMA223::initalize()) {
PCBVersion = 3;
}
} else
#endif
#ifdef ACCEL_MSA
if (MSA301::detect()) {
// Setup the MSA301 Accelerometer
if (MSA301::initalize()) {
PCBVersion = 4;
}
} else
if (MSA301::detect()) {
// Setup the MSA301 Accelerometer
if (MSA301::initalize()) {
PCBVersion = 4;
}
} else
#endif
{
//disable imu sensitivity
systemSettings.sensitivity = 0;
}
{
// disable imu sensitivity
systemSettings.sensitivity = 0;
}
}
inline void readAccelerometer(int16_t &tx, int16_t &ty, int16_t &tz, Orientation &rotation) {
inline void readAccelerometer(int16_t &tx, int16_t &ty, int16_t &tz,
Orientation &rotation) {
#ifdef ACCEL_LIS
if (PCBVersion == 2) {
LIS2DH12::getAxisReadings(tx, ty, tz);
rotation = LIS2DH12::getOrientation();
} else
if (DetectedAccelerometerVersion == 2) {
LIS2DH12::getAxisReadings(tx, ty, tz);
rotation = LIS2DH12::getOrientation();
} else
#endif
#ifdef ACCEL_MMA
if (PCBVersion == 1) {
MMA8652FC::getAxisReadings(tx, ty, tz);
rotation = MMA8652FC::getOrientation();
} else
if (DetectedAccelerometerVersion == 1) {
MMA8652FC::getAxisReadings(tx, ty, tz);
rotation = MMA8652FC::getOrientation();
} else
#endif
#ifdef ACCEL_BMA
if (PCBVersion == 3) {
BMA223::getAxisReadings(tx, ty, tz);
rotation = BMA223::getOrientation();
} else
if (DetectedAccelerometerVersion == 3) {
BMA223::getAxisReadings(tx, ty, tz);
rotation = BMA223::getOrientation();
} else
#endif
#ifdef ACCEL_MSA
if (PCBVersion == 3) {
MSA301::getAxisReadings(tx, ty, tz);
rotation = MSA301::getOrientation();
} else
if (PCBVersion == 3) {
MSA301::getAxisReadings(tx, ty, tz);
rotation = MSA301::getOrientation();
} else
#endif
{
//do nothing :(
}
{
// do nothing :(
}
}
void startMOVTask(void const *argument __unused) {
osDelay(1); //Make oled init happen first
postRToSInit();
OLED::setRotation(systemSettings.OrientationMode & 1);
detectAccelerometerVersion();
lastMovementTime = 0;
if ((systemSettings.autoStartMode == 2 || systemSettings.autoStartMode == 3))
osDelay(2 * TICKS_SECOND);
postRToSInit();
detectAccelerometerVersion();
osDelay(50); // wait ~50ms for setup of accel to finalise
lastMovementTime = 0;
// Mask 2 seconds if we are in autostart so that if user is plugging in and
// then putting in stand it doesnt wake instantly
if (systemSettings.autoStartMode)
osDelay(2 * TICKS_SECOND);
lastMovementTime = 0;
int16_t datax[MOVFilter] = { 0 };
int16_t datay[MOVFilter] = { 0 };
int16_t dataz[MOVFilter] = { 0 };
uint8_t currentPointer = 0;
int16_t tx = 0, ty = 0, tz = 0;
int32_t avgx, avgy, avgz;
if (systemSettings.sensitivity > 9)
systemSettings.sensitivity = 9;
Orientation rotation = ORIENTATION_FLAT;
// OLED::setFont(1);
// for (;;) {
// OLED::clearScreen();
// OLED::setCursor(0, 0);
// readAccelerometer(tx, ty, tz, rotation);
// OLED::printNumber(tx, 5, 0);
// OLED::setCursor(0, 8);
// OLED::printNumber(xTaskGetTickCount() / 10, 5, 1);
// OLED::refresh();
// osDelay(50);
// }
for (;;) {
int32_t threshold = 1500 + (9 * 200);
threshold -= systemSettings.sensitivity * 200; // 200 is the step size
readAccelerometer(tx, ty, tz, rotation);
if (systemSettings.OrientationMode == 2) {
if (rotation != ORIENTATION_FLAT) {
OLED::setRotation(rotation == ORIENTATION_LEFT_HAND); // link the data through
}
}
datax[currentPointer] = (int32_t) tx;
datay[currentPointer] = (int32_t) ty;
dataz[currentPointer] = (int32_t) tz;
if (!accelInit) {
for (uint8_t i = currentPointer + 1; i < MOVFilter; i++) {
datax[i] = (int32_t) tx;
datay[i] = (int32_t) ty;
dataz[i] = (int32_t) tz;
}
accelInit = 1;
}
currentPointer = (currentPointer + 1) % MOVFilter;
avgx = avgy = avgz = 0;
// calculate averages
for (uint8_t i = 0; i < MOVFilter; i++) {
avgx += datax[i];
avgy += datay[i];
avgz += dataz[i];
}
avgx /= MOVFilter;
avgy /= MOVFilter;
avgz /= MOVFilter;
int16_t datax[MOVFilter] = {0};
int16_t datay[MOVFilter] = {0};
int16_t dataz[MOVFilter] = {0};
uint8_t currentPointer = 0;
int16_t tx = 0, ty = 0, tz = 0;
int32_t avgx, avgy, avgz;
if (systemSettings.sensitivity > 9)
systemSettings.sensitivity = 9;
Orientation rotation = ORIENTATION_FLAT;
for (;;) {
int32_t threshold = 1500 + (9 * 200);
threshold -= systemSettings.sensitivity * 200; // 200 is the step size
readAccelerometer(tx, ty, tz, rotation);
if (systemSettings.OrientationMode == 2) {
if (rotation != ORIENTATION_FLAT) {
OLED::setRotation(rotation ==
ORIENTATION_LEFT_HAND); // link the data through
}
}
datax[currentPointer] = (int32_t)tx;
datay[currentPointer] = (int32_t)ty;
dataz[currentPointer] = (int32_t)tz;
if (!accelInit) {
for (uint8_t i = currentPointer + 1; i < MOVFilter; i++) {
datax[i] = (int32_t)tx;
datay[i] = (int32_t)ty;
dataz[i] = (int32_t)tz;
}
accelInit = 1;
}
currentPointer = (currentPointer + 1) % MOVFilter;
avgx = avgy = avgz = 0;
// calculate averages
for (uint8_t i = 0; i < MOVFilter; i++) {
avgx += datax[i];
avgy += datay[i];
avgz += dataz[i];
}
avgx /= MOVFilter;
avgy /= MOVFilter;
avgz /= MOVFilter;
// Sum the deltas
int32_t error = (abs(avgx - tx) + abs(avgy - ty) + abs(avgz - tz));
// So now we have averages, we want to look if these are different by more
// than the threshold
// Sum the deltas
int32_t error = (abs(avgx - tx) + abs(avgy - ty) + abs(avgz - tz));
// So now we have averages, we want to look if these are different by more
// than the threshold
// If error has occurred then we update the tick timer
if (error > threshold) {
lastMovementTime = xTaskGetTickCount();
}
// If movement has occurred then we update the tick timer
if (error > threshold) {
lastMovementTime = xTaskGetTickCount();
}
osDelay(100); // Slow down update rate
power_check();
}
osDelay(100); // Slow down update rate
power_check();
}
}