Skip to content

Commit

Permalink
Merge branch 'dev' into fix/pcb_not_building
Browse files Browse the repository at this point in the history
  • Loading branch information
MajorArkwolf authored Aug 17, 2023
2 parents e50449a + 7ad76f9 commit a2cd997
Show file tree
Hide file tree
Showing 207 changed files with 14,534 additions and 18,702 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
root = true

[*.{h,hpp,c,cpp,rc,js,jsx}]
[*.{h,hpp,c,cpp,rc,js,jsx,ts,tsx}]
end_of_line = lf
insert_final_newline = true
charset = utf-8
Expand Down
2 changes: 1 addition & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @Zer0-bit @kstam
* @Zer0-bit @kstam @matiasjrossi
19 changes: 19 additions & 0 deletions .github/workflows/compile-sketch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,22 @@ jobs:
- name: Run static analysis on ${{ matrix.environment }}
run: |
pio check -e ${{ matrix.environment }} --fail-on-defect medium --fail-on-defect high
web-build:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
cache: 'npm'
cache-dependency-path: webserver/web-interface/package-lock.json
- run: npm ci
working-directory: webserver/web-interface/
- run: npm run build --if-present
working-directory: webserver/web-interface/
- run: npm run lint
working-directory: webserver/web-interface/
env:
CI: true
Binary file removed lcd-hmi/nextion-lcd.HMI
Binary file not shown.
Binary file removed lcd-hmi/scales-calibrate.HMI
Binary file not shown.
63 changes: 63 additions & 0 deletions lib/Common/gaggia_settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef GAGGIA_SETTINGS_H
#define GAGGIA_SETTINGS_H

#include <Arduino.h>
#include <vector>
#include <string>

struct BoilerSettings {
uint16_t steamSetPoint; /* Desired steam temperature */
uint16_t offsetTemp; /* Temperature offset from what the thermocouple measures to the water temp */
uint16_t hpwr;
uint16_t mainDivider;
uint16_t brewDivider;
};
struct SystemSettings {
float pumpFlowAtZero; /* A PZ constant used for pump calibration */
uint16_t lcdSleep; /* Time (in minutes) after which the screen should go to sleep */
bool warmupState; /* Should gaggia wait to warmup */
};

struct ScalesSettings {
bool forcePredictive;
bool hwScalesEnabled;
int hwScalesF1;
int hwScalesF2;
bool btScalesEnabled;
bool btScalesAutoConnect;

friend bool operator==(const ScalesSettings& lhs, const ScalesSettings& rhs) {
return lhs.forcePredictive == rhs.forcePredictive &&
lhs.hwScalesEnabled == rhs.hwScalesEnabled &&
lhs.hwScalesF1 == rhs.hwScalesF1 &&
lhs.hwScalesF2 == rhs.hwScalesF2 &&
lhs.btScalesEnabled == rhs.btScalesEnabled &&
lhs.btScalesAutoConnect == rhs.btScalesAutoConnect;
}
};

struct BrewSettings {
bool basketPrefill; /* Whether we should add a prefill phase at the beginning of the profile */
bool homeOnShotFinish; /* Should we exit the graph a few seconds after the shot has finished */
bool brewDeltaState; /* Should gaggia add more heating power during a shot to compensate for cold water entering */
};

struct LedSettings {
bool state; /* Should the led be ON or OFF */
bool disco; /* Should we activate disco mode during brew */
struct Color { /* The led color */
uint8_t R;
uint8_t G;
uint8_t B;
} color;
};

struct GaggiaSettings {
BoilerSettings boiler;
SystemSettings system;
BrewSettings brew;
LedSettings led;
ScalesSettings scales;
};

#endif
210 changes: 28 additions & 182 deletions lib/Common/mcu_comms.cpp
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
/* 09:32 15/03/2023 - change triggering comment */
#include "mcu_comms.h"
#include "proto/proto_serializer.h"
#include "proto/profile_converters.h"
#include "proto/message_converters.h"
#include <stdarg.h>

using namespace std;

size_t ProfileSerializer::neededBufferSize(Profile& profile) const {
return sizeof(profile.phaseCount()) + profile.phaseCount() * sizeof(Phase) + sizeof(profile.globalStopConditions);
}

vector<uint8_t> ProfileSerializer::serializeProfile(Profile& profile) const {
vector<uint8_t> buffer;
buffer.reserve(neededBufferSize(profile));
size_t phaseCount = profile.phaseCount();

memcpy(buffer.data(), &phaseCount, sizeof(phaseCount));
memcpy(buffer.data() + sizeof(phaseCount), profile.phases.data(), phaseCount * sizeof(Phase));
memcpy(buffer.data() + sizeof(phaseCount) + phaseCount * sizeof(Phase), &profile.globalStopConditions, sizeof(profile.globalStopConditions));

return buffer;
}

void ProfileSerializer::deserializeProfile(vector<uint8_t>& buffer, Profile& profile) const {
size_t phaseCount;
memcpy(&phaseCount, buffer.data(), sizeof(profile.phaseCount()));
profile.phases.clear();
profile.phases.reserve(phaseCount);
memcpy(profile.phases.data(), buffer.data() + sizeof(profile.phaseCount()), phaseCount * sizeof(Phase));
memcpy(&profile.globalStopConditions, buffer.data() + sizeof(profile.phaseCount()) + phaseCount * sizeof(Phase), sizeof(profile.globalStopConditions));
}

//---------------------------------------------------------------------------------
//--------------------------- PRIVATE METHODS ----------------------------
//---------------------------------------------------------------------------------
void McuComms::sendMultiPacket(vector<uint8_t>& buffer, size_t dataSize, uint8_t packetID) {
// This is here to allow sending empty messages without allocating/deallocating memory for an empty vector
const std::vector<uint8_t>& emptyVector() {
static const std::vector<uint8_t> emptyVec;
return emptyVec;
}

// Sends a message in multiple packets if necessary
void McuComms::sendMultiPacket(const vector<uint8_t>& buffer, McuCommsMessageType messageType) {
uint8_t packetID = static_cast<uint8_t>(messageType);
size_t dataSize = buffer.size();
log("Sending buffer[%d]: ", dataSize);
logBufferHex(buffer, dataSize);

auto dataPerPacket = static_cast<uint8_t>(packetSize - 2u); // Two bytes are reserved for current index and last index
auto numPackets = static_cast<uint8_t>(dataSize / dataPerPacket);

if (dataSize % dataPerPacket > 0u) // Add an extra transmission if needed
if (numPackets == 0 || dataSize % dataPerPacket > 0u) // Add an extra transmission if needed
numPackets++;

for (uint8_t currentPacket = 0u; currentPacket < numPackets; currentPacket++) {
Expand Down Expand Up @@ -111,49 +98,6 @@ vector<uint8_t> McuComms::receiveMultiPacket() {
return buffer;
}


void McuComms::shotSnapshotReceived(ShotSnapshot& snapshot) const {
if (shotSnapshotCallback) {
shotSnapshotCallback(snapshot);
}
}

void McuComms::profileReceived(Profile& profile) const {
if (profileCallback) {
profileCallback(profile);
}
}

void McuComms::sensorStateSnapshotReceived(SensorStateSnapshot& snapshot) const {
if (sensorStateSnapshotCallback) {
sensorStateSnapshotCallback(snapshot);
}
}

void McuComms::remoteScalesWeightReceived(float weight) const {
if (remoteScalesWeightReceivedCallback) {
remoteScalesWeightReceivedCallback(weight);
}
}

void McuComms::remoteScalesTareCommandReceived() const {
if (remoteScalesTareCommandCallback) {
remoteScalesTareCommandCallback();
}
}

void McuComms::remoteScalesDisconnected() const {
if (remoteScalesDisconnectedCallback) {
remoteScalesDisconnectedCallback();
}
}

void McuComms::responseReceived(McuCommsResponse& response) const {
if (responseReceivedCallback) {
responseReceivedCallback(response);
}
}

void McuComms::log(const char* format, ...) const {
if (!debugPort) return;

Expand All @@ -166,7 +110,7 @@ void McuComms::log(const char* format, ...) const {
debugPort->print(buffer.data());
}

void McuComms::logBufferHex(vector<uint8_t>& buffer, size_t dataSize) const {
void McuComms::logBufferHex(const vector<uint8_t>& buffer, size_t dataSize) const {
if (!debugPort) return;

std::array<char, 3>hex;
Expand Down Expand Up @@ -218,129 +162,31 @@ void McuComms::setDebugPort(Stream* dbgPort) {
McuComms::debugPort = dbgPort;
}

void McuComms::setShotSnapshotCallback(ShotSnapshotReceivedCallback callback) {
shotSnapshotCallback = callback;
}

void McuComms::setProfileReceivedCallback(ProfileReceivedCallback callback) {
profileCallback = callback;
void McuComms::setMessageReceivedCallback(MessageReceivedCallback callback) {
messageReceivedCallback = callback;
}

void McuComms::setSensorStateSnapshotCallback(SensorStateSnapshotReceivedCallback callback) {
sensorStateSnapshotCallback = callback;
void McuComms::sendMessage(McuCommsMessageType messageType) {
sendMessage(messageType, emptyVector());
}

void McuComms::setRemoteScalesWeightReceivedCallback(RemoteScalesWeightReceivedCallback callback) {
remoteScalesWeightReceivedCallback = callback;
}

void McuComms::setRemoteScalesTareCommandCallback(RemoteScalesTareCommandCallback callback) {
remoteScalesTareCommandCallback = callback;
}

void McuComms::setRemoteScalesDisconnectedCallback(RemoteScalesDisconnectedCallback callback) {
remoteScalesDisconnectedCallback = callback;
}

void McuComms::setResponseReceivedCallback(ResponseReceivedCallback callback) {
responseReceivedCallback = callback;
}

void McuComms::sendShotData(const ShotSnapshot& snapshot) {
if (!isConnected()) return;
uint16_t messageSize = transfer.txObj(snapshot);
transfer.sendData(messageSize, static_cast<uint8_t>(McuCommsMessageType::MCUC_DATA_SHOT_SNAPSHOT));
}

void McuComms::sendProfile(Profile& profile) {
if (!isConnected()) return;
size_t dataSize = profileSerializer.neededBufferSize(profile);
vector<uint8_t> buffer = profileSerializer.serializeProfile(profile);
sendMultiPacket(buffer, dataSize, static_cast<uint8_t>(McuCommsMessageType::MCUC_DATA_PROFILE));
}

void McuComms::sendSensorStateSnapshot(const SensorStateSnapshot& snapshot) {
if (!isConnected()) return;
uint16_t messageSize = transfer.txObj(snapshot);
transfer.sendData(messageSize, static_cast<uint8_t>(McuCommsMessageType::MCUC_DATA_SENSOR_STATE_SNAPSHOT));
}

void McuComms::sendResponse(McuCommsResponse response) {
if (!isConnected()) return;
uint16_t messageSize = transfer.txObj(response);
transfer.sendData(messageSize, static_cast<uint8_t>(McuCommsMessageType::MCUC_RESPONSE));
}

void McuComms::sendRemoteScalesWeight(float weight) {
void McuComms::sendMessage(McuCommsMessageType messageType, const std::vector<uint8_t>& data) {
if (!isConnected()) return;
uint16_t messageSize = transfer.txObj(weight);
transfer.sendData(messageSize, static_cast<uint8_t>(McuCommsMessageType::MCUC_DATA_REMOTE_SCALES_WEIGHT));
}

void McuComms::sendRemoteScalesTare() {
if (!isConnected()) return;
uint16_t messageSize = transfer.txObj(static_cast<uint8_t>(McuCommsMessageType::MCUC_CMD_REMOTE_SCALES_TARE));
transfer.sendData(messageSize, static_cast<uint8_t>(McuCommsMessageType::MCUC_CMD_REMOTE_SCALES_TARE));
}

void McuComms::sendRemoteScalesDisconnected() {
if (!isConnected()) return;
uint16_t messageSize = transfer.txObj(static_cast<uint8_t>(McuCommsMessageType::MCUC_DATA_REMOTE_SCALES_DISCONNECTED));
transfer.sendData(messageSize, static_cast<uint8_t>(McuCommsMessageType::MCUC_DATA_REMOTE_SCALES_DISCONNECTED));
sendMultiPacket(data, messageType);
}

void McuComms::readDataAndTick() {
uint8_t availableData = transfer.available();

if (availableData > 0) {
log("Some data is available\n");
lastByteReceived = millis();
switch (static_cast<McuCommsMessageType>(transfer.currentPacketID())) {
case McuCommsMessageType::MCUC_HEARTBEAT: {
break;
} case McuCommsMessageType::MCUC_RESPONSE: {
log("Received a response packet\n");
McuCommsResponse response;
transfer.rxObj(response);
responseReceived(response);
break;
} case McuCommsMessageType::MCUC_DATA_SHOT_SNAPSHOT: {
log("Received a shot snapshot packet\n");
ShotSnapshot snapshot;
transfer.rxObj(snapshot);
shotSnapshotReceived(snapshot);
break;
} case McuCommsMessageType::MCUC_DATA_PROFILE: {
log("Received a profile packet\n");
vector<uint8_t> data = receiveMultiPacket();
Profile profile;
profileSerializer.deserializeProfile(data, profile);
profileReceived(profile);
break;
} case McuCommsMessageType::MCUC_DATA_SENSOR_STATE_SNAPSHOT: {
log("Received a sensor state snapshot packet\n");
SensorStateSnapshot snapshot;
transfer.rxObj(snapshot);
sensorStateSnapshotReceived(snapshot);
break;
} case McuCommsMessageType::MCUC_DATA_REMOTE_SCALES_WEIGHT: {
log("Received a weight packet\n");
float weight = 0.f;
transfer.rxObj(weight);
remoteScalesWeightReceived(weight);
break;
} case McuCommsMessageType::MCUC_CMD_REMOTE_SCALES_TARE: {
log("Received tare command");
remoteScalesTareCommandReceived();
break;
} case McuCommsMessageType::MCUC_DATA_REMOTE_SCALES_DISCONNECTED: {
log("Received scales disconnected message");
remoteScalesDisconnected();
break;
}
default:
log("WARN: Packet ID %d not handled\n", transfer.currentPacketID());
break;
auto messageType = static_cast<McuCommsMessageType>(transfer.currentPacketID());
log("Received a packet [%d]\n", static_cast<uint8_t>(messageType));

if (messageType == McuCommsMessageType::MCUC_HEARTBEAT) return;
auto data = receiveMultiPacket();
if (messageReceivedCallback) {
messageReceivedCallback(messageType, data);
}
}

Expand Down
Loading

0 comments on commit a2cd997

Please sign in to comment.