Add a common header for pins and settings

* Add a common/pimoroni.hpp to list default pins for various add-ons
* Move the BG SPI Slot enum here for safe keeping
* Switch all GPIO pin references to "uint" to match Pico SDK and bring back PIN_UNUSED as UINT_MAX
pull/129/head
Phil Howard 2021-04-20 10:54:10 +01:00
rodzic 76bdf3fb89
commit 3dfae2ed5c
18 zmienionych plików z 124 dodań i 120 usunięć

Wyświetl plik

@ -0,0 +1,35 @@
#pragma once
#include <stdint.h>
#include <climits>
#define PIMORONI_I2C_DEFAULT_INSTANCE i2c0
#define PIMORONI_SPI_DEFAULT_INSTANCE spi0
namespace pimoroni {
static const unsigned int PIN_UNUSED = UINT_MAX;
// I2C
static const unsigned int I2C_DEFAULT_SDA = 20;
static const unsigned int I2C_DEFAULT_SCL = 21;
static const unsigned int I2C_DEFAULT_INT = 22;
static const unsigned int I2C_BG_SDA = 4;
static const unsigned int I2C_BG_SCL = 5;
static const unsigned int I2C_BG_INT = 3;
// SPI
static const unsigned int SPI_DEFAULT_MOSI = 19;
static const unsigned int SPI_DEFAULT_MISO = 16;
static const unsigned int SPI_DEFAULT_SCK = 18;
static const unsigned int SPI_BG_FRONT_PWM = 20;
static const unsigned int SPI_BG_FRONT_CS = 17;
static const unsigned int SPI_BG_BACK_PWM = 21;
static const unsigned int SPI_BG_BACK_CS = 22;
enum BG_SPI_SLOT {
BG_SPI_FRONT,
BG_SPI_BACK
};
}

Wyświetl plik

@ -2,6 +2,7 @@
#include "hardware/i2c.h"
#include "hardware/gpio.h"
#include "common/pimoroni.hpp"
namespace pimoroni {
@ -110,9 +111,10 @@ namespace pimoroni {
// interface pins with our standard defaults where appropriate
int8_t address = DEFAULT_I2C_ADDRESS;
int8_t sda = DEFAULT_SDA_PIN;
int8_t scl = DEFAULT_SCL_PIN;
int8_t interrupt = DEFAULT_INT_PIN;
uint sda = I2C_DEFAULT_SDA;
uint scl = I2C_DEFAULT_SCL;
uint interrupt = I2C_DEFAULT_INT;
i2c_inst_t *i2c = PIMORONI_I2C_DEFAULT_INSTANCE;
//--------------------------------------------------
@ -121,7 +123,7 @@ namespace pimoroni {
public:
MSA301() {}
MSA301(i2c_inst_t *i2c, uint8_t sda, uint8_t scl, uint8_t interrupt) :
MSA301(i2c_inst_t *i2c, uint sda, uint scl, uint interrupt) :
i2c(i2c), sda(sda), scl(scl), interrupt(interrupt) {}

Wyświetl plik

@ -12,6 +12,7 @@ Distributed as-is; no warranty is given.
#include "hardware/i2c.h"
#include "hardware/gpio.h"
#include "common/pimoroni.hpp"
#include <stdio.h>
#include <stdlib.h>
@ -206,13 +207,13 @@ namespace pimoroni {
// Variables
//--------------------------------------------------
private:
i2c_inst_t *i2c = i2c0;
i2c_inst_t *i2c = PIMORONI_I2C_DEFAULT_INSTANCE;
// interface pins with our standard defaults where appropriate
int8_t address = DEFAULT_I2C_ADDRESS;
int8_t sda = DEFAULT_SDA_PIN;
int8_t scl = DEFAULT_SCL_PIN;
int8_t interrupt = DEFAULT_INT_PIN;
int8_t address = DEFAULT_I2C_ADDRESS;
uint sda = DEFAULT_SDA_PIN;
uint scl = DEFAULT_SCL_PIN;
uint interrupt = DEFAULT_INT_PIN;
uint8_t times[TIME_ARRAY_LENGTH];

Wyświetl plik

@ -2,6 +2,7 @@
#include "hardware/spi.h"
#include "hardware/gpio.h"
#include "../../common/pimoroni.hpp"
namespace pimoroni {
@ -20,17 +21,6 @@ namespace pimoroni {
static const uint8_t ROWS = 162;
static const uint8_t COLS = 132;
//--------------------------------------------------
// Enums
//--------------------------------------------------
public:
enum BG_SPI_SLOT {
BG_SPI_FRONT,
BG_SPI_BACK
};
//--------------------------------------------------
// Variables
//--------------------------------------------------

Wyświetl plik

@ -67,13 +67,13 @@ namespace pimoroni {
gpio_set_function(sck, GPIO_FUNC_SPI);
gpio_set_function(mosi, GPIO_FUNC_SPI);
if(miso != -1) {
if(miso != PIN_UNUSED) {
gpio_set_function(miso, GPIO_FUNC_SPI);
}
// if supported by the display then the vsync pin is
// toggled high during vertical blanking period
if(vsync != -1) {
if(vsync != PIN_UNUSED) {
gpio_set_function(vsync, GPIO_FUNC_SIO);
gpio_set_dir(vsync, GPIO_IN);
gpio_set_pulls(vsync, false, true);
@ -81,7 +81,7 @@ namespace pimoroni {
// if a backlight pin is provided then set it up for
// pwm control
if(bl != -1) {
if(bl != PIN_UNUSED) {
pwm_config cfg = pwm_get_default_config();
pwm_set_wrap(pwm_gpio_to_slice_num(bl), 65535);
pwm_init(pwm_gpio_to_slice_num(bl), &cfg, true);
@ -180,23 +180,23 @@ namespace pimoroni {
return spi;
}
int ST7789::get_cs() const {
uint ST7789::get_cs() const {
return cs;
}
int ST7789::get_dc() const {
uint ST7789::get_dc() const {
return dc;
}
int ST7789::get_sck() const {
uint ST7789::get_sck() const {
return sck;
}
int ST7789::get_mosi() const {
uint ST7789::get_mosi() const {
return mosi;
}
int ST7789::get_bl() const {
uint ST7789::get_bl() const {
return bl;
}

Wyświetl plik

@ -2,29 +2,12 @@
#include "hardware/spi.h"
#include "hardware/gpio.h"
#include "../../common/pimoroni.hpp"
namespace pimoroni {
class ST7789 {
//--------------------------------------------------
// Constants
//--------------------------------------------------
public:
static const uint8_t DEFAULT_CS_PIN = 17;
static const uint8_t DEFAULT_DC_PIN = 16;
static const uint8_t DEFAULT_SCK_PIN = 18;
static const uint8_t DEFAULT_MOSI_PIN = 19;
static const uint8_t DEFAULT_BL_PIN = 20;
//--------------------------------------------------
// Enums
//--------------------------------------------------
public:
enum BG_SPI_SLOT {
BG_SPI_FRONT,
BG_SPI_BACK
};
spi_inst_t *spi = PIMORONI_SPI_DEFAULT_INSTANCE;
//--------------------------------------------------
@ -35,42 +18,33 @@ namespace pimoroni {
uint16_t width;
uint16_t height;
uint16_t row_stride;
uint32_t dma_channel;
// interface pins with our standard defaults where appropriate
uint cs = SPI_BG_FRONT_CS;
uint dc = SPI_DEFAULT_MISO;
uint sck = SPI_DEFAULT_SCK;
uint mosi = SPI_DEFAULT_MOSI;
uint miso = PIN_UNUSED; // used as data/command
uint bl = SPI_BG_FRONT_PWM;
uint vsync = PIN_UNUSED; // only available on some products
uint32_t spi_baud = 16 * 1000 * 1000;
public:
// frame buffer where pixel data is stored
uint16_t *frame_buffer;
private:
spi_inst_t *spi = spi0;
uint32_t dma_channel;
// interface pins with our standard defaults where appropriate
int8_t cs = DEFAULT_CS_PIN;
int8_t dc = DEFAULT_DC_PIN;
int8_t sck = DEFAULT_SCK_PIN;
int8_t mosi = DEFAULT_MOSI_PIN;
int8_t miso = -1; // we generally don't use this pin
int8_t bl = DEFAULT_BL_PIN;
int8_t vsync = -1; // only available on some products
uint32_t spi_baud = 16 * 1000 * 1000;
//--------------------------------------------------
// Constructors/Destructor
//--------------------------------------------------
public:
ST7789(uint16_t width, uint16_t height, uint16_t *frame_buffer, BG_SPI_SLOT slot) :
width(width), height(height), frame_buffer(frame_buffer) {
switch(slot) {
case BG_SPI_FRONT:
cs = 17;
bl = 20;
cs = SPI_BG_FRONT_CS;
bl = SPI_BG_FRONT_PWM;
break;
case BG_SPI_BACK:
cs = 22;
bl = 21;
cs = SPI_BG_BACK_CS;
bl = SPI_BG_BACK_PWM;
break;
}
}
@ -80,9 +54,10 @@ namespace pimoroni {
ST7789(uint16_t width, uint16_t height, uint16_t *frame_buffer,
spi_inst_t *spi,
uint8_t cs, uint8_t dc, uint8_t sck, uint8_t mosi, uint8_t miso = -1, uint8_t bl = -1) :
width(width), height(height), frame_buffer(frame_buffer),
spi(spi), cs(cs), dc(dc), sck(sck), mosi(mosi), miso(miso), bl(bl) {}
uint cs, uint dc, uint sck, uint mosi, uint miso = PIN_UNUSED, uint bl = PIN_UNUSED) :
spi(spi),
width(width), height(height),
cs(cs), dc(dc), sck(sck), mosi(mosi), miso(miso), bl(bl), frame_buffer(frame_buffer) {}
//--------------------------------------------------
@ -92,11 +67,11 @@ namespace pimoroni {
void init(bool auto_init_sequence = true, bool round = false);
spi_inst_t* get_spi() const;
int get_cs() const;
int get_dc() const;
int get_sck() const;
int get_mosi() const;
int get_bl() const;
uint get_cs() const;
uint get_dc() const;
uint get_sck() const;
uint get_mosi() const;
uint get_bl() const;
void command(uint8_t command, size_t len = 0, const char *data = NULL);
void vsync_callback(gpio_irq_callback_t callback);

Wyświetl plik

@ -12,7 +12,7 @@ using namespace pimoroni;
uint16_t buffer[BreakoutRoundLCD::WIDTH * BreakoutRoundLCD::HEIGHT];
BreakoutRoundLCD display(buffer, ST7789::BG_SPI_FRONT);
BreakoutRoundLCD display(buffer, BG_SPI_FRONT);
constexpr float RADIUS = BreakoutRoundLCD::WIDTH / 2;

Wyświetl plik

@ -13,7 +13,7 @@ namespace pimoroni {
__fb = buf;
}
BreakoutColourLCD160x80::BreakoutColourLCD160x80(uint16_t *buf, ST7735::BG_SPI_SLOT slot)
BreakoutColourLCD160x80::BreakoutColourLCD160x80(uint16_t *buf, BG_SPI_SLOT slot)
: PicoGraphics(WIDTH, HEIGHT, buf), screen(WIDTH, HEIGHT, buf, slot) {
__fb = buf;
}

Wyświetl plik

@ -2,6 +2,7 @@
#include "drivers/st7735/st7735.hpp"
#include "libraries/pico_graphics/pico_graphics.hpp"
#include "common/pimoroni.hpp"
namespace pimoroni {
@ -31,7 +32,7 @@ namespace pimoroni {
BreakoutColourLCD160x80(uint16_t *buf);
BreakoutColourLCD160x80(uint16_t *buf, spi_inst_t *spi,
uint8_t cs, uint8_t dc, uint8_t sck, uint8_t mosi, uint8_t miso = PIN_UNUSED, uint8_t bl = PIN_UNUSED);
BreakoutColourLCD160x80(uint16_t *buf, ST7735::BG_SPI_SLOT slot);
BreakoutColourLCD160x80(uint16_t *buf, BG_SPI_SLOT slot);
//--------------------------------------------------

Wyświetl plik

@ -13,7 +13,7 @@ namespace pimoroni {
__fb = buf;
}
BreakoutColourLCD240x240::BreakoutColourLCD240x240(uint16_t *buf, ST7789::BG_SPI_SLOT slot)
BreakoutColourLCD240x240::BreakoutColourLCD240x240(uint16_t *buf, BG_SPI_SLOT slot)
: PicoGraphics(WIDTH, HEIGHT, buf), screen(WIDTH, HEIGHT, buf, slot) {
__fb = buf;
}

Wyświetl plik

@ -2,6 +2,7 @@
#include "drivers/st7789/st7789.hpp"
#include "libraries/pico_graphics/pico_graphics.hpp"
#include "common/pimoroni.hpp"
namespace pimoroni {
@ -31,7 +32,7 @@ namespace pimoroni {
BreakoutColourLCD240x240(uint16_t *buf);
BreakoutColourLCD240x240(uint16_t *buf, spi_inst_t *spi,
uint8_t cs, uint8_t dc, uint8_t sck, uint8_t mosi, uint8_t miso = PIN_UNUSED, uint8_t bl = PIN_UNUSED);
BreakoutColourLCD240x240(uint16_t *buf, ST7789::BG_SPI_SLOT slot);
BreakoutColourLCD240x240(uint16_t *buf, BG_SPI_SLOT slot);
//--------------------------------------------------

Wyświetl plik

@ -8,13 +8,12 @@ namespace pimoroni {
}
BreakoutRoundLCD::BreakoutRoundLCD(uint16_t *buf, spi_inst_t *spi,
uint8_t cs, uint8_t dc, uint8_t sck, uint8_t mosi, uint8_t miso, uint8_t bl)
uint cs, uint dc, uint sck, uint mosi, uint miso, uint bl)
: PicoGraphics(WIDTH, HEIGHT, buf), screen(WIDTH, HEIGHT, buf, spi, cs, dc, sck, mosi, miso, bl) {
__fb = buf;
}
BreakoutRoundLCD::BreakoutRoundLCD(uint16_t *buf, ST7789::BG_SPI_SLOT slot)
: PicoGraphics(WIDTH, HEIGHT, buf), screen(WIDTH, HEIGHT, buf, slot) {
BreakoutRoundLCD::BreakoutRoundLCD(uint16_t *buf, BG_SPI_SLOT slot) : PicoGraphics(WIDTH, HEIGHT, buf), screen(WIDTH, HEIGHT, buf, slot) {
__fb = buf;
}

Wyświetl plik

@ -31,8 +31,8 @@ namespace pimoroni {
public:
BreakoutRoundLCD(uint16_t *buf);
BreakoutRoundLCD(uint16_t *buf, spi_inst_t *spi,
uint8_t cs, uint8_t dc, uint8_t sck, uint8_t mosi, uint8_t miso = PIN_UNUSED, uint8_t bl = PIN_UNUSED);
BreakoutRoundLCD(uint16_t *buf, ST7789::BG_SPI_SLOT slot);
uint cs, uint dc, uint sck, uint mosi, uint miso = PIN_UNUSED, uint bl = PIN_UNUSED);
BreakoutRoundLCD(uint16_t *buf, BG_SPI_SLOT slot);
//--------------------------------------------------

Wyświetl plik

@ -95,7 +95,7 @@ namespace pimoroni {
}
}
void PicoExplorer::set_audio_pin(uint8_t pin) {
void PicoExplorer::set_audio_pin(uint pin) {
pwm_config tone_pwm_cfg = pwm_get_default_config();
// calculate the pwm wrap value for this frequency

Wyświetl plik

@ -9,10 +9,10 @@ namespace pimoroni {
public:
static const int WIDTH = 240;
static const int HEIGHT = 240;
static const uint8_t A = 12;
static const uint8_t B = 13;
static const uint8_t X = 14;
static const uint8_t Y = 15;
static const uint A = 12;
static const uint B = 13;
static const uint X = 14;
static const uint Y = 15;
static const uint8_t ADC0 = 0;
static const uint8_t ADC1 = 1;
@ -25,14 +25,14 @@ namespace pimoroni {
static const uint8_t REVERSE = 1;
static const uint8_t STOP = 2;
static const uint8_t GP0 = 0;
static const uint8_t GP1 = 1;
static const uint8_t GP2 = 2;
static const uint8_t GP3 = 3;
static const uint8_t GP4 = 4;
static const uint8_t GP5 = 5;
static const uint8_t GP6 = 6;
static const uint8_t GP7 = 7;
static const uint GP0 = 0;
static const uint GP1 = 1;
static const uint GP2 = 2;
static const uint GP3 = 3;
static const uint GP4 = 4;
static const uint GP5 = 5;
static const uint GP6 = 6;
static const uint GP7 = 7;
uint16_t *__fb;
private:
@ -50,7 +50,7 @@ namespace pimoroni {
void set_motor(uint8_t channel, uint8_t action, float speed = 0.0f);
void set_audio_pin(uint8_t pin);
void set_audio_pin(uint pin);
void set_tone(uint16_t frequency, float duty = 0.2f);
};

Wyświetl plik

@ -63,14 +63,14 @@ mp_obj_t BreakoutColourLCD160x80_make_new(const mp_obj_type_t *type, size_t n_ar
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
int slot = args[ARG_slot].u_int;
if(slot == ST7735::BG_SPI_FRONT || slot == ST7735::BG_SPI_BACK) {
if(slot == BG_SPI_FRONT || slot == BG_SPI_BACK) {
self = m_new_obj(breakout_colourlcd160x80_BreakoutColourLCD160x80_obj_t);
self->base.type = &breakout_colourlcd160x80_BreakoutColourLCD160x80_type;
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_RW);
self->breakout = new BreakoutColourLCD160x80((uint16_t *)bufinfo.buf, (ST7735::BG_SPI_SLOT)slot);
self->breakout = new BreakoutColourLCD160x80((uint16_t *)bufinfo.buf, (BG_SPI_SLOT)slot);
}
else {
mp_raise_ValueError("slot not a valid value. Expected 0 to 1");

Wyświetl plik

@ -63,14 +63,14 @@ mp_obj_t BreakoutColourLCD240x240_make_new(const mp_obj_type_t *type, size_t n_a
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
int slot = args[ARG_slot].u_int;
if(slot == ST7789::BG_SPI_FRONT || slot == ST7789::BG_SPI_BACK) {
if(slot == BG_SPI_FRONT || slot == BG_SPI_BACK) {
self = m_new_obj(breakout_colourlcd240x240_BreakoutColourLCD240x240_obj_t);
self->base.type = &breakout_colourlcd240x240_BreakoutColourLCD240x240_type;
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_RW);
self->breakout = new BreakoutColourLCD240x240((uint16_t *)bufinfo.buf, (ST7789::BG_SPI_SLOT)slot);
self->breakout = new BreakoutColourLCD240x240((uint16_t *)bufinfo.buf, (BG_SPI_SLOT)slot);
}
else {
mp_raise_ValueError("slot not a valid value. Expected 0 to 1");
@ -81,11 +81,11 @@ mp_obj_t BreakoutColourLCD240x240_make_new(const mp_obj_type_t *type, size_t n_a
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_spi, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_cs, MP_ARG_INT, {.u_int = ST7789::DEFAULT_CS_PIN} },
{ MP_QSTR_dc, MP_ARG_INT, {.u_int = ST7789::DEFAULT_DC_PIN} },
{ MP_QSTR_sck, MP_ARG_INT, {.u_int = ST7789::DEFAULT_SCK_PIN} },
{ MP_QSTR_mosi, MP_ARG_INT, {.u_int = ST7789::DEFAULT_MOSI_PIN} },
{ MP_QSTR_bl, MP_ARG_INT, {.u_int = ST7789::DEFAULT_BL_PIN} },
{ MP_QSTR_cs, MP_ARG_INT, {.u_int = pimoroni::SPI_BG_FRONT_CS} },
{ MP_QSTR_dc, MP_ARG_INT, {.u_int = pimoroni::SPI_DEFAULT_MISO} },
{ MP_QSTR_sck, MP_ARG_INT, {.u_int = pimoroni::SPI_DEFAULT_SCK} },
{ MP_QSTR_mosi, MP_ARG_INT, {.u_int = pimoroni::SPI_DEFAULT_MOSI} },
{ MP_QSTR_bl, MP_ARG_INT, {.u_int = pimoroni::SPI_BG_FRONT_PWM} },
};
// Parse args.

Wyświetl plik

@ -63,14 +63,14 @@ mp_obj_t BreakoutRoundLCD_make_new(const mp_obj_type_t *type, size_t n_args, siz
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
int slot = args[ARG_slot].u_int;
if(slot == ST7789::BG_SPI_FRONT || slot == ST7789::BG_SPI_BACK) {
if(slot == BG_SPI_FRONT || slot == BG_SPI_BACK) {
self = m_new_obj(breakout_roundlcd_BreakoutRoundLCD_obj_t);
self->base.type = &breakout_roundlcd_BreakoutRoundLCD_type;
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_RW);
self->breakout = new BreakoutRoundLCD((uint16_t *)bufinfo.buf, (ST7789::BG_SPI_SLOT)slot);
self->breakout = new BreakoutRoundLCD((uint16_t *)bufinfo.buf, (BG_SPI_SLOT)slot);
}
else {
mp_raise_ValueError("slot not a valid value. Expected 0 to 1");
@ -81,11 +81,11 @@ mp_obj_t BreakoutRoundLCD_make_new(const mp_obj_type_t *type, size_t n_args, siz
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_spi, MP_ARG_INT, {.u_int = -1} },
{ MP_QSTR_cs, MP_ARG_INT, {.u_int = ST7789::DEFAULT_CS_PIN} },
{ MP_QSTR_dc, MP_ARG_INT, {.u_int = ST7789::DEFAULT_DC_PIN} },
{ MP_QSTR_sck, MP_ARG_INT, {.u_int = ST7789::DEFAULT_SCK_PIN} },
{ MP_QSTR_mosi, MP_ARG_INT, {.u_int = ST7789::DEFAULT_MOSI_PIN} },
{ MP_QSTR_bl, MP_ARG_INT, {.u_int = ST7789::DEFAULT_BL_PIN} },
{ MP_QSTR_cs, MP_ARG_INT, {.u_int = pimoroni::SPI_BG_FRONT_CS} },
{ MP_QSTR_dc, MP_ARG_INT, {.u_int = pimoroni::SPI_DEFAULT_MISO} },
{ MP_QSTR_sck, MP_ARG_INT, {.u_int = pimoroni::SPI_DEFAULT_SCK} },
{ MP_QSTR_mosi, MP_ARG_INT, {.u_int = pimoroni::SPI_DEFAULT_MOSI} },
{ MP_QSTR_bl, MP_ARG_INT, {.u_int = pimoroni::SPI_BG_FRONT_PWM} },
};
// Parse args.