Skip to content

Commit

Permalink
Added freertos tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
orlopau committed Dec 2, 2023
1 parent b3c790a commit 9a1a02e
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 105 deletions.
3 changes: 2 additions & 1 deletion code/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@
"geometry": "cpp",
"forward_list": "cpp",
"regex": "cpp"
}
},
"cmake.configureOnOpen": false
}
2 changes: 1 addition & 1 deletion code/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ lib_deps =
https://github.com/ricmoo/QRCode.git
build_flags =
; -DDEV_DISPLAY
-DCORE_DEBUG_LEVEL=3
-DCORE_DEBUG_LEVEL=5
extra_scripts =
pre:scripts/firmware_version.py
pre:scripts/lang_from_env.py
Expand Down
163 changes: 83 additions & 80 deletions code/src/embedded/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
#include "hx711_loadcell.h"
#include "mode.h"
#include "mode_manager.h"
#include "modes/mode_scale.h"
#include "modes/mode_espresso.h"
#include "modes/mode_calibrate.h"
#include "modes/mode_espresso.h"
#include "modes/mode_recipe.h"
#include "modes/mode_scale.h"
#include "u8g_display.h"
#include "update.h"
#include "user_input.h"
Expand All @@ -30,11 +30,11 @@ Stopwatch stopwatch;

void saveScale(float scale)
{
ESP_LOGI(TAG, "New scale: %f", scale);
ESP_LOGI(TAG, "Saving scale to EEPROM...");
EEPROM.put(EEPROM_ADDR_SCALE, scale);
EEPROM.commit();
weightSensor.setScale(scale);
ESP_LOGI(TAG, "New scale: %f", scale);
ESP_LOGI(TAG, "Saving scale to EEPROM...");
EEPROM.put(EEPROM_ADDR_SCALE, scale);
EEPROM.commit();
weightSensor.setScale(scale);
}

ModeScale modeDefault(weightSensor, input, display, stopwatch);
Expand All @@ -46,88 +46,91 @@ ModeManager modeManager(modes, 4, display, input, battery);

EncoderDirection encoderDirection;

void IRAM_ATTR isr_input()
void IRAM_ATTR isr_input() { input.update(); }

void IRAM_ATTR isr_loadcell()
{
input.update();
weightSensor.update();
}

void setup()
void input_loop(void *params)
{
Serial.begin(115200);
ESP_LOGI(TAG, "CoffeeScale starting up...");

EEPROM.begin(2048);
btStop();

//////// DISPLAY ////////
display.begin();
display.drawOpener();

//////// WEIGHT SENSOR ////////
weightSensor.begin();

float scale;
EEPROM.get(EEPROM_ADDR_SCALE, scale);
if (isnan(scale))
{
scale = 1.0f;
}

ESP_LOGI(TAG, "Existing scale: %f", scale);
weightSensor.setScale(scale);

float delta = 1 / scale;
weightSensor.setAutoAveraging(abs(delta), 64);
ESP_LOGI(TAG, "Auto averaging delta: %f", delta);

// tare after 32 samples
ESP_LOGI(TAG, "Taring...");
for (int i = 0; i < 32;)
{
weightSensor.update();
if (weightSensor.isNewWeight())
{
i++;
}
}
weightSensor.tare();
ESP_LOGI(TAG, "Tared!");

//////// INTERRUPTS ////////
attachInterrupt(PIN_ENC_A, isr_input, CHANGE);
attachInterrupt(PIN_ENC_B, isr_input, CHANGE);
attachInterrupt(PIN_ENC_BTN, isr_input, CHANGE);

//////// UPDATES ////////
if (digitalRead(PIN_UPDATE_FIRMWARE) == LOW)
{
Updater::update_firmware(display, input);
}

ESP_LOGI(TAG, "Setup finished!");
ESP_LOGI(TAG, "Started input loop.");
for (;;)
{
weightSensor.update();
input.update();
modeManager.update();
display.update();
}
}

#ifdef PERF
unsigned int loops = 0;
unsigned long lastTime = millis();
#endif
void setup()
{
Serial.begin(115200);
ESP_LOGI(TAG, "CoffeeScale starting up...");

EEPROM.begin(2048);
btStop();

//////// DISPLAY ////////
display.begin();
display.drawOpener();
display.update();

//////// WEIGHT SENSOR ////////
weightSensor.begin();

float scale;
EEPROM.get(EEPROM_ADDR_SCALE, scale);
if (isnan(scale))
{
scale = 1.0f;
}

ESP_LOGI(TAG, "Existing scale: %f", scale);
weightSensor.setScale(scale);

float delta = 1 / scale;
weightSensor.setAutoAveraging(abs(delta), 64);
ESP_LOGI(TAG, "Auto averaging delta: %f", delta);

ESP_LOGI(TAG, "Taring...");
for (int i = 0; i < 16;)
{
weightSensor.update();
if (weightSensor.isNewWeight())
{
i++;
}
}
weightSensor.tare();
ESP_LOGI(TAG, "Tared!");

//////// INTERRUPTS ////////
attachInterrupt(PIN_ENC_A, isr_input, CHANGE);
attachInterrupt(PIN_ENC_B, isr_input, CHANGE);
attachInterrupt(PIN_ENC_BTN, isr_input, CHANGE);

//////// UPDATES ////////
if (digitalRead(PIN_UPDATE_FIRMWARE) == LOW)
{
Updater::update_firmware(display, input);
}

//////// SCHEDULE TAKS ////////
int code = xTaskCreatePinnedToCore(input_loop, "INPUT", 64 * 1024, NULL, 10, NULL, ARDUINO_RUNNING_CORE);
if (code != pdPASS)
{
ESP_LOGE(TAG, "Task couldnt be created: %d", code);
}

ESP_LOGI(TAG, "Setup finished!");
}

void loop()
{
input.update();
weightSensor.update();
display.update();
modeManager.update();

#ifdef PERF
if (loops >= AVERAGING_LOOPS)
{
ESP_LOGI(TAG, "Loop time: %lu", (millis() - lastTime) / loops
loops = 0;
lastTime = millis();
}
loops++;
#endif
// NOP
}

#endif
43 changes: 21 additions & 22 deletions code/src/embedded/u8g_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#define FONT_MEDIUM u8g2_font_profont17_tf
#define FONT_LARGE u8g2_font_logisoso20_tf

static const char *TAG = "U8GDISP";

U8GDisplay::U8GDisplay(const uint8_t pin_sda, const uint8_t pin_scl, const u8g2_cb_t *rotation)
: u8g(rotation, U8X8_PIN_NONE, pin_scl, pin_sda)
{
Expand All @@ -27,6 +29,25 @@ void U8GDisplay::begin()
u8g.begin();
}

void U8GDisplay::update()
{
u8g.sendBuffer();
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
frameCounter++;
// every 120 frames log the fps
if (frameCounter >= 120)
{
unsigned long time = millis();

uint8_t fps = (1000.0 / (time - lastFpsLogTime)) * frameCounter;
ESP_LOGD(TAG, "FPS: %d", fps);

lastFpsLogTime = time;
frameCounter = 0;
}
#endif
}

void U8GDisplay::clear()
{
u8g.clearBuffer();
Expand All @@ -41,7 +62,6 @@ void U8GDisplay::display(float weight, unsigned long time)
u8g.drawStr(0, 30, weightText);
u8g.setFont(u8g2_font_logisoso22_tf);
u8g.drawStr(0, 64, timeText);
u8g.sendBuffer();
}

void U8GDisplay::promptText(const char *prompt, const char *text)
Expand All @@ -50,7 +70,6 @@ void U8GDisplay::promptText(const char *prompt, const char *text)
u8g.setFont(u8g_font_6x10);
u8g.drawStr(0, 10, prompt);
u8g.drawStr(0, 20, text);
u8g.sendBuffer();
}

void U8GDisplay::drawHCenterText(const char *text, uint8_t y)
Expand Down Expand Up @@ -89,7 +108,6 @@ void U8GDisplay::centerText(const char *text, const uint8_t size)
}

drawHCenterText(text, mid);
u8g.sendBuffer();
}

int U8GDisplay::drawTitleLine(const char *title)
Expand Down Expand Up @@ -156,8 +174,6 @@ void U8GDisplay::switcher(const char* title, const uint8_t index, const uint8_t
u8g.setDrawColor(1);
yy += optionHeight;
}

u8g.sendBuffer();
};

#define QR_CODE_VERSION 2
Expand Down Expand Up @@ -213,8 +229,6 @@ void U8GDisplay::recipeSummary(const char *name, const char *description, const
// qr code size 54
drawQRCode(url, width - 54, (height - 54) / 2.0);
}

u8g.sendBuffer();
}

bool U8GDisplay::shouldBlinkedBeVisible()
Expand Down Expand Up @@ -253,7 +267,6 @@ void U8GDisplay::recipeConfigCoffeeWeight(const char *header, unsigned int weigh
u8g.drawStr(u8g.getWidth() / 2.0, yy + (remainingHeight / 4.0) * 3, buffer);

u8g.setFontPosBaseline();
u8g.sendBuffer();
}

void U8GDisplay::recipeConfigRatio(const char *header, uint32_t coffee, uint32_t water)
Expand Down Expand Up @@ -284,8 +297,6 @@ void U8GDisplay::recipeConfigRatio(const char *header, uint32_t coffee, uint32_t
sprintf(buffer, "%.1f", water / 10.0);
u8g.drawStr(3 * u8g.getDisplayWidth() / 4.0 - u8g.getStrWidth(buffer) / 2.0, yy, buffer);
}

u8g.sendBuffer();
}

int U8GDisplay::drawSelectedBar(uint8_t index, uint8_t size)
Expand Down Expand Up @@ -317,8 +328,6 @@ void U8GDisplay::recipeInsertCoffee(int32_t weightMg, uint32_t requiredWeightMg)
static char buffer[16];
sprintf(buffer, "%.2fg/%.1fg", weightMg / 1000.0, requiredWeightMg / 1000.0);
drawCenterText(buffer);

u8g.sendBuffer();
}

void U8GDisplay::recipePour(const char *text, int32_t weightToPourMg, uint64_t timeToFinishMs, bool isPause, uint8_t pourIndex, uint8_t pours)
Expand Down Expand Up @@ -364,8 +373,6 @@ void U8GDisplay::recipePour(const char *text, int32_t weightToPourMg, uint64_t t
}
int textWidth = u8g.getStrWidth(buffer);
u8g.drawStr(width - textWidth - TEXT_X_PADDING, center + ascent / 2.0, buffer);

u8g.sendBuffer();
}

void U8GDisplay::text(const char *text)
Expand All @@ -385,8 +392,6 @@ void U8GDisplay::text(const char *text)
pointer = strtok(NULL, "\n");
}
delete textCopy;

u8g.sendBuffer();
}

void U8GDisplay::drawTextAutoWrap(const char *text, int yTop, int xLeft, int maxWidth)
Expand Down Expand Up @@ -448,8 +453,6 @@ void U8GDisplay::modeSwitcher(const char *current, const uint8_t index, const ui
int textWidth = u8g.getUTF8Width(buffer);
u8g.drawUTF8(u8g.getDisplayWidth() - textWidth - PADDING, u8g.getDisplayHeight() - PADDING, buffer);
}

u8g.sendBuffer();
}

void U8GDisplay::drawOpener()
Expand Down Expand Up @@ -488,8 +491,6 @@ void U8GDisplay::drawOpener()
ascent = u8g.getAscent();
textWidth = u8g.getStrWidth(textLineVersion);
u8g.drawStr(remainingCenter - textWidth / 2.0, height - 2, textLineVersion);

u8g.sendBuffer();
}

void U8GDisplay::espressoShot(uint32_t currentTimeMs, uint32_t timeToFinishMs, int32_t currentWeightMg, uint32_t targetWeightMg,
Expand Down Expand Up @@ -541,8 +542,6 @@ void U8GDisplay::espressoShot(uint32_t currentTimeMs, uint32_t timeToFinishMs, i
int barProgress = (currentWeightG / targetWeightG) * barWidth;
u8g.drawFrame(barX, barY, barWidth, barHeight);
u8g.drawBox(barX, barY, barProgress, barHeight);

u8g.sendBuffer();
}

#endif
6 changes: 5 additions & 1 deletion code/src/embedded/u8g_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class U8GDisplay : public Display
void recipePour(const char *text, int32_t weightToPourMg, uint64_t timeToFinishMs, bool isPause, uint8_t pourIndex, uint8_t pours) override;
void espressoShot(uint32_t currentTimeMs, uint32_t timeToFinishMs, int32_t currentWeightMg, uint32_t targetWeightMg, bool waiting) override;
void text(const char *text) override;
void update(){};
void update() override;
void clear() override;
void drawOpener();

Expand All @@ -42,6 +42,10 @@ class U8GDisplay : public Display
int drawQRCode(const char* bytes, uint8_t x, uint8_t y);
void drawTextAutoWrap(const char *text, int yTop, int xLeft, int maxWidth);
bool shouldBlinkedBeVisible();
#if ARDUHAL_LOG_LEVEL >= ARDUHAL_LOG_LEVEL_VERBOSE
unsigned long lastFpsLogTime = 0;
unsigned long frameCounter = 0;
#endif
};

#endif

0 comments on commit 9a1a02e

Please sign in to comment.