From 27bccab7ceb18a6ddaa9417e7b5f8d1fd80d0ef6 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Tue, 14 May 2019 14:20:31 -0700 Subject: [PATCH 1/4] Refactor screen io into a separate class to support SPI and parallel --- inc/drivers/ILI9341.h | 2 +- inc/drivers/ST7735.h | 34 +++++++++++++++++++++++++++------- source/drivers/ILI9341.cpp | 2 +- source/drivers/ST7735.cpp | 23 ++++++++++++++++++----- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/inc/drivers/ILI9341.h b/inc/drivers/ILI9341.h index b45b7e43..a1f1983d 100644 --- a/inc/drivers/ILI9341.h +++ b/inc/drivers/ILI9341.h @@ -33,7 +33,7 @@ namespace codal class ILI9341 : public ST7735 { public: - ILI9341(SPI &spi, Pin &cs, Pin &dc); + ILI9341(ScreenIO &io, Pin &cs, Pin &dc); int init(); }; diff --git a/inc/drivers/ST7735.h b/inc/drivers/ST7735.h index 91b0ee62..9599adbb 100644 --- a/inc/drivers/ST7735.h +++ b/inc/drivers/ST7735.h @@ -42,10 +42,28 @@ struct ST7735WorkBuffer; #define MADCTL_BGR 0x08 #define MADCTL_MH 0x04 +class ScreenIO +{ +public: + virtual void send(const void *txBuffer, uint32_t txSize) = 0; + virtual void startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler, + void *handlerArg) = 0; +}; + +class SPIScreenIO : public ScreenIO +{ +public: + SPI &spi; + SPIScreenIO(SPI &spi); + virtual void send(const void *txBuffer, uint32_t txSize); + virtual void startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler, + void *handlerArg); +}; + class ST7735 : public CodalComponent { protected: - SPI &spi; + ScreenIO &io; Pin &cs; Pin &dc; uint8_t cmdBuf[20]; @@ -68,14 +86,15 @@ class ST7735 : public CodalComponent static void sendColorsStep(ST7735 *st); public: - ST7735(SPI &spi, Pin &cs, Pin &dc); + ST7735(ScreenIO &io, Pin &cs, Pin &dc); virtual int init(); /** * Configure screen-specific parameters. * * @param madctl See MADCTL_* constants above - * @param frmctr1 defaults to 0x083b3b, 0x053a3a, 0x053c3c depending on screen size; 0x000605 was found to work well on 160x128 screen; big-endian + * @param frmctr1 defaults to 0x083b3b, 0x053a3a, 0x053c3c depending on screen size; 0x000605 + * was found to work well on 160x128 screen; big-endian */ void configure(uint8_t madctl, uint32_t frmctr1); /** @@ -83,12 +102,13 @@ class ST7735 : public CodalComponent */ void setAddrWindow(int x, int y, int w, int h); /** - * Send 4 bit indexed color image, little endian, column-major, using specified palette (use NULL - * if unchanged). + * Send 4 bit indexed color image, little endian, column-major, using specified palette (use + * NULL if unchanged). */ int sendIndexedImage(const uint8_t *src, unsigned width, unsigned height, uint32_t *palette); /** - * Waits for the previous sendIndexedImage() operation to complete (it normally executes in background). + * Waits for the previous sendIndexedImage() operation to complete (it normally executes in + * background). */ void waitForSendDone(); @@ -98,6 +118,6 @@ class ST7735 : public CodalComponent virtual int setSleep(bool sleepMode); }; -} +} // namespace codal #endif \ No newline at end of file diff --git a/source/drivers/ILI9341.cpp b/source/drivers/ILI9341.cpp index f679b814..18b7433c 100644 --- a/source/drivers/ILI9341.cpp +++ b/source/drivers/ILI9341.cpp @@ -148,7 +148,7 @@ static const uint8_t initcmd[] = { namespace codal { -ILI9341::ILI9341(SPI &spi, Pin &cs, Pin &dc) : ST7735(spi, cs, dc) +ILI9341::ILI9341(ScreenIO &io, Pin &cs, Pin &dc) : ST7735(io, cs, dc) { double16 = true; } diff --git a/source/drivers/ST7735.cpp b/source/drivers/ST7735.cpp index 708278b5..f52ed920 100644 --- a/source/drivers/ST7735.cpp +++ b/source/drivers/ST7735.cpp @@ -65,7 +65,20 @@ namespace codal { -ST7735::ST7735(SPI &spi, Pin &cs, Pin &dc) : spi(spi), cs(cs), dc(dc), work(NULL) +SPIScreenIO::SPIScreenIO(SPI &spi) : spi(spi) {} + +void SPIScreenIO::send(const void *txBuffer, uint32_t txSize) +{ + spi.transfer((const uint8_t *)txBuffer, txSize, NULL, 0); +} + +void SPIScreenIO::startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler, + void *handlerArg) +{ + spi.startTransfer((const uint8_t *)txBuffer, txSize, NULL, 0, doneHandler, handlerArg); +} + +ST7735::ST7735(ScreenIO &io, Pin &cs, Pin &dc) : io(io), cs(cs), dc(dc), work(NULL) { double16 = false; } @@ -238,7 +251,7 @@ void ST7735::sendColorsStep(ST7735 *st) base[i + 32 + 64] = (palette[i] >> 2) & 0x3f; } st->startRAMWR(0x2D); - st->spi.transfer(work->dataBuf, 128, NULL, 0); + st->io.send(work->dataBuf, 128); st->cs.setDigitalValue(1); } @@ -286,7 +299,7 @@ void ST7735::sendColorsStep(ST7735 *st) void ST7735::startTransfer(unsigned size) { - spi.startTransfer(work->dataBuf, size, NULL, 0, (PVoidCallback)&ST7735::sendColorsStep, this); + io.startSend(work->dataBuf, size, (PVoidCallback)&ST7735::sendColorsStep, this); } void ST7735::startRAMWR(int cmd) @@ -386,12 +399,12 @@ void ST7735::sendCmd(uint8_t *buf, int len) buf = cmdBuf; dc.setDigitalValue(0); cs.setDigitalValue(0); - spi.transfer(buf, 1, NULL, 0); + io.send(buf, 1); dc.setDigitalValue(1); len--; buf++; if (len > 0) - spi.transfer(buf, len, NULL, 0); + io.send(buf, len); cs.setDigitalValue(1); } From 26c7c47d336062b0cc7529320498626ce5923aac Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Wed, 15 May 2019 08:27:04 -0700 Subject: [PATCH 2/4] add 2G event (which works great for step counting) --- inc/driver-models/Accelerometer.h | 5 ++++- source/driver-models/Accelerometer.cpp | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/inc/driver-models/Accelerometer.h b/inc/driver-models/Accelerometer.h index f0ed1ed1..1a860d44 100644 --- a/inc/driver-models/Accelerometer.h +++ b/inc/driver-models/Accelerometer.h @@ -56,6 +56,7 @@ DEALINGS IN THE SOFTWARE. #define ACCELEROMETER_EVT_6G 9 #define ACCELEROMETER_EVT_8G 10 #define ACCELEROMETER_EVT_SHAKE 11 +#define ACCELEROMETER_EVT_2G 12 /** * Gesture recogniser constants @@ -64,6 +65,7 @@ DEALINGS IN THE SOFTWARE. #define ACCELEROMETER_TILT_TOLERANCE 200 #define ACCELEROMETER_FREEFALL_TOLERANCE 400 #define ACCELEROMETER_SHAKE_TOLERANCE 400 +#define ACCELEROMETER_2G_TOLERANCE 2048 #define ACCELEROMETER_3G_TOLERANCE 3072 #define ACCELEROMETER_6G_TOLERANCE 6144 #define ACCELEROMETER_8G_TOLERANCE 8192 @@ -73,6 +75,7 @@ DEALINGS IN THE SOFTWARE. #define ACCELEROMETER_REST_THRESHOLD (ACCELEROMETER_REST_TOLERANCE * ACCELEROMETER_REST_TOLERANCE) #define ACCELEROMETER_FREEFALL_THRESHOLD ((uint32_t)ACCELEROMETER_FREEFALL_TOLERANCE * (uint32_t)ACCELEROMETER_FREEFALL_TOLERANCE) +#define ACCELEROMETER_2G_THRESHOLD ((uint32_t)ACCELEROMETER_2G_TOLERANCE * (uint32_t)ACCELEROMETER_2G_TOLERANCE) #define ACCELEROMETER_3G_THRESHOLD ((uint32_t)ACCELEROMETER_3G_TOLERANCE * (uint32_t)ACCELEROMETER_3G_TOLERANCE) #define ACCELEROMETER_6G_THRESHOLD ((uint32_t)ACCELEROMETER_6G_TOLERANCE * (uint32_t)ACCELEROMETER_6G_TOLERANCE) #define ACCELEROMETER_8G_THRESHOLD ((uint32_t)ACCELEROMETER_8G_TOLERANCE * (uint32_t)ACCELEROMETER_8G_TOLERANCE) @@ -86,7 +89,7 @@ namespace codal x:1, y:1, z:1, - unused, + impulse_2, impulse_3, impulse_6, impulse_8, diff --git a/source/driver-models/Accelerometer.cpp b/source/driver-models/Accelerometer.cpp index 193afadc..8eb2a42d 100644 --- a/source/driver-models/Accelerometer.cpp +++ b/source/driver-models/Accelerometer.cpp @@ -60,6 +60,7 @@ Accelerometer::Accelerometer(CoordinateSpace &cspace, uint16_t id) : sample(), s this->shake.z = 0; this->shake.count = 0; this->shake.timer = 0; + this->shake.impulse_2 = 1; this->shake.impulse_3 = 1; this->shake.impulse_6 = 1; this->shake.impulse_8 = 1; @@ -223,8 +224,13 @@ void Accelerometer::updateGesture() // For these events, we don't perform any low pass filtering. uint32_t force = instantaneousAccelerationSquared(); - if (force > ACCELEROMETER_3G_THRESHOLD) + if (force > ACCELEROMETER_2G_THRESHOLD) { + if (force > ACCELEROMETER_2G_THRESHOLD && !shake.impulse_2) + { + Event e(DEVICE_ID_GESTURE, ACCELEROMETER_EVT_2G); + shake.impulse_2 = 1; + } if (force > ACCELEROMETER_3G_THRESHOLD && !shake.impulse_3) { Event e(DEVICE_ID_GESTURE, ACCELEROMETER_EVT_3G); @@ -248,7 +254,7 @@ void Accelerometer::updateGesture() if (impulseSigma < ACCELEROMETER_GESTURE_DAMPING) impulseSigma++; else - shake.impulse_3 = shake.impulse_6 = shake.impulse_8 = 0; + shake.impulse_2 = shake.impulse_3 = shake.impulse_6 = shake.impulse_8 = 0; // Determine what it looks like we're doing based on the latest sample... From be3c5e8fb76ba735caa01a79a14bdd713b8440b6 Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Wed, 15 May 2019 18:03:23 -0700 Subject: [PATCH 3/4] Default MADCTL to no X/Y swap --- source/drivers/ILI9341.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/drivers/ILI9341.cpp b/source/drivers/ILI9341.cpp index 18b7433c..7c807d5a 100644 --- a/source/drivers/ILI9341.cpp +++ b/source/drivers/ILI9341.cpp @@ -124,7 +124,7 @@ static const uint8_t initcmd[] = { ILI9341_PWCTR2 , 1, 0x10, // Power control SAP[2:0];BT[3:0] ILI9341_VMCTR1 , 2, 0x3e, 0x28, // VCM control ILI9341_VMCTR2 , 1, 0x86, // VCM control2 - ILI9341_MADCTL , 1, 0x48, // Memory Access Control + ILI9341_MADCTL , 1, 0x08, // Memory Access Control ILI9341_VSCRSADD, 1, 0x00, // Vertical scroll zero ILI9341_PIXFMT , 1, 0x55, ILI9341_FRMCTR1 , 2, 0x00, 0x18, From 61fd8eece00eec58c029e121f9e654d8adae77aa Mon Sep 17 00:00:00 2001 From: Michal Moskal Date: Mon, 20 May 2019 10:52:40 -0700 Subject: [PATCH 4/4] Add separate files for ScreenIO and SPIScreenIO --- inc/driver-models/ScreenIO.h | 43 +++++++++++++++++++++++++++++++++ inc/drivers/SPIScreenIO.h | 47 ++++++++++++++++++++++++++++++++++++ inc/drivers/ST7735.h | 19 +-------------- source/drivers/ST7735.cpp | 13 ---------- source/drivers/ScreenIO.cpp | 19 +++++++++++++++ 5 files changed, 110 insertions(+), 31 deletions(-) create mode 100644 inc/driver-models/ScreenIO.h create mode 100644 inc/drivers/SPIScreenIO.h create mode 100644 source/drivers/ScreenIO.cpp diff --git a/inc/driver-models/ScreenIO.h b/inc/driver-models/ScreenIO.h new file mode 100644 index 00000000..737de7fa --- /dev/null +++ b/inc/driver-models/ScreenIO.h @@ -0,0 +1,43 @@ +/* +The MIT License (MIT) + +Copyright (c) 2017 Lancaster University. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + +#ifndef CODAL_SCREENIO_H +#define CODAL_SCREENIO_H + +#include "CodalConfig.h" + +namespace codal +{ + +class ScreenIO +{ +public: + virtual void send(const void *txBuffer, uint32_t txSize) = 0; + virtual void startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler, + void *handlerArg) = 0; +}; + +} // namespace codal + +#endif diff --git a/inc/drivers/SPIScreenIO.h b/inc/drivers/SPIScreenIO.h new file mode 100644 index 00000000..1d96a34b --- /dev/null +++ b/inc/drivers/SPIScreenIO.h @@ -0,0 +1,47 @@ +/* +The MIT License (MIT) + +Copyright (c) 2017 Lancaster University. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. +*/ + +#ifndef DEVICE_SPISCREENIO_H +#define DEVICE_SPISCREENIO_H + +#include "Pin.h" +#include "SPI.h" +#include "ScreenIO.h" + +namespace codal +{ + +class SPIScreenIO : public ScreenIO +{ +public: + SPI &spi; + SPIScreenIO(SPI &spi); + virtual void send(const void *txBuffer, uint32_t txSize); + virtual void startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler, + void *handlerArg); +}; + +} // namespace codal + +#endif \ No newline at end of file diff --git a/inc/drivers/ST7735.h b/inc/drivers/ST7735.h index 9599adbb..79a834e2 100644 --- a/inc/drivers/ST7735.h +++ b/inc/drivers/ST7735.h @@ -28,6 +28,7 @@ DEALINGS IN THE SOFTWARE. #include "Pin.h" #include "SPI.h" #include "Event.h" +#include "ScreenIO.h" namespace codal { @@ -42,24 +43,6 @@ struct ST7735WorkBuffer; #define MADCTL_BGR 0x08 #define MADCTL_MH 0x04 -class ScreenIO -{ -public: - virtual void send(const void *txBuffer, uint32_t txSize) = 0; - virtual void startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler, - void *handlerArg) = 0; -}; - -class SPIScreenIO : public ScreenIO -{ -public: - SPI &spi; - SPIScreenIO(SPI &spi); - virtual void send(const void *txBuffer, uint32_t txSize); - virtual void startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler, - void *handlerArg); -}; - class ST7735 : public CodalComponent { protected: diff --git a/source/drivers/ST7735.cpp b/source/drivers/ST7735.cpp index f52ed920..ad0de32f 100644 --- a/source/drivers/ST7735.cpp +++ b/source/drivers/ST7735.cpp @@ -65,19 +65,6 @@ namespace codal { -SPIScreenIO::SPIScreenIO(SPI &spi) : spi(spi) {} - -void SPIScreenIO::send(const void *txBuffer, uint32_t txSize) -{ - spi.transfer((const uint8_t *)txBuffer, txSize, NULL, 0); -} - -void SPIScreenIO::startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler, - void *handlerArg) -{ - spi.startTransfer((const uint8_t *)txBuffer, txSize, NULL, 0, doneHandler, handlerArg); -} - ST7735::ST7735(ScreenIO &io, Pin &cs, Pin &dc) : io(io), cs(cs), dc(dc), work(NULL) { double16 = false; diff --git a/source/drivers/ScreenIO.cpp b/source/drivers/ScreenIO.cpp new file mode 100644 index 00000000..ddda0d90 --- /dev/null +++ b/source/drivers/ScreenIO.cpp @@ -0,0 +1,19 @@ +#include "SPIScreenIO.h" + +namespace codal +{ + +SPIScreenIO::SPIScreenIO(SPI &spi) : spi(spi) {} + +void SPIScreenIO::send(const void *txBuffer, uint32_t txSize) +{ + spi.transfer((const uint8_t *)txBuffer, txSize, NULL, 0); +} + +void SPIScreenIO::startSend(const void *txBuffer, uint32_t txSize, PVoidCallback doneHandler, + void *handlerArg) +{ + spi.startTransfer((const uint8_t *)txBuffer, txSize, NULL, 0, doneHandler, handlerArg); +} + +} // namespace codal \ No newline at end of file