Skip to content

Commit

Permalink
Separated user interface and display headers, made interface not be a…
Browse files Browse the repository at this point in the history
… class
  • Loading branch information
orlopau committed Dec 7, 2024
1 parent f8c2959 commit eade1de
Show file tree
Hide file tree
Showing 46 changed files with 354 additions and 364 deletions.
5 changes: 4 additions & 1 deletion code/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,8 @@
"geometry": "cpp",
"forward_list": "cpp",
"regex": "cpp"
}
},
"java.project.sourcePaths": [
"test/native"
]
}
24 changes: 24 additions & 0 deletions code/include/interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include "button.h"

namespace Interface
{
enum class EncoderDirection
{
NONE = 0,
CW = 1,
CCW = -1,
};

void begin();
void update();
EncoderDirection getEncoderDirection();
long getEncoderTicks();
void setEncoderTicks(long ticks);
void resetEncoderTicks();
ClickType consumeEncoderClick();
ClickType getEncoderClick();
bool isEncoderPressed();
void buzzerTone(unsigned int durationMs);
}
2 changes: 2 additions & 0 deletions code/include/loadcell.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

namespace LoadCell
{
void begin();
Expand Down
13 changes: 13 additions & 0 deletions code/include/mock/mock_interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "interface.h"

namespace Interface
{
extern ClickType encoderClick;
extern ClickType bootClick;
extern int encoderTicks;
extern EncoderDirection encoderDirection;

void reset();
}
24 changes: 1 addition & 23 deletions code/include/mocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "battery.h"
#include "scale.h"
#include "millis.h"
#include "user_interface.h"
#include "display.h"
#include <math.h>
#include <string.h>

Expand All @@ -19,28 +19,6 @@ class MockWeightSensor : public WeightSensor
bool newWeight = false;
};

class MockButtons : public UserInput
{
public:
void update() {}
long getEncoderTicks() { return encoderTicks; };
void resetEncoderTicks() { encoderTicks = 0; };
ClickType consumeEncoderClick()
{
ClickType click = encoderClick;
encoderClick = ClickType::NONE;
return click;
}
ClickType getEncoderClick() { return encoderClick; };
EncoderDirection getEncoderDirection() { return encoderDirection; };
void setEncoderTicks(long ticks) { encoderTicks = ticks; };
ClickType getBootClick() { return bootClick; };
ClickType encoderClick = ClickType::NONE;
ClickType bootClick = ClickType::NONE;
int encoderTicks = 0;
EncoderDirection encoderDirection = EncoderDirection::NONE;
};

class MockDisplay : public Display
{
public:
Expand Down
2 changes: 1 addition & 1 deletion code/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ platform = native
build_type = test
build_flags =
-DNATIVE
debug_test = native/test_step_brewing
debug_test = native/test_step_switcher
test_filter = native/*
21 changes: 0 additions & 21 deletions code/src/user_interface.h → code/src/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,6 @@
#include <math.h>
#include "button.h"

enum class EncoderDirection
{
NONE = 0,
CW = 1,
CCW = -1,
};

class UserInput
{
public:
virtual ~UserInput() {}
virtual void update() = 0;
virtual EncoderDirection getEncoderDirection() = 0;
virtual long getEncoderTicks() = 0;
virtual void resetEncoderTicks() = 0;
virtual void setEncoderTicks(long ticks) = 0;
virtual ClickType getEncoderClick() = 0;
virtual ClickType consumeEncoderClick() = 0;
virtual void buzzerTone(uint16_t durationMs){};
};

class Display
{
public:
Expand Down
83 changes: 83 additions & 0 deletions code/src/embedded/interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#ifndef NATIVE

#include <Arduino.h>
#include <RotaryEncoder.h>
#include "interface.h"
#include "constants.h"
#include "button.h"

static RotaryEncoder encoder(PIN_ENC_A, PIN_ENC_B, RotaryEncoder::LatchMode::FOUR3);
static Button encoderButton;
static uint64_t lastBuzzerMillis;
static uint32_t lastBuzzerDuration;


void Interface::begin() {
pinMode(PIN_ENC_BTN, INPUT_PULLUP);
pinMode(PIN_BUZZER, OUTPUT);
}

void Interface::update()
{
encoder.tick();
encoderButton.update(digitalRead(PIN_ENC_BTN) == LOW);

if (lastBuzzerMillis + lastBuzzerDuration < millis())
{
digitalWrite(PIN_BUZZER, LOW);
}
}

Interface::EncoderDirection Interface::getEncoderDirection()
{
switch (encoder.getDirection())
{
case RotaryEncoder::Direction::NOROTATION:
return EncoderDirection::NONE;
case RotaryEncoder::Direction::CLOCKWISE:
return EncoderDirection::CW;
case RotaryEncoder::Direction::COUNTERCLOCKWISE:
return EncoderDirection::CCW;
default:
return EncoderDirection::NONE;
}
}

long Interface::getEncoderTicks()
{
return encoder.getPosition();
}

void Interface::setEncoderTicks(long ticks)
{
encoder.setPosition(ticks);
}

void Interface::resetEncoderTicks()
{
encoder.setPosition(0);
}

ClickType Interface::consumeEncoderClick()
{
return encoderButton.consumeClickType();
}

ClickType Interface::getEncoderClick()
{
return encoderButton.getClickType();
}

bool Interface::isEncoderPressed()
{
return encoderButton.isPressed();
}

void Interface::buzzerTone(unsigned int durationMs)
{
digitalWrite(PIN_BUZZER, HIGH);
lastBuzzerMillis = millis();
lastBuzzerDuration = durationMs;
}

#endif
24 changes: 13 additions & 11 deletions code/src/embedded/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "modes/mode_recipe.h"
#include "u8g_display.h"
#include "update.h"
#include "user_input.h"
#include "interface.h"
#include "battery.h"

#define AVERAGING_LOOPS 100
Expand All @@ -23,7 +23,6 @@

DefaultWeightSensor weightSensor;
U8GDisplay display(PIN_I2C_SDA, PIN_I2C_SCL, U8G2_R1);
EmbeddedUserInput input(PIN_ENC_A, PIN_ENC_B, PIN_ENC_BTN, PIN_BUZZER);
Stopwatch stopwatch;

void saveScale(float scale)
Expand All @@ -35,18 +34,18 @@ void saveScale(float scale)
weightSensor.setScale(scale);
}

ModeScale modeDefault(weightSensor, input, display, stopwatch);
ModeEspresso modeEspresso(weightSensor, input, display, stopwatch);
ModeCalibration modeCalibration(input, display, stopwatch, saveScale);
ModeRecipes modeRecipes(weightSensor, input, display, RECIPES, RECIPE_COUNT);
ModeScale modeDefault(weightSensor, display, stopwatch);
ModeEspresso modeEspresso(weightSensor, display, stopwatch);
ModeCalibration modeCalibration(display, stopwatch, saveScale);
ModeRecipes modeRecipes(weightSensor, display, RECIPES, RECIPE_COUNT);
Mode *modes[] = {&modeDefault, &modeRecipes, &modeEspresso, &modeCalibration};
ModeManager modeManager(modes, 4, display, input);
ModeManager modeManager(modes, 4, display);

EncoderDirection encoderDirection;
Interface::EncoderDirection encoderDirection;

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

void setup()
Expand All @@ -57,6 +56,9 @@ void setup()
EEPROM.begin(2048);
btStop();

//////// INTERFACE //////
Interface::begin();

//////// BATTERY ////////
Battery::init();

Expand Down Expand Up @@ -103,7 +105,7 @@ void setup()
//////// UPDATES ////////
if (digitalRead(PIN_UPDATE_FIRMWARE) == LOW)
{
Updater::update_firmware(display, input);
Updater::update_firmware(display);
}

ESP_LOGI(TAG, "Setup finished!");
Expand All @@ -116,7 +118,7 @@ unsigned long lastTime = millis();

void loop()
{
input.update();
Interface::update();
weightSensor.update();
display.update();
modeManager.update();
Expand Down
2 changes: 1 addition & 1 deletion code/src/embedded/u8g_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <U8g2lib.h>

#include "user_interface.h"
#include "display.h"

class U8GDisplay : public Display
{
Expand Down
11 changes: 6 additions & 5 deletions code/src/embedded/update.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include <WiFi.h>

#include "constants.h"
#include "user_input.h"
#include "display.h"
#include "interface.h"
#include "data/localization.h"

#define TAG "UPDATER"
Expand Down Expand Up @@ -39,7 +40,7 @@ namespace Updater
return true;
}

void update_firmware(Display &display, UserInput &input)
void update_firmware(Display &display)
{
display.centerText(UPDATER_UPDATING, 13);
ESP_LOGI(TAG, "Updating firmware...");
Expand Down Expand Up @@ -73,9 +74,9 @@ namespace Updater
const uint8_t numQualifiers = 2;
const char *names[] = {"Deutsch", "English"};
const char *qualifiers[] = {"_de", "_en"};
while (input.getEncoderClick() != ClickType::SINGLE)
while (Interface::getEncoderClick() != ClickType::SINGLE)
{
selectedQualifier += static_cast<int>(input.getEncoderDirection());
selectedQualifier += static_cast<int>(Interface::getEncoderDirection());
if (selectedQualifier < 0)
{
selectedQualifier = 0;
Expand All @@ -85,7 +86,7 @@ namespace Updater
selectedQualifier = numQualifiers - 1;
}
display.switcher("Choose Language", selectedQualifier, numQualifiers, names);
input.update();
Interface::update();
}

HTTPUpdate httpUpdate;
Expand Down
2 changes: 1 addition & 1 deletion code/src/embedded/update.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Updater
{
void update_firmware(Display &display, UserInput &input);
void update_firmware(Display &display);
}

#endif
Loading

0 comments on commit eade1de

Please sign in to comment.