Skip to content

Commit

Permalink
add SetStatusBarColor gcodes and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vegano1 committed Dec 16, 2024
1 parent 7f68457 commit 47241d9
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 73 deletions.
19 changes: 0 additions & 19 deletions stm32-modules/flex-stacker/firmware/system/i2c_hardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,6 @@ bool i2c_register_handle(HAL_I2C_HANDLE handle, I2C_BUS bus) {
return true;
}

/**
* @brief Checks if the device is communicating.
*/
uint8_t hal_i2c_comms_ready(HAL_I2C_HANDLE handle, uint16_t dev_address) {
HAL_StatusTypeDef status = HAL_OK;
//status = HAL_I2C_IsDeviceReady(handle, dev_address, MAX_RETRIES, MAX_TIMEOUT);
return status;
}

/**
* Wrapper around HAL_I2C_Mem_Write
*/
Expand All @@ -194,11 +185,6 @@ uint8_t hal_i2c_write(I2C_BUS bus, uint16_t DevAddress, uint8_t reg, uint8_t *da
// Bus was not registered
if(notification_handle == NULL) return false;

// Make sure the device is communicating.
HAL_StatusTypeDef dev_status = HAL_OK;
dev_status = hal_i2c_comms_ready(i2c_handle, DevAddress);
if (dev_status != HAL_OK) return dev_status;

uint8_t retries = 0;
HAL_StatusTypeDef tx_result = HAL_OK;
do {
Expand Down Expand Up @@ -229,11 +215,6 @@ uint8_t hal_i2c_read(I2C_BUS bus, uint16_t DevAddress, uint16_t reg, uint8_t *da
// Bus was not registered
if(notification_handle == NULL) return NO_HANDLE_ERROR;

// Make sure the device is communicating.
HAL_StatusTypeDef dev_status = HAL_OK;
dev_status = hal_i2c_comms_ready(i2c_handle, DevAddress);
if (dev_status != HAL_OK) return dev_status;

uint8_t retries = 0;
HAL_StatusTypeDef rx_result = HAL_OK;
do {
Expand Down
1 change: 1 addition & 0 deletions stm32-modules/flex-stacker/firmware/ui/ui_policy.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#include <cstdint>

#include "FreeRTOS.h"
Expand Down
3 changes: 3 additions & 0 deletions stm32-modules/flex-stacker/src/errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const char* const SYSTEM_SERIAL_NUMBER_HAL_ERROR =
"ERR302:system:HAL error, busy, or timeout\n";
const char* const SYSTEM_EEPROM_ERROR =
"ERR303:system:EEPROM communication error\n";
const char* const SYSTEM_SET_STATUSBAR_COLOR_ERROR =
"ERR304:system:STATUSBAR communication error\n";
const char* const TMC2160_READ_ERROR = "ERR901:TMC2160 driver read error\n";
const char* const TMC2160_WRITE_ERROR = "ERR902:TMC2160 driver write error\n";
const char* const TMC2160_INVALID_ADDRESS = "ERR903:TMC2160 invalid address\n";
Expand Down Expand Up @@ -49,6 +51,7 @@ auto errors::errorstring(ErrorCode code) -> const char* {
HANDLE_CASE(SYSTEM_SERIAL_NUMBER_INVALID);
HANDLE_CASE(SYSTEM_SERIAL_NUMBER_HAL_ERROR);
HANDLE_CASE(SYSTEM_EEPROM_ERROR);
HANDLE_CASE(SYSTEM_SET_STATUSBAR_COLOR_ERROR);
HANDLE_CASE(TMC2160_READ_ERROR);
HANDLE_CASE(TMC2160_WRITE_ERROR);
HANDLE_CASE(TMC2160_INVALID_ADDRESS);
Expand Down
2 changes: 1 addition & 1 deletion stm32-modules/include/flex-stacker/firmware/ui_policy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <array>
#include <cstdint>

#include "task.h"
#include "i2c_comms.hpp"
#include "task.h"

namespace ui_policy {
using namespace i2c::hardware;
Expand Down
1 change: 1 addition & 0 deletions stm32-modules/include/flex-stacker/flex-stacker/errors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ enum class ErrorCode {
SYSTEM_SERIAL_NUMBER_INVALID = 301,
SYSTEM_SERIAL_NUMBER_HAL_ERROR = 302,
SYSTEM_EEPROM_ERROR = 303,
SYSTEM_SET_STATUSBAR_COLOR_ERROR = 304,
// 9xx - TMC2160
TMC2160_READ_ERROR = 901,
TMC2160_WRITE_ERROR = 902,
Expand Down
53 changes: 53 additions & 0 deletions stm32-modules/include/flex-stacker/flex-stacker/gcodes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,57 @@ struct GetDoorClosed {
}
};

struct SetStatusBarColor {
StatusBarID bar_id;
std::optional<StatusBarColor> color;
float power;

using ParseResult = std::optional<SetStatusBarColor>;
static constexpr auto prefix = std::array{'M', '2', '0', '0', ' '};
static constexpr const char* response = "M200 OK\n";

using IArg = Arg<uint8_t, 'I'>;
using EArg = Arg<uint8_t, 'E'>;
using ColorArg = Arg<uint8_t, 'C'>;

template <typename InputIt, typename Limit>
requires std::forward_iterator<InputIt> &&
std::sized_sentinel_for<Limit, InputIt>
static auto parse(const InputIt& input, Limit limit)
-> std::pair<ParseResult, InputIt> {
auto res = gcode::SingleParser<IArg, EArg, ColorArg>::parse_gcode(
input, limit, prefix);
if (!res.first.has_value()) {
return std::make_pair(ParseResult(), input);
}

auto ret = SetStatusBarColor{.bar_id = StatusBarID::Internal,
.color = std::nullopt,
.power = 0};

auto arguments = res.first.value();
if (std::get<0>(arguments).present) {
ret.power = static_cast<float>(std::get<0>(arguments).value);
} else if (std::get<1>(arguments).present) {
ret.bar_id = StatusBarID::External;
ret.power = static_cast<float>(std::get<1>(arguments).value);
} else {
return std::make_pair(ParseResult(), input);
}

if (std::get<2>(arguments).present) {
ret.color =
static_cast<StatusBarColor>(std::get<2>(arguments).value);
}
return std::make_pair(ret, res.second);
}

template <typename InputIt, typename InLimit>
requires std::forward_iterator<InputIt> &&
std::sized_sentinel_for<InputIt, InLimit>
static auto write_response_into(InputIt buf, InLimit limit) -> InputIt {
return write_string_to_iterpair(buf, limit, response);
}
};

} // namespace gcode
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "flex-stacker/errors.hpp"
#include "flex-stacker/gcodes.hpp"
#include "flex-stacker/messages.hpp"
#include "gcodes.hpp"
#include "gcodes_motor.hpp"
#include "hal/message_queue.hpp"
#include "messages.hpp"
Expand Down Expand Up @@ -48,14 +49,15 @@ class HostCommsTask {
gcode::GetLimitSwitches, gcode::SetMicrosteps, gcode::GetMoveParams,
gcode::SetMotorStallGuard, gcode::GetMotorStallGuard, gcode::HomeMotor,
gcode::GetPlatformSensors, gcode::GetDoorClosed, gcode::GetEstopStatus,
gcode::StopMotor>;
gcode::StopMotor, gcode::SetStatusBarColor>;
using AckOnlyCache =
AckCache<8, gcode::EnterBootloader, gcode::SetSerialNumber,
gcode::SetTMCRegister, gcode::SetRunCurrent,
gcode::SetHoldCurrent, gcode::EnableMotor, gcode::DisableMotor,
gcode::MoveMotorInSteps, gcode::MoveToLimitSwitch,
gcode::MoveMotorInMm, gcode::SetMicrosteps,
gcode::SetMotorStallGuard, gcode::HomeMotor, gcode::StopMotor>;
gcode::SetStatusBarColor, gcode::SetMotorStallGuard,
gcode::HomeMotor, gcode::StopMotor>;
using GetSystemInfoCache = AckCache<8, gcode::GetSystemInfo>;
using GetTMCRegisterCache = AckCache<8, gcode::GetTMCRegister>;
using GetLimitSwitchesCache = AckCache<8, gcode::GetLimitSwitches>;
Expand Down Expand Up @@ -997,6 +999,32 @@ class HostCommsTask {
return std::make_pair(true, tx_into);
}

template <typename InputIt, typename InputLimit>
requires std::forward_iterator<InputIt> &&
std::sized_sentinel_for<InputLimit, InputIt>
auto visit_gcode(const gcode::SetStatusBarColor& gcode, InputIt tx_into,
InputLimit tx_limit) -> std::pair<bool, InputIt> {
auto id = ack_only_cache.add(gcode);
if (id == 0) {
return std::make_pair(
false, errors::write_into(tx_into, tx_limit,
errors::ErrorCode::GCODE_CACHE_FULL));
}
auto message = messages::SetStatusBarColorMessage{
.id = id,
.bar_id = gcode.bar_id,
.power = gcode.power,
.color = gcode.color,
};
if (!task_registry->send(message, TICKS_TO_WAIT_ON_SEND)) {
auto wrote_to = errors::write_into(
tx_into, tx_limit, errors::ErrorCode::INTERNAL_QUEUE_FULL);
ack_only_cache.remove_if_present(id);
return std::make_pair(false, wrote_to);
}
return std::make_pair(true, tx_into);
}

// Our error handler just writes an error and bails
template <typename InputIt, typename InputLimit>
requires std::forward_iterator<InputIt> &&
Expand Down
11 changes: 7 additions & 4 deletions stm32-modules/include/flex-stacker/flex-stacker/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,12 @@ struct GetEstopResponse {
bool triggered;
};

// This empty message is just used to signal that the UI task should update
// its outputs
struct UpdateUIMessage {};
struct SetStatusBarColorMessage {
uint32_t id = 0;
StatusBarID bar_id = StatusBarID::Internal;
float power = 0;
std::optional<StatusBarColor> color = std::nullopt;
};

using HostCommsMessage =
::std::variant<std::monostate, IncomingMessageFromHost, ForceUSBDisconnect,
Expand All @@ -289,7 +292,7 @@ using SystemMessage =
SetSerialNumberMessage, EnterBootloaderMessage,
GetDoorClosedMessage>;

using UIMessage = ::std::variant<std::monostate, UpdateUIMessage>;
using UIMessage = ::std::variant<std::monostate, SetStatusBarColorMessage>;

using MotorDriverMessage =
::std::variant<std::monostate, SetTMCRegisterMessage, GetTMCRegisterMessage,
Expand Down
Loading

0 comments on commit 47241d9

Please sign in to comment.