diff --git a/src/hal/eeprom.h b/src/hal/eeprom.h index edb1eea..2e0d055 100644 --- a/src/hal/eeprom.h +++ b/src/hal/eeprom.h @@ -1,6 +1,7 @@ /// @file eeprom.h #pragma once #include +#include namespace hal { @@ -9,7 +10,11 @@ namespace eeprom { class EEPROM { public: +#ifdef UNITTEST + using addr_t = size_t; +#else using addr_t = uint16_t; +#endif static void WriteByte(addr_t addr, uint8_t value); static void UpdateByte(addr_t addr, uint8_t value); diff --git a/src/logic/feed_to_bondtech.cpp b/src/logic/feed_to_bondtech.cpp index 1894b1e..f004271 100644 --- a/src/logic/feed_to_bondtech.cpp +++ b/src/logic/feed_to_bondtech.cpp @@ -29,8 +29,7 @@ void logic::FeedToBondtech::GoToPushToNozzle() { state = PushingFilamentIntoNozzle; } -void FeedToBondtech::UpdateBowdenLength(int32_t feedEnd_mm) { - int32_t measuredBowdenLength = abs(feedEnd_mm - feedStart_mm); +void FeedToBondtech::UpdateBowdenLength(int32_t measuredBowdenLength) { if (measuredBowdenLength < config::maximumBowdenLength.v) { // is the measured length any valid/acceptable? static_assert(config::maximumBowdenLength.v <= 65535, "Max bowden length too long"); int16_t mbl = (int16_t)measuredBowdenLength; @@ -121,7 +120,7 @@ bool FeedToBondtech::Step() { dbg_logic_P(PSTR("Feed to Bondtech --> Idler disengaged")); dbg_logic_fP(PSTR("Pulley end steps %u"), mpu::pulley.CurrentPosition_mm()); 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); } return false; diff --git a/src/logic/feed_to_bondtech.h b/src/logic/feed_to_bondtech.h index 017dd23..5e5dbaf 100644 --- a/src/logic/feed_to_bondtech.h +++ b/src/logic/feed_to_bondtech.h @@ -52,7 +52,7 @@ struct FeedToBondtech { private: /// 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) // bool PushingFilament(); diff --git a/src/modules/permanent_storage.cpp b/src/modules/permanent_storage.cpp index cff6319..000b8d7 100644 --- a/src/modules/permanent_storage.cpp +++ b/src/modules/permanent_storage.cpp @@ -53,7 +53,7 @@ constexpr const uint16_t eepromBowdenLenMaximum = config::maximumBowdenLength.v; namespace ee = hal::eeprom; -#define EEOFFSET(x) reinterpret_cast(&(x)) +#define EEOFFSET(x) reinterpret_cast(&(x)) void Init() { if (ee::EEPROM::ReadByte(ee::EEPROM::End()) != layoutVersion) { @@ -93,11 +93,10 @@ static bool validBowdenLen(const uint16_t BowdenLength) { /// @return stored bowden length uint16_t BowdenLength::Get(uint8_t 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(reinterpret_cast(&(eepromBase->eepromBowdenLen[slot]))); + uint16_t bowdenLength = ee::EEPROM::ReadWord(EEOFFSET(eepromBase->eepromBowdenLen[slot])); if (eepromEmpty == bowdenLength) { - const uint8_t LengthCorrectionLegacy = ee::EEPROM::ReadByte(reinterpret_cast(&(eepromBase->eepromLengthCorrection))); + const uint8_t LengthCorrectionLegacy = ee::EEPROM::ReadByte(EEOFFSET(eepromBase->eepromLengthCorrection)); if (LengthCorrectionLegacy <= 200) { bowdenLength = eepromLengthCorrectionBase + LengthCorrectionLegacy * 10; } diff --git a/src/modules/pulley.h b/src/modules/pulley.h index b2c2d2f..0510f03 100644 --- a/src/modules/pulley.h +++ b/src/modules/pulley.h @@ -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 }); // 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(delta), mm::unitToAxisUnit(feed_rate), mm::unitToAxisUnit(end_rate)); diff --git a/src/registers.cpp b/src/registers.cpp index b4d5ea0..58bf602 100644 --- a/src/registers.cpp +++ b/src/registers.cpp @@ -244,7 +244,7 @@ static const RegisterRec registers[] /*PROGMEM*/ = { RegisterRec(false, &project_build_number), // 0x04 RegisterRec( // MMU errors - []() -> uint16_t { return mg::globals.DriveErrors(); }, + []() -> uint16_t { return mg::globals.DriveErrors(); }, // compiles to: <{lambda()#1}::_FUN()>: jmp // [](uint16_t) {}, // @@TODO think about setting/clearing the error counter from the outside 2), // 0x05 @@ -269,18 +269,18 @@ static const RegisterRec registers[] /*PROGMEM*/ = { RegisterRec([]() -> uint16_t { return static_cast(mg::globals.MotorsStealth()); }, 1), // 0xb extra load distance after fsensor triggered (30mm default) [mm] RW 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); }, 1), // 0x0c fsensor unload check distance (40mm default) [mm] RW 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); }, 1), // 0xd 2 Pulley unload feedrate [mm/s] RW 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); }, 2), @@ -302,23 +302,23 @@ static const RegisterRec registers[] /*PROGMEM*/ = { // 0x11 Pulley load feedrate [mm/s] RW 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); }, 2), // 0x12 Selector nominal feedrate [mm/s] RW 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); }, 2), // 0x13 Idler nominal feedrate [deg/s] RW 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); }, 2), // 0x14 Pulley slow load to fsensor feedrate [mm/s] RW 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); }, 2), // 0x15 Selector homing feedrate [mm/s] RW