From 0ef333164bb47956a5ac686f376583e865cedcc4 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 16 Nov 2022 11:24:33 +1100 Subject: Use underscores instead of dashes in filenames consistently --- src/drivers/CMakeLists.txt | 4 +- src/drivers/dac.cpp | 2 +- src/drivers/display-init.cpp | 103 --------------- src/drivers/display.cpp | 2 +- src/drivers/display_init.cpp | 103 +++++++++++++++ src/drivers/gpio-expander.cpp | 87 ------------- src/drivers/gpio_expander.cpp | 87 +++++++++++++ src/drivers/include/dac.hpp | 2 +- src/drivers/include/display-init.hpp | 81 ------------ src/drivers/include/display.hpp | 4 +- src/drivers/include/display_init.hpp | 81 ++++++++++++ src/drivers/include/gpio-expander.hpp | 208 ------------------------------- src/drivers/include/gpio_expander.hpp | 208 +++++++++++++++++++++++++++++++ src/drivers/include/i2s_audio_output.hpp | 2 +- src/drivers/include/storage.hpp | 2 +- src/drivers/storage.cpp | 2 +- src/drivers/test/test_dac.cpp | 2 +- src/drivers/test/test_gpio_expander.cpp | 2 +- src/drivers/test/test_storage.cpp | 2 +- src/main/main.cpp | 4 +- 20 files changed, 494 insertions(+), 494 deletions(-) delete mode 100644 src/drivers/display-init.cpp create mode 100644 src/drivers/display_init.cpp delete mode 100644 src/drivers/gpio-expander.cpp create mode 100644 src/drivers/gpio_expander.cpp delete mode 100644 src/drivers/include/display-init.hpp create mode 100644 src/drivers/include/display_init.hpp delete mode 100644 src/drivers/include/gpio-expander.hpp create mode 100644 src/drivers/include/gpio_expander.hpp diff --git a/src/drivers/CMakeLists.txt b/src/drivers/CMakeLists.txt index 899fcf79..42b774ba 100644 --- a/src/drivers/CMakeLists.txt +++ b/src/drivers/CMakeLists.txt @@ -1,6 +1,6 @@ idf_component_register( - SRCS "dac.cpp" "gpio-expander.cpp" "battery.cpp" "storage.cpp" "i2c.cpp" - "audio_playback.cpp" "i2s_audio_output.cpp" "display.cpp" "display-init.cpp" "spi.cpp" + SRCS "dac.cpp" "gpio_expander.cpp" "battery.cpp" "storage.cpp" "i2c.cpp" + "audio_playback.cpp" "i2s_audio_output.cpp" "display.cpp" "display_init.cpp" "spi.cpp" INCLUDE_DIRS "include" REQUIRES "esp_adc_cal" "fatfs" "audio_pipeline" "audio_stream" "result" "lvgl") target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS}) diff --git a/src/drivers/dac.cpp b/src/drivers/dac.cpp index ac5b5eaa..3b7011c4 100644 --- a/src/drivers/dac.cpp +++ b/src/drivers/dac.cpp @@ -1,6 +1,6 @@ #include "dac.hpp" -#include "gpio-expander.hpp" +#include "gpio_expander.hpp" #include "i2c.hpp" #include diff --git a/src/drivers/display-init.cpp b/src/drivers/display-init.cpp deleted file mode 100644 index b958505d..00000000 --- a/src/drivers/display-init.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include "display-init.hpp" - -namespace drivers { -namespace displays { - -/* Bit to use to signify we should delay after part of an init sequence */ -const uint8_t kDelayBit = 0x80; - -// ST7735 commands and general format from the Adafruit library for these -// displays. AFAICT it's the most complete implementation out there, and I -// really don't want to have to derive this from the datasheet myself. -// See https://github.com/adafruit/Adafruit-ST7735-Library/ - -// clang-format off -static const uint8_t kST7735RCommonHeader[]{ - 15, // 15 commands in list: - ST77XX_SWRESET, kDelayBit, // 1: Software reset, 0 args, w/delay - 150, // 150 ms delay - ST77XX_SLPOUT, kDelayBit, // 2: Out of sleep mode, 0 args, w/delay - 255, // 500 ms delay - ST7735_FRMCTR1, 3, // 3: Framerate ctrl - normal mode, 3 arg: - 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D) - ST7735_FRMCTR2, 3, // 4: Framerate ctrl - idle mode, 3 args: - 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D) - ST7735_FRMCTR3, 6, // 5: Framerate - partial mode, 6 args: - 0x01, 0x2C, 0x2D, // Dot inversion mode - 0x01, 0x2C, 0x2D, // Line inversion mode - ST7735_INVCTR, 1, // 6: Display inversion ctrl, 1 arg: - 0x07, // No inversion - ST7735_PWCTR1, 3, // 7: Power control, 3 args, no delay: - 0xA2, - 0x02, // -4.6V - 0x84, // AUTO mode - ST7735_PWCTR2, 1, // 8: Power control, 1 arg, no delay: - 0xC5, // VGH25=2.4C VGSEL=-10 VGH=3 * AVDD - ST7735_PWCTR3, 2, // 9: Power control, 2 args, no delay: - 0x0A, // Opamp current small - 0x00, // Boost frequency - ST7735_PWCTR4, 2, // 10: Power control, 2 args, no delay: - 0x8A, // BCLK/2, - 0x2A, // opamp current small & medium low - ST7735_PWCTR5, 2, // 11: Power control, 2 args, no delay: - 0x8A, 0xEE, - ST7735_VMCTR1, 1, // 12: Power control, 1 arg, no delay: - 0x0E, - ST77XX_INVOFF, 0, // 13: Don't invert display, no args - ST77XX_MADCTL, 1, // 14: Mem access ctl (directions), 1 arg: - 0xC8, // row/col addr, bottom-top refresh - ST77XX_COLMOD, 1, // 15: set color mode, 1 arg, no delay: - 0x05 -}; - -// Commands to include for the variant of the panel that has a green pull tab on -// the screen protector. -static const uint8_t kST7735RCommonGreen[]{ - 2, // 2 commands in list: - ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay: - 0x00, 0x02, // XSTART = 0 - 0x00, 0x7F+0x02, // XEND = 127 - ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay: - 0x00, 0x01, // XSTART = 0 - 0x00, 0x9F+0x01}; - -// Commands to include for the variant of the panel that has a red pull tab on -// the screen protector. -static const uint8_t kST7735RCommonRed[]{ - 3, // 2 commands in list: - ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay: - 0x00, 0x00, // XSTART = 0 - 0x00, 0x7F, // XEND = 127 - ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay: - 0x00, 0x00, // XSTART = 0 - 0x00, 0x9F, - ST77XX_MADCTL, 1, - 0xC0, -}; - -static const uint8_t kST7735RCommonFooter[]{ - 4, // 4 commands in list: - ST7735_GMCTRP1, 16 , // 1: Gamma Adjustments (pos. polarity), 16 args + delay: - 0x02, 0x1c, 0x07, 0x12, // (Not entirely necessary, but provides - 0x37, 0x32, 0x29, 0x2d, // accurate colors) - 0x29, 0x25, 0x2B, 0x39, - 0x00, 0x01, 0x03, 0x10, - ST7735_GMCTRN1, 16 , // 2: Gamma Adjustments (neg. polarity), 16 args + delay: - 0x03, 0x1d, 0x07, 0x06, // (Not entirely necessary, but provides - 0x2E, 0x2C, 0x29, 0x2D, // accurate colors) - 0x2E, 0x2E, 0x37, 0x3F, - 0x00, 0x00, 0x02, 0x10, - ST77XX_NORON, kDelayBit, // 3: Normal display on, no args, w/delay - 10, // 10 ms delay - ST77XX_DISPON, kDelayBit, // 4: Main screen turn on, no args w/delay - 100 -}; -// clang-format on - -const InitialisationData kST7735R = { - .num_sequences = 3, - .sequences = {kST7735RCommonHeader, kST7735RCommonRed, - kST7735RCommonFooter}}; - -} // namespace displays -} // namespace drivers diff --git a/src/drivers/display.cpp b/src/drivers/display.cpp index dd9005cb..f5f926bf 100644 --- a/src/drivers/display.cpp +++ b/src/drivers/display.cpp @@ -5,7 +5,7 @@ #include #include #include "assert.h" -#include "display-init.hpp" +#include "display_init.hpp" #include "driver/gpio.h" #include "driver/spi_master.h" #include "esp_attr.h" diff --git a/src/drivers/display_init.cpp b/src/drivers/display_init.cpp new file mode 100644 index 00000000..e66d6a47 --- /dev/null +++ b/src/drivers/display_init.cpp @@ -0,0 +1,103 @@ +#include "display_init.hpp" + +namespace drivers { +namespace displays { + +/* Bit to use to signify we should delay after part of an init sequence */ +const uint8_t kDelayBit = 0x80; + +// ST7735 commands and general format from the Adafruit library for these +// displays. AFAICT it's the most complete implementation out there, and I +// really don't want to have to derive this from the datasheet myself. +// See https://github.com/adafruit/Adafruit-ST7735-Library/ + +// clang-format off +static const uint8_t kST7735RCommonHeader[]{ + 15, // 15 commands in list: + ST77XX_SWRESET, kDelayBit, // 1: Software reset, 0 args, w/delay + 150, // 150 ms delay + ST77XX_SLPOUT, kDelayBit, // 2: Out of sleep mode, 0 args, w/delay + 255, // 500 ms delay + ST7735_FRMCTR1, 3, // 3: Framerate ctrl - normal mode, 3 arg: + 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D) + ST7735_FRMCTR2, 3, // 4: Framerate ctrl - idle mode, 3 args: + 0x01, 0x2C, 0x2D, // Rate = fosc/(1x2+40) * (LINE+2C+2D) + ST7735_FRMCTR3, 6, // 5: Framerate - partial mode, 6 args: + 0x01, 0x2C, 0x2D, // Dot inversion mode + 0x01, 0x2C, 0x2D, // Line inversion mode + ST7735_INVCTR, 1, // 6: Display inversion ctrl, 1 arg: + 0x07, // No inversion + ST7735_PWCTR1, 3, // 7: Power control, 3 args, no delay: + 0xA2, + 0x02, // -4.6V + 0x84, // AUTO mode + ST7735_PWCTR2, 1, // 8: Power control, 1 arg, no delay: + 0xC5, // VGH25=2.4C VGSEL=-10 VGH=3 * AVDD + ST7735_PWCTR3, 2, // 9: Power control, 2 args, no delay: + 0x0A, // Opamp current small + 0x00, // Boost frequency + ST7735_PWCTR4, 2, // 10: Power control, 2 args, no delay: + 0x8A, // BCLK/2, + 0x2A, // opamp current small & medium low + ST7735_PWCTR5, 2, // 11: Power control, 2 args, no delay: + 0x8A, 0xEE, + ST7735_VMCTR1, 1, // 12: Power control, 1 arg, no delay: + 0x0E, + ST77XX_INVOFF, 0, // 13: Don't invert display, no args + ST77XX_MADCTL, 1, // 14: Mem access ctl (directions), 1 arg: + 0xC8, // row/col addr, bottom-top refresh + ST77XX_COLMOD, 1, // 15: set color mode, 1 arg, no delay: + 0x05 +}; + +// Commands to include for the variant of the panel that has a green pull tab on +// the screen protector. +static const uint8_t kST7735RCommonGreen[]{ + 2, // 2 commands in list: + ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay: + 0x00, 0x02, // XSTART = 0 + 0x00, 0x7F+0x02, // XEND = 127 + ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay: + 0x00, 0x01, // XSTART = 0 + 0x00, 0x9F+0x01}; + +// Commands to include for the variant of the panel that has a red pull tab on +// the screen protector. +static const uint8_t kST7735RCommonRed[]{ + 3, // 2 commands in list: + ST77XX_CASET, 4, // 1: Column addr set, 4 args, no delay: + 0x00, 0x00, // XSTART = 0 + 0x00, 0x7F, // XEND = 127 + ST77XX_RASET, 4, // 2: Row addr set, 4 args, no delay: + 0x00, 0x00, // XSTART = 0 + 0x00, 0x9F, + ST77XX_MADCTL, 1, + 0xC0, +}; + +static const uint8_t kST7735RCommonFooter[]{ + 4, // 4 commands in list: + ST7735_GMCTRP1, 16 , // 1: Gamma Adjustments (pos. polarity), 16 args + delay: + 0x02, 0x1c, 0x07, 0x12, // (Not entirely necessary, but provides + 0x37, 0x32, 0x29, 0x2d, // accurate colors) + 0x29, 0x25, 0x2B, 0x39, + 0x00, 0x01, 0x03, 0x10, + ST7735_GMCTRN1, 16 , // 2: Gamma Adjustments (neg. polarity), 16 args + delay: + 0x03, 0x1d, 0x07, 0x06, // (Not entirely necessary, but provides + 0x2E, 0x2C, 0x29, 0x2D, // accurate colors) + 0x2E, 0x2E, 0x37, 0x3F, + 0x00, 0x00, 0x02, 0x10, + ST77XX_NORON, kDelayBit, // 3: Normal display on, no args, w/delay + 10, // 10 ms delay + ST77XX_DISPON, kDelayBit, // 4: Main screen turn on, no args w/delay + 100 +}; +// clang-format on + +const InitialisationData kST7735R = { + .num_sequences = 3, + .sequences = {kST7735RCommonHeader, kST7735RCommonRed, + kST7735RCommonFooter}}; + +} // namespace displays +} // namespace drivers diff --git a/src/drivers/gpio-expander.cpp b/src/drivers/gpio-expander.cpp deleted file mode 100644 index 8b2e527e..00000000 --- a/src/drivers/gpio-expander.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "gpio-expander.hpp" - -#include "i2c.hpp" - -#include - -namespace drivers { - -GpioExpander::GpioExpander() { - ports_ = pack(kPortADefault, kPortBDefault); - // Read and write initial values on initialisation so that we do not have a - // strange partially-initialised state. - // TODO: log or abort if these error; it's really bad! - Write(); - Read(); -} - -GpioExpander::~GpioExpander() {} - -void GpioExpander::with(std::function f) { - f(*this); - Write(); -} - -esp_err_t GpioExpander::Write() { - i2c_cmd_handle_t handle = i2c_cmd_link_create(); - if (handle == NULL) { - return ESP_ERR_NO_MEM; - } - - std::pair ports_ab = unpack(ports()); - - I2CTransaction transaction; - transaction.start() - .write_addr(kPca8575Address, I2C_MASTER_WRITE) - .write_ack(ports_ab.first, ports_ab.second) - .stop(); - - return transaction.Execute(); -} - -esp_err_t GpioExpander::Read() { - uint8_t input_a, input_b; - - I2CTransaction transaction; - transaction.start() - .write_addr(kPca8575Address, I2C_MASTER_READ) - .read(&input_a, I2C_MASTER_ACK) - .read(&input_b, I2C_MASTER_LAST_NACK) - .stop(); - - esp_err_t ret = transaction.Execute(); - inputs_ = pack(input_a, input_b); - return ret; -} - -void GpioExpander::set_pin(ChipSelect cs, bool value) { - set_pin((Pin)cs, value); -} - -void GpioExpander::set_pin(Pin pin, bool value) { - if (value) { - ports_ |= (1 << pin); - } else { - ports_ &= ~(1 << pin); - } -} - -bool GpioExpander::get_input(Pin pin) const { - return (inputs_ & (1 << pin)) > 0; -} - -GpioExpander::SpiLock GpioExpander::AcquireSpiBus(ChipSelect cs) { - // TODO: also spi_device_acquire_bus? - return SpiLock(*this, cs); -} - -GpioExpander::SpiLock::SpiLock(GpioExpander& gpio, ChipSelect cs) - : lock_(gpio.cs_mutex_), gpio_(gpio), cs_(cs) { - gpio_.with([&](auto& expander) { expander.set_pin(cs_, 0); }); -} - -GpioExpander::SpiLock::~SpiLock() { - gpio_.with([&](auto& expander) { expander.set_pin(cs_, 1); }); -} - -} // namespace drivers diff --git a/src/drivers/gpio_expander.cpp b/src/drivers/gpio_expander.cpp new file mode 100644 index 00000000..02bd73c3 --- /dev/null +++ b/src/drivers/gpio_expander.cpp @@ -0,0 +1,87 @@ +#include "gpio_expander.hpp" + +#include "i2c.hpp" + +#include + +namespace drivers { + +GpioExpander::GpioExpander() { + ports_ = pack(kPortADefault, kPortBDefault); + // Read and write initial values on initialisation so that we do not have a + // strange partially-initialised state. + // TODO: log or abort if these error; it's really bad! + Write(); + Read(); +} + +GpioExpander::~GpioExpander() {} + +void GpioExpander::with(std::function f) { + f(*this); + Write(); +} + +esp_err_t GpioExpander::Write() { + i2c_cmd_handle_t handle = i2c_cmd_link_create(); + if (handle == NULL) { + return ESP_ERR_NO_MEM; + } + + std::pair ports_ab = unpack(ports()); + + I2CTransaction transaction; + transaction.start() + .write_addr(kPca8575Address, I2C_MASTER_WRITE) + .write_ack(ports_ab.first, ports_ab.second) + .stop(); + + return transaction.Execute(); +} + +esp_err_t GpioExpander::Read() { + uint8_t input_a, input_b; + + I2CTransaction transaction; + transaction.start() + .write_addr(kPca8575Address, I2C_MASTER_READ) + .read(&input_a, I2C_MASTER_ACK) + .read(&input_b, I2C_MASTER_LAST_NACK) + .stop(); + + esp_err_t ret = transaction.Execute(); + inputs_ = pack(input_a, input_b); + return ret; +} + +void GpioExpander::set_pin(ChipSelect cs, bool value) { + set_pin((Pin)cs, value); +} + +void GpioExpander::set_pin(Pin pin, bool value) { + if (value) { + ports_ |= (1 << pin); + } else { + ports_ &= ~(1 << pin); + } +} + +bool GpioExpander::get_input(Pin pin) const { + return (inputs_ & (1 << pin)) > 0; +} + +GpioExpander::SpiLock GpioExpander::AcquireSpiBus(ChipSelect cs) { + // TODO: also spi_device_acquire_bus? + return SpiLock(*this, cs); +} + +GpioExpander::SpiLock::SpiLock(GpioExpander& gpio, ChipSelect cs) + : lock_(gpio.cs_mutex_), gpio_(gpio), cs_(cs) { + gpio_.with([&](auto& expander) { expander.set_pin(cs_, 0); }); +} + +GpioExpander::SpiLock::~SpiLock() { + gpio_.with([&](auto& expander) { expander.set_pin(cs_, 1); }); +} + +} // namespace drivers diff --git a/src/drivers/include/dac.hpp b/src/drivers/include/dac.hpp index b4e95ad4..8c4b6419 100644 --- a/src/drivers/include/dac.hpp +++ b/src/drivers/include/dac.hpp @@ -1,6 +1,6 @@ #pragma once -#include "gpio-expander.hpp" +#include "gpio_expander.hpp" #include #include diff --git a/src/drivers/include/display-init.hpp b/src/drivers/include/display-init.hpp deleted file mode 100644 index f7d287f7..00000000 --- a/src/drivers/include/display-init.hpp +++ /dev/null @@ -1,81 +0,0 @@ -#pragma once - -#include - -namespace drivers { -namespace displays { - -extern const uint8_t kDelayBit; - -struct InitialisationData { - uint8_t num_sequences; - const uint8_t* sequences[4]; -}; - -extern const InitialisationData kST7735R; - -/* - * Valid command bytes that can be sent to ST77XX displays, as well as commands - * for more specific variants. - */ -enum StCommands { - ST77XX_NOP = 0x00, - ST77XX_SWRESET = 0x01, - ST77XX_RDDID = 0x04, - ST77XX_RDDST = 0x09, - - ST77XX_SLPIN = 0x10, - ST77XX_SLPOUT = 0x11, - ST77XX_PTLON = 0x12, - ST77XX_NORON = 0x13, - - ST77XX_INVOFF = 0x20, - ST77XX_INVON = 0x21, - ST77XX_DISPOFF = 0x28, - ST77XX_DISPON = 0x29, - ST77XX_CASET = 0x2A, - ST77XX_RASET = 0x2B, - ST77XX_RAMWR = 0x2C, - ST77XX_RAMRD = 0x2E, - - ST77XX_PTLAR = 0x30, - ST77XX_TEOFF = 0x34, - ST77XX_TEON = 0x35, - ST77XX_MADCTL = 0x36, - ST77XX_COLMOD = 0x3A, - - ST77XX_MADCTL_MY = 0x80, - ST77XX_MADCTL_MX = 0x40, - ST77XX_MADCTL_MV = 0x20, - ST77XX_MADCTL_ML = 0x10, - ST77XX_MADCTL_RGB = 0x00, - - ST77XX_RDID1 = 0xDA, - ST77XX_RDID2 = 0xDB, - ST77XX_RDID3 = 0xDC, - ST77XX_RDID4 = 0xDD, - - ST7735_MADCTL_BGR = 0x08, - ST7735_MADCTL_MH = 0x04, - - ST7735_FRMCTR1 = 0xB1, - ST7735_FRMCTR2 = 0xB2, - ST7735_FRMCTR3 = 0xB3, - ST7735_INVCTR = 0xB4, - ST7735_DISSET5 = 0xB6, - - ST7735_PWCTR1 = 0xC0, - ST7735_PWCTR2 = 0xC1, - ST7735_PWCTR3 = 0xC2, - ST7735_PWCTR4 = 0xC3, - ST7735_PWCTR5 = 0xC4, - ST7735_VMCTR1 = 0xC5, - - ST7735_PWCTR6 = 0xFC, - - ST7735_GMCTRP1 = 0xE0, - ST7735_GMCTRN1 = 0xE1, -}; - -} // namespace displays -} // namespace drivers diff --git a/src/drivers/include/display.hpp b/src/drivers/include/display.hpp index 6caf1571..958218ca 100644 --- a/src/drivers/include/display.hpp +++ b/src/drivers/include/display.hpp @@ -1,9 +1,9 @@ #pragma once #include -#include "display-init.hpp" +#include "display_init.hpp" #include "driver/spi_master.h" -#include "gpio-expander.hpp" +#include "gpio_expander.hpp" #include "lvgl/lvgl.h" #include "result.hpp" diff --git a/src/drivers/include/display_init.hpp b/src/drivers/include/display_init.hpp new file mode 100644 index 00000000..f7d287f7 --- /dev/null +++ b/src/drivers/include/display_init.hpp @@ -0,0 +1,81 @@ +#pragma once + +#include + +namespace drivers { +namespace displays { + +extern const uint8_t kDelayBit; + +struct InitialisationData { + uint8_t num_sequences; + const uint8_t* sequences[4]; +}; + +extern const InitialisationData kST7735R; + +/* + * Valid command bytes that can be sent to ST77XX displays, as well as commands + * for more specific variants. + */ +enum StCommands { + ST77XX_NOP = 0x00, + ST77XX_SWRESET = 0x01, + ST77XX_RDDID = 0x04, + ST77XX_RDDST = 0x09, + + ST77XX_SLPIN = 0x10, + ST77XX_SLPOUT = 0x11, + ST77XX_PTLON = 0x12, + ST77XX_NORON = 0x13, + + ST77XX_INVOFF = 0x20, + ST77XX_INVON = 0x21, + ST77XX_DISPOFF = 0x28, + ST77XX_DISPON = 0x29, + ST77XX_CASET = 0x2A, + ST77XX_RASET = 0x2B, + ST77XX_RAMWR = 0x2C, + ST77XX_RAMRD = 0x2E, + + ST77XX_PTLAR = 0x30, + ST77XX_TEOFF = 0x34, + ST77XX_TEON = 0x35, + ST77XX_MADCTL = 0x36, + ST77XX_COLMOD = 0x3A, + + ST77XX_MADCTL_MY = 0x80, + ST77XX_MADCTL_MX = 0x40, + ST77XX_MADCTL_MV = 0x20, + ST77XX_MADCTL_ML = 0x10, + ST77XX_MADCTL_RGB = 0x00, + + ST77XX_RDID1 = 0xDA, + ST77XX_RDID2 = 0xDB, + ST77XX_RDID3 = 0xDC, + ST77XX_RDID4 = 0xDD, + + ST7735_MADCTL_BGR = 0x08, + ST7735_MADCTL_MH = 0x04, + + ST7735_FRMCTR1 = 0xB1, + ST7735_FRMCTR2 = 0xB2, + ST7735_FRMCTR3 = 0xB3, + ST7735_INVCTR = 0xB4, + ST7735_DISSET5 = 0xB6, + + ST7735_PWCTR1 = 0xC0, + ST7735_PWCTR2 = 0xC1, + ST7735_PWCTR3 = 0xC2, + ST7735_PWCTR4 = 0xC3, + ST7735_PWCTR5 = 0xC4, + ST7735_VMCTR1 = 0xC5, + + ST7735_PWCTR6 = 0xFC, + + ST7735_GMCTRP1 = 0xE0, + ST7735_GMCTRN1 = 0xE1, +}; + +} // namespace displays +} // namespace drivers diff --git a/src/drivers/include/gpio-expander.hpp b/src/drivers/include/gpio-expander.hpp deleted file mode 100644 index 7ae0f107..00000000 --- a/src/drivers/include/gpio-expander.hpp +++ /dev/null @@ -1,208 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -#include "driver/i2c.h" -#include "esp_check.h" -#include "esp_err.h" -#include "esp_log.h" -#include "freertos/FreeRTOS.h" - -namespace drivers { - -/** - * Wrapper for interfacing with the PCA8575 GPIO expander. Includes basic - * low-level pin setting methods, as well as higher level convenience functions - * for reading, writing, and atomically interacting with the SPI chip select - * pins. - * - * Each method of this class can be called safely from any thread, and all - * updates are guaranteed to be atomic. Any access to chip select related pins - * should be done whilst holding `cs_lock` (preferably via the helper methods). - */ -class GpioExpander { - public: - GpioExpander(); - ~GpioExpander(); - - static const uint8_t kPca8575Address = 0x20; - static const uint8_t kPca8575Timeout = 100 / portTICK_RATE_MS; - - // Port A: - // 0 - audio power enable - // 1 - usb interface power enable - // 2 - display power enable - // 3 - sd card power enable - // 4 - charge power ok (active low) - // 5 - sd mux switch - // 6 - sd chip select - // 7 - display chip select - // All power switches low, chip selects high, active-low charge power high - static const uint8_t kPortADefault = 0b11010001; - - // Port B: - // 0 - 3.5mm jack detect (active low) - // 1 - dac soft mute switch - // 2 - GPIO - // 3 - GPIO - // 4 - GPIO - // 5 - GPIO - // 6 - GPIO - // 7 - GPIO - // DAC mute output low, everything else is active-low inputs. - static const uint8_t kPortBDefault = 0b11111111; - - /* - * Convenience mehod for packing the port a and b bytes into a single 16 bit - * value. - */ - static uint16_t pack(uint8_t a, uint8_t b) { return ((uint16_t)b) << 8 | a; } - - /* - * Convenience mehod for unpacking the result of `pack` back into two single - * byte port datas. - */ - static std::pair unpack(uint16_t ba) { - return std::pair((uint8_t)ba, (uint8_t)(ba >> 8)); - } - - /* - * Convenience function for running some arbitrary pin writing code, then - * flushing a `Write()` to the expander. Example usage: - * - * ``` - * gpio_.with([&](auto& gpio) { - * gpio.set_pin(AUDIO_POWER_ENABLE, true); - * }); - * ``` - */ - void with(std::function f); - - /** - * Sets the ports on the GPIO expander to the values currently represented - * in `ports`. - */ - esp_err_t Write(void); - - /** - * Reads from the GPIO expander, populating `inputs` with the most recent - * values. - */ - esp_err_t Read(void); - - /* Maps each pin of the expander to its number in a `pack`ed uint16. */ - enum Pin { - // Port A - AUDIO_POWER_ENABLE = 0, - USB_INTERFACE_POWER_ENABLE = 1, - DISPLAY_POWER_ENABLE = 2, - SD_CARD_POWER_ENABLE = 3, - CHARGE_POWER_OK = 4, // Active-low input - SD_MUX_SWITCH = 5, - SD_CHIP_SELECT = 6, - DISPLAY_CHIP_SELECT = 7, - - // Port B - PHONE_DETECT = 8, // Active-high input - DAC_MUTE = 9, - GPIO_1 = 10, - GPIO_2 = 11, - GPIO_3 = 12, - GPIO_4 = 13, - GPIO_5 = 14, - GPIO_6 = 15, - }; - - /* Pins whose access should be guarded by `cs_lock`. */ - enum ChipSelect { - SD_CARD = SD_CHIP_SELECT, - DISPLAY = DISPLAY_CHIP_SELECT, - }; - - /* Nicer value names for use with the SD_MUX_SWITCH pin. */ - enum SdController { - SD_MUX_ESP = 0, - SD_MUX_USB = 1, - }; - - /** - * Returns the current driven status of each of the ports. The first byte is - * port a, and the second byte is port b. - */ - std::atomic& ports() { return ports_; } - - /* - * Sets a single specific pin to the given value. `true` corresponds to - * HIGH, and `false` corresponds to LOW. - * - * Calls to this method will be buffered in memory until a call to `Write()` - * is made. - */ - void set_pin(Pin pin, bool value); - void set_pin(ChipSelect cs, bool value); - - /** - * Returns the input status of each of the ports. The first byte is port a, - * and the second byte is port b. - */ - const std::atomic& inputs() const { return inputs_; } - - /* Returns the most recently cached value of the given pin. Only valid for - * pins used as inputs; to check what value we're driving a pin, use - * `ports()`. - */ - bool get_input(Pin pin) const; - - /* Returns the mutex that must be held whilst pulling a CS pin low. */ - std::mutex& cs_mutex() { return cs_mutex_; } - - /* - * Helper class containing an active `cs_mutex` lock. When an instance of - * this class is destroyed (usually by falling out of scope), the associated - * CS pin will be driven high before the lock is released. - */ - class SpiLock { - public: - SpiLock(GpioExpander& gpio, ChipSelect cs); - ~SpiLock(); - - SpiLock(const SpiLock&) = delete; - - private: - std::scoped_lock lock_; - GpioExpander& gpio_; - ChipSelect cs_; - }; - - /* - * Pulls the given CS pin low to signal that we are about to communicate - * with a particular device, after acquiring a lock on `cs_mutex`. The - * recommended way to safely interact with devices on the SPI bus is to have - * a self-contained block like so: - * - * ``` - * { - * auto lock = AcquireSpiBus(WHATEVER); - * // Do some cool things here. - * } - * ``` - */ - SpiLock AcquireSpiBus(ChipSelect cs); - - // Not copyable or movable. There should usually only ever be once instance - // of this class, and that instance will likely have a static lifetime. - GpioExpander(const GpioExpander&) = delete; - GpioExpander& operator=(const GpioExpander&) = delete; - - private: - std::mutex cs_mutex_; - std::atomic ports_; - std::atomic inputs_; -}; - -} // namespace drivers diff --git a/src/drivers/include/gpio_expander.hpp b/src/drivers/include/gpio_expander.hpp new file mode 100644 index 00000000..7ae0f107 --- /dev/null +++ b/src/drivers/include/gpio_expander.hpp @@ -0,0 +1,208 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include "driver/i2c.h" +#include "esp_check.h" +#include "esp_err.h" +#include "esp_log.h" +#include "freertos/FreeRTOS.h" + +namespace drivers { + +/** + * Wrapper for interfacing with the PCA8575 GPIO expander. Includes basic + * low-level pin setting methods, as well as higher level convenience functions + * for reading, writing, and atomically interacting with the SPI chip select + * pins. + * + * Each method of this class can be called safely from any thread, and all + * updates are guaranteed to be atomic. Any access to chip select related pins + * should be done whilst holding `cs_lock` (preferably via the helper methods). + */ +class GpioExpander { + public: + GpioExpander(); + ~GpioExpander(); + + static const uint8_t kPca8575Address = 0x20; + static const uint8_t kPca8575Timeout = 100 / portTICK_RATE_MS; + + // Port A: + // 0 - audio power enable + // 1 - usb interface power enable + // 2 - display power enable + // 3 - sd card power enable + // 4 - charge power ok (active low) + // 5 - sd mux switch + // 6 - sd chip select + // 7 - display chip select + // All power switches low, chip selects high, active-low charge power high + static const uint8_t kPortADefault = 0b11010001; + + // Port B: + // 0 - 3.5mm jack detect (active low) + // 1 - dac soft mute switch + // 2 - GPIO + // 3 - GPIO + // 4 - GPIO + // 5 - GPIO + // 6 - GPIO + // 7 - GPIO + // DAC mute output low, everything else is active-low inputs. + static const uint8_t kPortBDefault = 0b11111111; + + /* + * Convenience mehod for packing the port a and b bytes into a single 16 bit + * value. + */ + static uint16_t pack(uint8_t a, uint8_t b) { return ((uint16_t)b) << 8 | a; } + + /* + * Convenience mehod for unpacking the result of `pack` back into two single + * byte port datas. + */ + static std::pair unpack(uint16_t ba) { + return std::pair((uint8_t)ba, (uint8_t)(ba >> 8)); + } + + /* + * Convenience function for running some arbitrary pin writing code, then + * flushing a `Write()` to the expander. Example usage: + * + * ``` + * gpio_.with([&](auto& gpio) { + * gpio.set_pin(AUDIO_POWER_ENABLE, true); + * }); + * ``` + */ + void with(std::function f); + + /** + * Sets the ports on the GPIO expander to the values currently represented + * in `ports`. + */ + esp_err_t Write(void); + + /** + * Reads from the GPIO expander, populating `inputs` with the most recent + * values. + */ + esp_err_t Read(void); + + /* Maps each pin of the expander to its number in a `pack`ed uint16. */ + enum Pin { + // Port A + AUDIO_POWER_ENABLE = 0, + USB_INTERFACE_POWER_ENABLE = 1, + DISPLAY_POWER_ENABLE = 2, + SD_CARD_POWER_ENABLE = 3, + CHARGE_POWER_OK = 4, // Active-low input + SD_MUX_SWITCH = 5, + SD_CHIP_SELECT = 6, + DISPLAY_CHIP_SELECT = 7, + + // Port B + PHONE_DETECT = 8, // Active-high input + DAC_MUTE = 9, + GPIO_1 = 10, + GPIO_2 = 11, + GPIO_3 = 12, + GPIO_4 = 13, + GPIO_5 = 14, + GPIO_6 = 15, + }; + + /* Pins whose access should be guarded by `cs_lock`. */ + enum ChipSelect { + SD_CARD = SD_CHIP_SELECT, + DISPLAY = DISPLAY_CHIP_SELECT, + }; + + /* Nicer value names for use with the SD_MUX_SWITCH pin. */ + enum SdController { + SD_MUX_ESP = 0, + SD_MUX_USB = 1, + }; + + /** + * Returns the current driven status of each of the ports. The first byte is + * port a, and the second byte is port b. + */ + std::atomic& ports() { return ports_; } + + /* + * Sets a single specific pin to the given value. `true` corresponds to + * HIGH, and `false` corresponds to LOW. + * + * Calls to this method will be buffered in memory until a call to `Write()` + * is made. + */ + void set_pin(Pin pin, bool value); + void set_pin(ChipSelect cs, bool value); + + /** + * Returns the input status of each of the ports. The first byte is port a, + * and the second byte is port b. + */ + const std::atomic& inputs() const { return inputs_; } + + /* Returns the most recently cached value of the given pin. Only valid for + * pins used as inputs; to check what value we're driving a pin, use + * `ports()`. + */ + bool get_input(Pin pin) const; + + /* Returns the mutex that must be held whilst pulling a CS pin low. */ + std::mutex& cs_mutex() { return cs_mutex_; } + + /* + * Helper class containing an active `cs_mutex` lock. When an instance of + * this class is destroyed (usually by falling out of scope), the associated + * CS pin will be driven high before the lock is released. + */ + class SpiLock { + public: + SpiLock(GpioExpander& gpio, ChipSelect cs); + ~SpiLock(); + + SpiLock(const SpiLock&) = delete; + + private: + std::scoped_lock lock_; + GpioExpander& gpio_; + ChipSelect cs_; + }; + + /* + * Pulls the given CS pin low to signal that we are about to communicate + * with a particular device, after acquiring a lock on `cs_mutex`. The + * recommended way to safely interact with devices on the SPI bus is to have + * a self-contained block like so: + * + * ``` + * { + * auto lock = AcquireSpiBus(WHATEVER); + * // Do some cool things here. + * } + * ``` + */ + SpiLock AcquireSpiBus(ChipSelect cs); + + // Not copyable or movable. There should usually only ever be once instance + // of this class, and that instance will likely have a static lifetime. + GpioExpander(const GpioExpander&) = delete; + GpioExpander& operator=(const GpioExpander&) = delete; + + private: + std::mutex cs_mutex_; + std::atomic ports_; + std::atomic inputs_; +}; + +} // namespace drivers diff --git a/src/drivers/include/i2s_audio_output.hpp b/src/drivers/include/i2s_audio_output.hpp index d4f6dae1..dabf1ff0 100644 --- a/src/drivers/include/i2s_audio_output.hpp +++ b/src/drivers/include/i2s_audio_output.hpp @@ -2,7 +2,7 @@ #include "audio_output.hpp" #include "dac.hpp" -#include "gpio-expander.hpp" +#include "gpio_expander.hpp" #include "result.hpp" #include diff --git a/src/drivers/include/storage.hpp b/src/drivers/include/storage.hpp index 3aa2cb92..3dec16a5 100644 --- a/src/drivers/include/storage.hpp +++ b/src/drivers/include/storage.hpp @@ -1,6 +1,6 @@ #pragma once -#include "gpio-expander.hpp" +#include "gpio_expander.hpp" #include diff --git a/src/drivers/storage.cpp b/src/drivers/storage.cpp index 465c84ea..7840e6f4 100644 --- a/src/drivers/storage.cpp +++ b/src/drivers/storage.cpp @@ -1,6 +1,6 @@ #include "storage.hpp" -#include "gpio-expander.hpp" +#include "gpio_expander.hpp" #include #include diff --git a/src/drivers/test/test_dac.cpp b/src/drivers/test/test_dac.cpp index aa7a521c..84be09b9 100644 --- a/src/drivers/test/test_dac.cpp +++ b/src/drivers/test/test_dac.cpp @@ -1,6 +1,6 @@ #include #include "dac.hpp" -#include "gpio-expander.hpp" +#include "gpio_expander.hpp" #include "i2c.hpp" #include "i2c_fixture.hpp" diff --git a/src/drivers/test/test_gpio_expander.cpp b/src/drivers/test/test_gpio_expander.cpp index 901953d0..d0d31002 100644 --- a/src/drivers/test/test_gpio_expander.cpp +++ b/src/drivers/test/test_gpio_expander.cpp @@ -1,4 +1,4 @@ -#include "gpio-expander.hpp" +#include "gpio_expander.hpp" #include "i2c.hpp" #include "i2c_fixture.hpp" diff --git a/src/drivers/test/test_storage.cpp b/src/drivers/test/test_storage.cpp index a767a52e..49b3d097 100644 --- a/src/drivers/test/test_storage.cpp +++ b/src/drivers/test/test_storage.cpp @@ -3,7 +3,7 @@ #include #include -#include "gpio-expander.hpp" +#include "gpio_expander.hpp" #include "i2c.hpp" #include "i2c_fixture.hpp" #include "spi.hpp" diff --git a/src/main/main.cpp b/src/main/main.cpp index 26deef39..c79cc055 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -4,11 +4,11 @@ #include "core/lv_disp.h" #include "core/lv_obj_pos.h" #include "dac.hpp" -#include "display-init.hpp" +#include "display_init.hpp" #include "display.hpp" #include "esp_freertos_hooks.h" #include "freertos/portmacro.h" -#include "gpio-expander.hpp" +#include "gpio_expander.hpp" #include "i2c.hpp" #include "i2s_audio_output.hpp" #include "misc/lv_color.h" -- cgit v1.2.3