Optimize code size a bit

pull/141/head
D.R.racer 2022-08-18 08:44:37 +02:00
parent e8805b543e
commit e44ef0d93f
6 changed files with 22 additions and 17 deletions

View File

@ -1,6 +1,7 @@
/// @file eeprom.h /// @file eeprom.h
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <stddef.h>
namespace hal { namespace hal {
@ -9,7 +10,11 @@ namespace eeprom {
class EEPROM { class EEPROM {
public: public:
#ifdef UNITTEST
using addr_t = size_t;
#else
using addr_t = uint16_t; using addr_t = uint16_t;
#endif
static void WriteByte(addr_t addr, uint8_t value); static void WriteByte(addr_t addr, uint8_t value);
static void UpdateByte(addr_t addr, uint8_t value); static void UpdateByte(addr_t addr, uint8_t value);

View File

@ -29,8 +29,7 @@ void logic::FeedToBondtech::GoToPushToNozzle() {
state = PushingFilamentIntoNozzle; state = PushingFilamentIntoNozzle;
} }
void FeedToBondtech::UpdateBowdenLength(int32_t feedEnd_mm) { void FeedToBondtech::UpdateBowdenLength(int32_t measuredBowdenLength) {
int32_t measuredBowdenLength = abs(feedEnd_mm - feedStart_mm);
if (measuredBowdenLength < config::maximumBowdenLength.v) { // is the measured length any valid/acceptable? if (measuredBowdenLength < config::maximumBowdenLength.v) { // is the measured length any valid/acceptable?
static_assert(config::maximumBowdenLength.v <= 65535, "Max bowden length too long"); static_assert(config::maximumBowdenLength.v <= 65535, "Max bowden length too long");
int16_t mbl = (int16_t)measuredBowdenLength; int16_t mbl = (int16_t)measuredBowdenLength;
@ -121,7 +120,7 @@ bool FeedToBondtech::Step() {
dbg_logic_P(PSTR("Feed to Bondtech --> Idler disengaged")); dbg_logic_P(PSTR("Feed to Bondtech --> Idler disengaged"));
dbg_logic_fP(PSTR("Pulley end steps %u"), mpu::pulley.CurrentPosition_mm()); dbg_logic_fP(PSTR("Pulley end steps %u"), mpu::pulley.CurrentPosition_mm());
state = OK; state = OK;
UpdateBowdenLength(mpu::pulley.CurrentPosition_mm()); UpdateBowdenLength(abs(mpu::pulley.CurrentPosition_mm() - feedStart_mm));
ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::on); ml::leds.SetMode(mg::globals.ActiveSlot(), ml::green, ml::on);
} }
return false; return false;

View File

@ -52,7 +52,7 @@ struct FeedToBondtech {
private: private:
/// Update bowden length if changed significantly /// Update bowden length if changed significantly
void UpdateBowdenLength(int32_t feedEnd_mm); void UpdateBowdenLength(int32_t measuredBowdenLength);
/// Common processing of pushing filament into fsensor (reused by multiple states) /// Common processing of pushing filament into fsensor (reused by multiple states)
// bool PushingFilament(); // bool PushingFilament();

View File

@ -53,7 +53,7 @@ constexpr const uint16_t eepromBowdenLenMaximum = config::maximumBowdenLength.v;
namespace ee = hal::eeprom; namespace ee = hal::eeprom;
#define EEOFFSET(x) reinterpret_cast<size_t>(&(x)) #define EEOFFSET(x) reinterpret_cast<ee::EEPROM::addr_t>(&(x))
void Init() { void Init() {
if (ee::EEPROM::ReadByte(ee::EEPROM::End()) != layoutVersion) { if (ee::EEPROM::ReadByte(ee::EEPROM::End()) != layoutVersion) {
@ -93,11 +93,10 @@ static bool validBowdenLen(const uint16_t BowdenLength) {
/// @return stored bowden length /// @return stored bowden length
uint16_t BowdenLength::Get(uint8_t slot) { uint16_t BowdenLength::Get(uint8_t slot) {
if (validFilament(slot)) { if (validFilament(slot)) {
// @@TODO these reinterpret_cast expressions look horrible but I'm keeping them almost intact to respect the original code from MM_control_01 uint16_t bowdenLength = ee::EEPROM::ReadWord(EEOFFSET(eepromBase->eepromBowdenLen[slot]));
uint16_t bowdenLength = ee::EEPROM::ReadWord(reinterpret_cast<size_t>(&(eepromBase->eepromBowdenLen[slot])));
if (eepromEmpty == bowdenLength) { if (eepromEmpty == bowdenLength) {
const uint8_t LengthCorrectionLegacy = ee::EEPROM::ReadByte(reinterpret_cast<size_t>(&(eepromBase->eepromLengthCorrection))); const uint8_t LengthCorrectionLegacy = ee::EEPROM::ReadByte(EEOFFSET(eepromBase->eepromLengthCorrection));
if (LengthCorrectionLegacy <= 200) { if (LengthCorrectionLegacy <= 200) {
bowdenLength = eepromLengthCorrectionBase + LengthCorrectionLegacy * 10; bowdenLength = eepromLengthCorrectionBase + LengthCorrectionLegacy * 10;
} }

View File

@ -27,7 +27,9 @@ public:
void PlanMove(mm::P_pos_t delta, mm::P_speed_t feed_rate, mm::P_speed_t end_rate = { 0 }); void PlanMove(mm::P_pos_t delta, mm::P_speed_t feed_rate, mm::P_speed_t end_rate = { 0 });
// NOTE: always_inline is required here to force gcc <= 7.x to evaluate each call at compile time // NOTE: always_inline is required here to force gcc <= 7.x to evaluate each call at compile time
void __attribute__((always_inline)) PlanMove(unit::U_mm delta, unit::U_mm_s feed_rate, unit::U_mm_s end_rate = { 0 }) { // But it increases the code size with gcc 7.x by ~200B when PlanMove is called with non-constant feed rates
// -> so we don't know yet how to avoid those stupid to/from float conversions (due to C++ unit system), but we'll find a way around ;)
void /*__attribute__((always_inline))*/ PlanMove(unit::U_mm delta, unit::U_mm_s feed_rate, unit::U_mm_s end_rate = { 0 }) {
PlanMove(mm::unitToAxisUnit<mm::P_pos_t>(delta), PlanMove(mm::unitToAxisUnit<mm::P_pos_t>(delta),
mm::unitToAxisUnit<mm::P_speed_t>(feed_rate), mm::unitToAxisUnit<mm::P_speed_t>(feed_rate),
mm::unitToAxisUnit<mm::P_speed_t>(end_rate)); mm::unitToAxisUnit<mm::P_speed_t>(end_rate));

View File

@ -244,7 +244,7 @@ static const RegisterRec registers[] /*PROGMEM*/ = {
RegisterRec(false, &project_build_number), RegisterRec(false, &project_build_number),
// 0x04 // 0x04
RegisterRec( // MMU errors RegisterRec( // MMU errors
[]() -> uint16_t { return mg::globals.DriveErrors(); }, []() -> uint16_t { return mg::globals.DriveErrors(); }, // compiles to: <{lambda()#1}::_FUN()>: jmp <modules::permanent_storage::DriveError::get()>
// [](uint16_t) {}, // @@TODO think about setting/clearing the error counter from the outside // [](uint16_t) {}, // @@TODO think about setting/clearing the error counter from the outside
2), 2),
// 0x05 // 0x05
@ -269,18 +269,18 @@ static const RegisterRec registers[] /*PROGMEM*/ = {
RegisterRec([]() -> uint16_t { return static_cast<uint16_t>(mg::globals.MotorsStealth()); }, 1), RegisterRec([]() -> uint16_t { return static_cast<uint16_t>(mg::globals.MotorsStealth()); }, 1),
// 0xb extra load distance after fsensor triggered (30mm default) [mm] RW // 0xb extra load distance after fsensor triggered (30mm default) [mm] RW
RegisterRec( RegisterRec(
[]() -> uint16_t { return mg::globals.FSensorToNozzle_mm().v; }, []() -> uint16_t { return mm::truncatedUnit(mg::globals.FSensorToNozzle_mm()); },
[](uint16_t d) { mg::globals.SetFSensorToNozzle_mm(d); }, [](uint16_t d) { mg::globals.SetFSensorToNozzle_mm(d); },
1), 1),
// 0x0c fsensor unload check distance (40mm default) [mm] RW // 0x0c fsensor unload check distance (40mm default) [mm] RW
RegisterRec( RegisterRec(
[]() -> uint16_t { return mg::globals.FSensorUnloadCheck_mm().v; }, []() -> uint16_t { return mm::truncatedUnit(mg::globals.FSensorUnloadCheck_mm()); },
[](uint16_t d) { mg::globals.SetFSensorUnloadCheck_mm(d); }, [](uint16_t d) { mg::globals.SetFSensorUnloadCheck_mm(d); },
1), 1),
// 0xd 2 Pulley unload feedrate [mm/s] RW // 0xd 2 Pulley unload feedrate [mm/s] RW
RegisterRec( RegisterRec(
[]() -> uint16_t { return mg::globals.PulleyUnloadFeedrate_mm_s().v; }, []() -> uint16_t { return mm::truncatedUnit(mg::globals.PulleyUnloadFeedrate_mm_s()); },
[](uint16_t d) { mg::globals.SetPulleyUnloadFeedrate_mm_s(d); }, [](uint16_t d) { mg::globals.SetPulleyUnloadFeedrate_mm_s(d); },
2), 2),
@ -302,23 +302,23 @@ static const RegisterRec registers[] /*PROGMEM*/ = {
// 0x11 Pulley load feedrate [mm/s] RW // 0x11 Pulley load feedrate [mm/s] RW
RegisterRec( RegisterRec(
[]() -> uint16_t { return mg::globals.PulleyLoadFeedrate_mm_s().v; }, []() -> uint16_t { return mm::truncatedUnit(mg::globals.PulleyLoadFeedrate_mm_s()); },
[](uint16_t d) { mg::globals.SetPulleyLoadFeedrate_mm_s(d); }, [](uint16_t d) { mg::globals.SetPulleyLoadFeedrate_mm_s(d); },
2), 2),
// 0x12 Selector nominal feedrate [mm/s] RW // 0x12 Selector nominal feedrate [mm/s] RW
RegisterRec( RegisterRec(
[]() -> uint16_t { return mg::globals.SelectorFeedrate_mm_s().v; }, []() -> uint16_t { return mm::truncatedUnit(mg::globals.SelectorFeedrate_mm_s()); },
[](uint16_t d) { mg::globals.SetSelectorFeedrate_mm_s(d); }, [](uint16_t d) { mg::globals.SetSelectorFeedrate_mm_s(d); },
2), 2),
// 0x13 Idler nominal feedrate [deg/s] RW // 0x13 Idler nominal feedrate [deg/s] RW
RegisterRec( RegisterRec(
[]() -> uint16_t { return mg::globals.IdlerFeedrate_deg_s().v; }, []() -> uint16_t { return mm::truncatedUnit(mg::globals.IdlerFeedrate_deg_s()); },
[](uint16_t d) { mg::globals.SetIdlerFeedrate_deg_s(d); }, [](uint16_t d) { mg::globals.SetIdlerFeedrate_deg_s(d); },
2), 2),
// 0x14 Pulley slow load to fsensor feedrate [mm/s] RW // 0x14 Pulley slow load to fsensor feedrate [mm/s] RW
RegisterRec( RegisterRec(
[]() -> uint16_t { return mg::globals.PulleySlowFeedrate_mm_s().v; }, []() -> uint16_t { return mm::truncatedUnit(mg::globals.PulleySlowFeedrate_mm_s()); },
[](uint16_t d) { mg::globals.SetPulleySlowFeedrate_mm_s(d); }, [](uint16_t d) { mg::globals.SetPulleySlowFeedrate_mm_s(d); },
2), 2),
// 0x15 Selector homing feedrate [mm/s] RW // 0x15 Selector homing feedrate [mm/s] RW