diff options
author | David Crocker <dcrocker@eschertech.com> | 2018-02-06 02:38:47 +0300 |
---|---|---|
committer | David Crocker <dcrocker@eschertech.com> | 2018-02-06 02:38:47 +0300 |
commit | c5a8315c2d0ab6eb6a7c961e0f44cea198bee38d (patch) | |
tree | c15f0b8d3d7eb6a378992816a4b4223f56d858ef /src | |
parent | 1448121041393c9a878c3dd84ddd97dff2a4687f (diff) |
More work on SAM4S port
Renamed IoPort{.cpp,.h} to avoid name clash with ASF
Fixes to 12864 display
Support beeper on 12864 display
Encoder parameters are now configurable (M918)
Diffstat (limited to 'src')
-rw-r--r-- | src/Display/Display.cpp | 52 | ||||
-rw-r--r-- | src/Display/Display.h | 9 | ||||
-rw-r--r-- | src/Display/RotaryEncoder.cpp | 18 | ||||
-rw-r--r-- | src/Display/RotaryEncoder.h | 7 | ||||
-rw-r--r-- | src/Display/ST7920/lcd7920.cpp | 35 | ||||
-rw-r--r-- | src/GCodes/GCodes2.cpp | 16 | ||||
-rw-r--r-- | src/IoPorts.cpp (renamed from src/IoPort.cpp) | 2 | ||||
-rw-r--r-- | src/IoPorts.h (renamed from src/IoPort.h) | 4 | ||||
-rw-r--r-- | src/Platform.cpp | 13 | ||||
-rw-r--r-- | src/Platform.h | 2 | ||||
-rw-r--r-- | src/PortControl.h | 2 | ||||
-rw-r--r-- | src/RepRap.cpp | 27 | ||||
-rw-r--r-- | src/RepRap.h | 4 | ||||
-rw-r--r-- | src/Version.h | 2 |
14 files changed, 147 insertions, 46 deletions
diff --git a/src/Display/Display.cpp b/src/Display/Display.cpp index aa8fcba6..264978ce 100644 --- a/src/Display/Display.cpp +++ b/src/Display/Display.cpp @@ -7,14 +7,18 @@ #include "Display.h" #include "GCodes/GCodes.h" +#include "IoPort.h" +#include "Pins.h" + +constexpr int DefaultPulsesPerClick = -4; // values that work with displays I have are 2 and -4 extern const LcdFont font16x16; -//extern const LcdFont font10x10; +extern const LcdFont font10x10; static int val = 0; Display::Display() - : lcd(LcdCSPin), encoder(EncoderPinA, EncoderPinB, EncoderPinSw, 4) + : lcd(LcdCSPin), encoder(EncoderPinA, EncoderPinB, EncoderPinSw), present(false) { //TODO init menus here } @@ -22,7 +26,7 @@ Display::Display() void Display::Init() { lcd.Init(); - encoder.Init(); + encoder.Init(DefaultPulsesPerClick); //TODO display top menu here // For now we just print some text to test the display @@ -35,6 +39,9 @@ void Display::Init() lcd.SetCursor(20, 5); lcd.SetRightMargin(50); lcd.print(val); + + IoPort::SetPinMode(LcdBeepPin, OUTPUT_PWM_LOW); + beepActive = false; } void Display::Spin(bool full) @@ -67,6 +74,11 @@ void Display::Spin(bool full) lcd.FlushSome(); } + + if (beepActive && millis() - whenBeepStarted > beepLength) + { + IoPort::WriteAnalog(LcdBeepPin, 0.0, 0); + } } void Display::Exit() @@ -74,4 +86,38 @@ void Display::Exit() // TODO display a "shutdown" message, or turn the display off? } +void Display::Beep(unsigned int frequency, unsigned int milliseconds) +{ + whenBeepStarted = millis(); + beepLength = milliseconds; + beepActive = true; + IoPort::WriteAnalog(LcdBeepPin, 0.5, (uint16_t)frequency); +} + +GCodeResult Display::Configure(GCodeBuffer& gb, const StringRef& reply) +{ + if (gb.Seen('P') && gb.GetUIValue() == 1) + { + // 12864 display configuration + present = true; + if (gb.Seen('E')) + { + encoder.Init(gb.GetIValue()); // configure encoder pulses per click and direction + } + } + return GCodeResult::ok; +} + +// Suspend normal operation and display an "Updating firmware" message +void Display::UpdatingFirmware() +{ + IoPort::WriteAnalog(LcdBeepPin, 0.0, 0); // stop any beep + lcd.TextInvert(false); + lcd.Clear(); + lcd.SetFont(&font10x10); + lcd.SetCursor(20, 0); + lcd.print("Updating firmware..."); + lcd.FlushAll(); +} + // End diff --git a/src/Display/Display.h b/src/Display/Display.h index fa321a1c..b7c90322 100644 --- a/src/Display/Display.h +++ b/src/Display/Display.h @@ -11,6 +11,7 @@ #include "RotaryEncoder.h" #include "ST7920/lcd7920.h" #include "Menu.h" +#include "GCodes/GCodeResult.h" class Display { @@ -18,13 +19,21 @@ public: Display(); void Init(); + GCodeResult Configure(GCodeBuffer& gb, const StringRef& reply); void Spin(bool full); void Exit(); + void Beep(unsigned int frequency, unsigned int milliseconds); + bool IsPresent() const { return present; } + void UpdatingFirmware(); private: Lcd7920 lcd; RotaryEncoder encoder; Menu *mainMenu; + uint32_t whenBeepStarted; + uint32_t beepLength; + bool present; + bool beepActive; }; #endif /* SRC_DISPLAY_DISPLAY_H_ */ diff --git a/src/Display/RotaryEncoder.cpp b/src/Display/RotaryEncoder.cpp index a862591b..33e9d702 100644 --- a/src/Display/RotaryEncoder.cpp +++ b/src/Display/RotaryEncoder.cpp @@ -4,21 +4,25 @@ #if SUPPORT_12864_LCD #include "RotaryEncoder.h" +#include "IoPorts.h" -RotaryEncoder::RotaryEncoder(Pin p0, Pin p1, Pin pb, int pulsesPerClick) - : pin0(p0), pin1(p1), pinButton(pb), ppc(pulsesPerClick), encoderChange(0), encoderState(0), newPress(false) {} +RotaryEncoder::RotaryEncoder(Pin p0, Pin p1, Pin pb) + : pin0(p0), pin1(p1), pinButton(pb), ppc(1), encoderChange(0), encoderState(0), newPress(false), reverseDirection(false) {} inline unsigned int RotaryEncoder::ReadEncoderState() const { return (digitalRead(pin0) ? 1u : 0u) | (digitalRead(pin1) ? 2u : 0u); } -void RotaryEncoder::Init() +void RotaryEncoder::Init(int pulsesPerClick) { + ppc = max<unsigned int>(abs(pulsesPerClick), 1); + reverseDirection = (pulsesPerClick < 0); + // Set up pins - pinMode(pin0, INPUT_PULLUP); - pinMode(pin1, INPUT_PULLUP); - pinMode(pinButton, INPUT_PULLUP); + IoPort::SetPinMode(pin0, INPUT_PULLUP); + IoPort::SetPinMode(pin1, INPUT_PULLUP); + IoPort::SetPinMode(pinButton, INPUT_PULLUP); delay(2); // ensure we read the initial state correctly // Initialise encoder variables @@ -88,7 +92,7 @@ int RotaryEncoder::GetChange() r = 0; } encoderChange -= (r * ppc); - return r; + return (reverseDirection) ? -r : r; } bool RotaryEncoder::GetButtonPress() diff --git a/src/Display/RotaryEncoder.h b/src/Display/RotaryEncoder.h index 79eac3db..972e7d93 100644 --- a/src/Display/RotaryEncoder.h +++ b/src/Display/RotaryEncoder.h @@ -7,11 +7,12 @@ class RotaryEncoder { const Pin pin0, pin1, pinButton; - const int ppc; + int ppc; int encoderChange; unsigned int encoderState; bool buttonState; bool newPress; + bool reverseDirection; uint32_t whenSame; unsigned int ReadEncoderState() const; @@ -19,9 +20,9 @@ class RotaryEncoder static constexpr uint32_t DebounceMillis = 5; public: - RotaryEncoder(Pin p0, Pin p1, Pin pb, int pulsesPerClick); + RotaryEncoder(Pin p0, Pin p1, Pin pb); - void Init(); + void Init(int pulsesPerClick); void Poll(); int GetChange(); bool GetButtonPress(); diff --git a/src/Display/ST7920/lcd7920.cpp b/src/Display/ST7920/lcd7920.cpp index e5b336aa..fd9a9665 100644 --- a/src/Display/ST7920/lcd7920.cpp +++ b/src/Display/ST7920/lcd7920.cpp @@ -153,7 +153,7 @@ size_t Lcd7920::writeNative(uint16_t ch) const uint8_t bytesPerColumn = (fontHeight + 7)/8; const uint8_t bytesPerChar = (bytesPerColumn * fontWidth) + 1; const uint8_t *fontPtr = currentFont->ptr + (bytesPerChar * (ch - startChar)); - uint16_t cmask = (1 << fontHeight) - 1; + const uint16_t cmask = (1u << fontHeight) - 1; uint8_t nCols = *fontPtr++; @@ -171,10 +171,10 @@ size_t Lcd7920::writeNative(uint16_t ch) // We add a space column after a space character if we would have added one between the preceding and following characters. if (column < rightMargin) { - uint16_t thisCharColData = *fontPtr & cmask; - if (thisCharColData == 0) // for characters with deliberate space row at the start, e.g. decimal point + uint16_t thisCharColData = *reinterpret_cast<const uint16_t*>(fontPtr) & cmask; + if (thisCharColData == 0) // for characters with deliberate space column at the start, e.g. decimal point { - thisCharColData = *(fontPtr + 2) & cmask; + thisCharColData = *reinterpret_cast<const uint16_t*>(fontPtr + 2) & cmask; } const bool wantSpace = ((thisCharColData | (thisCharColData << 1)) & (lastCharColData | (lastCharColData << 1))) != 0; if (wantSpace) @@ -200,7 +200,7 @@ size_t Lcd7920::writeNative(uint16_t ch) while (nCols != 0 && column < rightMargin) { - uint16_t colData = *fontPtr; + uint16_t colData = *reinterpret_cast<const uint16_t*>(fontPtr); fontPtr += bytesPerColumn; if (colData != 0) { @@ -212,7 +212,7 @@ size_t Lcd7920::writeNative(uint16_t ch) const uint16_t setPixelVal = (textInverted) ? 0 : 1; for (uint8_t i = 0; i < fontHeight && p < (image + sizeof(image)); ++i) { - if ((colData & 1) == setPixelVal) + if ((colData & 1u) == setPixelVal) { *p |= mask1; // set pixel } @@ -240,10 +240,10 @@ void Lcd7920::SetRightMargin(uint8_t r) rightMargin = (r > numCols) ? numCols : r; } -// Clear a rectangle from the current position to the right margin (graphics mode only). The height of the rectangle is the height of the current font. +// Clear a rectangle from the current position to the right margin. The height of the rectangle is the height of the current font. void Lcd7920::ClearToMargin() { - if (currentFont != 0) + if (currentFont != nullptr) { if (column < rightMargin) { @@ -257,11 +257,20 @@ void Lcd7920::ClearToMargin() if (endRow < nextRow) { endRow = nextRow; } if (endCol < rightMargin) { endCol = rightMargin; } } + while (column < rightMargin) { - // Add space after character - uint8_t mask = 0x80 >> (column & 7); uint8_t *p = image + ((row * (numCols/8)) + (column/8)); + uint8_t mask = 0xFF >> (column & 7); + if ((column & (~7)) < (rightMargin & (~7))) + { + column = (column & (~7)) + 8; + } + else + { + mask ^= 0xFF >> (rightMargin & 7); + column = rightMargin;; + } for (uint8_t i = 0; i < fontHeight && p < (image + sizeof(image)); ++i) { if (textInverted) @@ -274,7 +283,6 @@ void Lcd7920::ClearToMargin() } p += (numCols/8); } - ++column; } } } @@ -370,7 +378,7 @@ void Lcd7920::Circle(uint8_t x0, uint8_t y0, uint8_t radius, PixelMode mode) } } -// Draw a bitmap +// Draw a bitmap. x0 and numCols must be divisible by 8. void Lcd7920::Bitmap(uint8_t x0, uint8_t y0, uint8_t width, uint8_t height, const uint8_t data[]) { for (uint8_t r = 0; r < height && r + y0 < numRows; ++r) @@ -434,7 +442,7 @@ bool Lcd7920::FlushSome() return false; } -// Set the cursor position. We can only set alternate columns. The row addressing is rather odd. +// Set the cursor position void Lcd7920::SetCursor(uint8_t r, uint8_t c) { row = r % numRows; @@ -479,6 +487,7 @@ bool Lcd7920::ReadPixel(uint8_t x, uint8_t y) const return false; } +// Set the address to write to. The column address is in 16-bit words, so it ranges from 0 to 7. void Lcd7920::setGraphicsAddress(unsigned int r, unsigned int c) { ensureExtendedMode(); diff --git a/src/GCodes/GCodes2.cpp b/src/GCodes/GCodes2.cpp index 7b04e4ac..a8d4189c 100644 --- a/src/GCodes/GCodes2.cpp +++ b/src/GCodes/GCodes2.cpp @@ -30,6 +30,10 @@ # include "FirmwareUpdater.h" #endif +#if SUPPORT_12864_LCD +# include "Display/Display.h" +#endif + #include <utility> // for std::swap // If the code to act on is completed, this returns true, otherwise false. @@ -2094,8 +2098,8 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) case 300: // Beep { - const int ms = (gb.Seen('P')) ? gb.GetIValue() : 1000; // time in milliseconds - const int freq = (gb.Seen('S')) ? gb.GetIValue() : 4600; // 4600Hz produces the loudest sound on a PanelDue + const unsigned int ms = (gb.Seen('P')) ? gb.GetUIValue() : 1000; // time in milliseconds + const unsigned int freq = (gb.Seen('S')) ? gb.GetUIValue() : 4600; // 4600Hz produces the loudest sound on a PanelDue reprap.Beep(freq, ms); } break; @@ -3997,6 +4001,14 @@ bool GCodes::HandleMcode(GCodeBuffer& gb, const StringRef& reply) break; #endif + // 917 set standstill current reduction not yet supported + +#if SUPPORT_12864_LCD + case 918: // Configure direct-connect display + result = reprap.GetDisplay().Configure(gb, reply); + break; +#endif + case 929: // Start/stop event logging result = GetGCodeResultFromError(platform.ConfigureLogging(gb, reply)); break; diff --git a/src/IoPort.cpp b/src/IoPorts.cpp index d5b81821..097cdc7c 100644 --- a/src/IoPort.cpp +++ b/src/IoPorts.cpp @@ -5,7 +5,7 @@ * Author: David */ -#include "IoPort.h" +#include "IoPorts.h" #include "RepRap.h" #include "Platform.h" #include "Configuration.h" diff --git a/src/IoPort.h b/src/IoPorts.h index 17182a2f..c6f3a69f 100644 --- a/src/IoPort.h +++ b/src/IoPorts.h @@ -5,8 +5,8 @@ * Author: David */ -#ifndef SRC_IOPORT_H_ -#define SRC_IOPORT_H_ +#ifndef SRC_IOPORTS_H_ +#define SRC_IOPORTS_H_ #include "RepRapFirmware.h" diff --git a/src/Platform.cpp b/src/Platform.cpp index 8de6c225..28f12fe1 100644 --- a/src/Platform.cpp +++ b/src/Platform.cpp @@ -40,10 +40,9 @@ #include "sd_mmc.h" -#ifdef DUET_NG +#if defined(DUET_NG) # include "TMC2660.h" -#endif -#ifdef DUET_M +#elif defined(DUET_M) # include "TMC22xx.h" #endif @@ -51,6 +50,10 @@ # include "FirmwareUpdater.h" #endif +#if SUPPORT_12864_LCD +# include "Display/Display.h" +#endif + #include <climits> #include <malloc.h> @@ -1023,6 +1026,10 @@ void Platform::UpdateFirmware() return; } +#if SUPPORT_12864_LCD + reprap.GetDisplay().UpdatingFirmware(); // put the firmware update message on the display +#endif + // The machine will be unresponsive for a few seconds, don't risk damaging the heaters... reprap.EmergencyStop(); diff --git a/src/Platform.h b/src/Platform.h index daaa4772..65f2475c 100644 --- a/src/Platform.h +++ b/src/Platform.h @@ -29,7 +29,7 @@ Licence: GPL // Platform-specific includes #include "RepRapFirmware.h" -#include "IoPort.h" +#include "IoPorts.h" #include "DueFlashStorage.h" #include "Fan.h" #include "Heating/TemperatureError.h" diff --git a/src/PortControl.h b/src/PortControl.h index f51a02b7..bc0b6cce 100644 --- a/src/PortControl.h +++ b/src/PortControl.h @@ -9,7 +9,7 @@ #define SRC_PORTCONTROL_H_ #include "RepRapFirmware.h" -#include "IoPort.h" // for PinConfiguration +#include "IoPorts.h" // for PinConfiguration class GCodeBuffer; diff --git a/src/RepRap.cpp b/src/RepRap.cpp index ab1a0a97..cc094d5e 100644 --- a/src/RepRap.cpp +++ b/src/RepRap.cpp @@ -668,8 +668,8 @@ OutputBuffer *RepRap::GetStatusResponse(uint8_t type, ResponseSource source) // Output notifications { - bool sendBeep = ((source == ResponseSource::AUX || !platform->HaveAux()) && beepDuration != 0 && beepFrequency != 0); - bool sendMessage = (message[0] != 0); + const bool sendBeep = ((source == ResponseSource::AUX || !platform->HaveAux()) && beepDuration != 0 && beepFrequency != 0); + const bool sendMessage = (message[0] != 0); float timeLeft = 0.0; if (displayMessageBox && boxTimer != 0) @@ -685,7 +685,7 @@ OutputBuffer *RepRap::GetStatusResponse(uint8_t type, ResponseSource source) // Report beep values if (sendBeep) { - response->catf("\"beepDuration\":%d,\"beepFrequency\":%d", beepDuration, beepFrequency); + response->catf("\"beepDuration\":%u,\"beepFrequency\":%u", beepDuration, beepFrequency); if (sendMessage) { response->cat(","); @@ -1617,16 +1617,29 @@ OutputBuffer *RepRap::GetFilelistResponse(const char *dir) } // Send a beep. We send it to both PanelDue and the web interface. -void RepRap::Beep(int freq, int ms) +void RepRap::Beep(unsigned int freq, unsigned int ms) { - beepFrequency = freq; - beepDuration = ms; + // Limit the frequency and duration to sensible values + freq = constrain<unsigned int>(freq, 50, 10000); + ms = constrain<unsigned int>(ms, 10, 60000); + // If there is an LCD device present, make it beep +#if SUPPORT_12864_LCD + if (display->IsPresent()) + { + display->Beep(freq, ms); + } + else +#endif if (platform->HaveAux()) { - // If there is an LCD device present, make it beep platform->Beep(freq, ms); } + else + { + beepFrequency = freq; + beepDuration = ms; + } } // Send a short message. We send it to both PanelDue and the web interface. diff --git a/src/RepRap.h b/src/RepRap.h index 54e5a520..0a8daa2f 100644 --- a/src/RepRap.h +++ b/src/RepRap.h @@ -103,7 +103,7 @@ public: OutputBuffer *GetFilesResponse(const char* dir, bool flagsDirs); OutputBuffer *GetFilelistResponse(const char* dir); - void Beep(int freq, int ms); + void Beep(unsigned int freq, unsigned int ms); void SetMessage(const char *msg); void SetAlert(const char *msg, const char *title, int mode, float timeout, AxesBitmap controls); void ClearAlert(); @@ -162,7 +162,7 @@ private: String<PASSWORD_LENGTH> password; String<MACHINE_NAME_LENGTH> myName; - int beepFrequency, beepDuration; + unsigned int beepFrequency, beepDuration; char message[MaxMessageLength + 1]; // Message box data diff --git a/src/Version.h b/src/Version.h index dc9efd2a..4dde57ac 100644 --- a/src/Version.h +++ b/src/Version.h @@ -13,7 +13,7 @@ #endif #ifndef DATE -# define DATE "2018-02-04" +# define DATE "2018-02-05" #endif #define AUTHORS "reprappro, dc42, chrishamm, t3p3, dnewman" |