Skip to content

Commit

Permalink
Add ability to start/stop and configurable preheat when starting a cy…
Browse files Browse the repository at this point in the history
…cle.

- Required adding a new `EncoderShim` to intercept all encoder interactions so that we can know a long-press has occurred.
- Preheat is indicated by a title bar widget.
- Preheat start/stop with long-press is a toggle of current state, it is not impacted in any way by what operation was being done (ie, you start a preheat while stopping a spin cycle with a long-press)
- Made encoder inversion work in new shim
- Switch StealthChop transition point into a simple on/off for StealthChop as a whole.   There isn't any point in having stealthchop enabled at all if we cannot use it across full motor speed range, as the transition is abbrupt, and causes a large jerk.
  • Loading branch information
Kevin Balthaser committed May 4, 2023
1 parent 6e7a716 commit d148ca1
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 48 deletions.
9 changes: 1 addition & 8 deletions src/EncoderShim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

#include "EncoderShim.h"

EncoderShim::EncoderShim(MenuManager *manager) {
menuManager = manager;
EncoderShim::EncoderShim() {

}

Expand All @@ -15,11 +14,7 @@ void EncoderShim::initForEncoder() {
switches.init(internalDigitalIo(), SWITCHES_NO_POLLING, true);

switches.addSwitchListener(BUTTON, this, NO_REPEAT, false);
//switches.addSwitch(encoderButton, nullptr);
//switches.onRelease(encoderButton, EncoderShim::*buttonRelease);
setupRotaryEncoderWithInterrupt(ENC1, ENC2, this, HWACCEL_NONE, FULL_CYCLE);
//setupRotaryEncoderWithInterrupt(encoderPinA, encoderPinB, [](int value) {menuMgr.valueChanged(value); }, HWACCEL_REGULAR, type);

}

void EncoderShim::encoderHasChanged(int newValue) {
Expand All @@ -34,8 +29,6 @@ void EncoderShim::encoderHasChanged(int newValue) {
direction = true;
}

//Serial.println(direction);

encoderValue = newValue;

menuMgr.valueChanged(newValue);
Expand Down
21 changes: 18 additions & 3 deletions src/EncoderShim.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <tcMenu.h>

#ifndef KIWIBOARDFIRMWARE_ENCODERSHIM_H

#define KIWIBOARDFIRMWARE_ENCODERSHIM_H


Expand All @@ -20,11 +19,27 @@ typedef void (*EncoderShimFn)(bool direction, bool longPress);
class EncoderShim : EncoderListener, SwitchListener {

public:
EncoderShim(MenuManager* manager);
EncoderShim();

/**
* Initialize the encoder and button for pins defined in picoPlatform. This must be called
* before setupMenu.
*/
void initForEncoder();

/**
* Register the function that should be called when the encoder value changes.
*/
void registerChangeCallback(EncoderShimFn callback);

/**
* Register the function that should be called when the encoder button has been pressed.
*/
void registerClickCallback(EncoderShimFn callback);

/**
* Swap the two encoder pins, this will invert the direction of the encoder in all modes.
*/
void invertEncoderDirection();

private:
Expand All @@ -34,7 +49,7 @@ class EncoderShim : EncoderListener, SwitchListener {

void onReleased(pinid_t pin, bool held) override;

MenuManager* menuManager;
// Callback Functions for click and change
EncoderShimFn encoderChangeFn = nullptr;
EncoderShimFn encoderClickFn = nullptr;

Expand Down
36 changes: 2 additions & 34 deletions src/KiwiBoardFirmware_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ void setup() {
// Init the graphics subsystem and trigger the splash.
gfx.begin();
gfx.setRotation(3);

// turn on the LED if DMA init successfully..
gfx.initDMA(true);

showSplash();
Expand All @@ -68,7 +66,7 @@ void setup() {
gfx.fillScreen(TFT_BLACK);

// Setup switches and encoder?
encoderShim = new EncoderShim(&menuMgr);
encoderShim = new EncoderShim();
encoderShim->initForEncoder();
encoderShim->registerChangeCallback(handleEncoderMove);
encoderShim->registerClickCallback(checkLongPress);
Expand Down Expand Up @@ -102,9 +100,6 @@ void setup() {
menuVersion.setTextValue(VERSION_NUM, true);

setMenuOptions();

setTitlePressedCallback(titleBarClick);

scheduleTasks();
}

Expand Down Expand Up @@ -174,6 +169,7 @@ void ui_tick() {
drawable->startDraw();
drawable->drawXBitmap(Coord(135, 0), Coord(50, 50), KiwiLogoWidIcon0);
drawable->endDraw();

}

// Check for heat icon, if the heater is on, show the icon, otherwise swap in the blank widget
Expand Down Expand Up @@ -239,26 +235,6 @@ void motorErrorDialog(TMC5160::DriverStatus status) {
}
}

/**
* Show version dialog.
*
* @param id
*/
void titleBarClick(int id) {

const char error[] PROGMEM = "Free Heap Usage";

BaseDialog *dlg = renderer.getDialog();
if (dlg) {
dlg->setButtons(BTNTYPE_NONE, BTNTYPE_CLOSE);
dlg->show(error, false);
int freeHeap = rp2040.getFreeHeap();
char cstr[16];
itoa(freeHeap, cstr, 10);
dlg->copyIntoBuffer(cstr);
}
}

void CALLBACK_FUNCTION wash(int id) {
run(0, &menuWash);
}
Expand Down Expand Up @@ -423,8 +399,6 @@ void setMenuOptions() {
// first we get the graphics factory
auto &factory = renderer.getGraphicsPropertiesFactory();

// renderer.takeOverDisplay(checkLongPress);

// don't do the periodic reset, which causes some awkward redraw flashes.
renderer.turnOffResetLogic();

Expand Down Expand Up @@ -461,10 +435,6 @@ void setMenuOptions() {
GridPosition::JUSTIFY_CENTER_VALUE_ONLY, MenuBorder(0));

// Settings for the Settings menu
// // here is how we completely redefine the drawing of a specific item, you can also define for submenu or default
//color_t specialPalette[] { RGB(0, 255, 0), RGB(255, 0, 0), RGB(70, 70, 70), RGB(0, 0, 255) };

// TODO work out these how to style these, because currently, you can't see the cursor when editing multi part large numbers
factory.setDrawingPropertiesAllInSub(ItemDisplayProperties::COMPTYPE_ITEM, menuSettings.getId(),
settingsMenuPalette,
MenuPadding(4), nullptr, 4, 2, 36,
Expand Down Expand Up @@ -497,7 +467,6 @@ void setMenuOptions() {
renderer.setFirstWidget(&HeatWidget);

// Black on white cursor
// factory.setSelectedColors(RGB(255, 0, 0), RGB(0, 255, 0));
factory.setSelectedColors(RGB(255, 255, 255), RGB(0, 0, 0));

// Home the user to the wash menu option
Expand Down Expand Up @@ -563,7 +532,6 @@ void resetIcons() {
factory.addImageToCache(DrawableIcon(menuDry.getId(), iconSize, DrawableIcon::ICON_XBITMAP, CycleIconsBitmap2));
factory.addImageToCache(
DrawableIcon(menuSettings.getId(), iconSize, DrawableIcon::ICON_XBITMAP, CycleIconsBitmap3));
MenuPadding perSidePadding(3, 3, 3, 3);

if (stoppedButton != nullptr) {
MenuPadding buttonPadding(4, 4, 4, 4);
Expand Down
2 changes: 1 addition & 1 deletion src/KiwiBoardFirmware_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void setIconStopped(MenuItem* icon);

void resetIcons();

void checkLongPress(bool direction, bool held);
void checkLongPress(bool direction, bool clicked);

void handleEncoderMove(bool direction, bool held);

Expand Down
4 changes: 2 additions & 2 deletions src/picoPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void PicoPlatform::initializePlatform() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);

// Remap IO to the correct pins for hardware SPI 0
// Remap IO to the correct pins for hardware SPI 1
SPI1.setRX(TMC_MISO);
SPI1.setTX(TMC_MOSI);
SPI1.setSCK(TMC_SCLK);
Expand All @@ -43,7 +43,7 @@ void PicoPlatform::initializePlatform() {
pinMode(LCD_BACKLIGHT, OUTPUT);
analogWrite(LCD_BACKLIGHT, 125);

//Setup SPI0 for the TFT
//Setup Hardware SPI0 for the TFT
SPI.setCS(LCD_CS);
SPI.setRX(LCD_MISO);
SPI.setTX(LCD_MOSI);
Expand Down

0 comments on commit d148ca1

Please sign in to comment.