2021-01-16 12:26:03 +00:00
|
|
|
#include <math.h>
|
2021-01-20 15:59:48 +00:00
|
|
|
#include <string.h>
|
2021-01-16 12:26:03 +00:00
|
|
|
|
2021-01-23 23:39:32 +00:00
|
|
|
#include "hardware/gpio.h" // Workaround SDK bug - https://github.com/raspberrypi/pico-sdk/issues/3
|
2021-01-16 12:26:03 +00:00
|
|
|
#include "hardware/pwm.h"
|
|
|
|
|
|
|
|
#include "pico_display.hpp"
|
|
|
|
|
|
|
|
const uint8_t LED_R = 6;
|
|
|
|
const uint8_t LED_G = 7;
|
|
|
|
const uint8_t LED_B = 8;
|
|
|
|
|
|
|
|
namespace pimoroni {
|
|
|
|
|
2021-01-20 18:21:53 +00:00
|
|
|
PicoDisplay::PicoDisplay(uint16_t *buf)
|
2021-06-05 18:58:23 +01:00
|
|
|
: PicoGraphics(WIDTH, HEIGHT, buf), screen(WIDTH, HEIGHT, buf, BG_SPI_FRONT) {
|
2021-01-20 18:21:53 +00:00
|
|
|
__fb = buf;
|
2021-01-20 15:59:48 +00:00
|
|
|
}
|
2021-01-16 12:26:03 +00:00
|
|
|
|
2021-03-06 21:27:40 +00:00
|
|
|
PicoDisplay::PicoDisplay(uint16_t *buf, int width, int height)
|
2021-06-05 18:58:23 +01:00
|
|
|
: PicoGraphics(width, height, buf), screen(width, height, buf, BG_SPI_FRONT) {
|
2021-03-06 21:27:40 +00:00
|
|
|
__fb = buf;
|
|
|
|
}
|
|
|
|
|
2021-01-20 15:59:48 +00:00
|
|
|
void PicoDisplay::init() {
|
2021-01-16 12:26:03 +00:00
|
|
|
// setup the rgb led for pwm control
|
|
|
|
pwm_config cfg = pwm_get_default_config();
|
|
|
|
pwm_config_set_output_polarity(&cfg, true, true);
|
|
|
|
|
|
|
|
// red
|
|
|
|
pwm_set_wrap(pwm_gpio_to_slice_num(LED_R), 65535);
|
|
|
|
pwm_init(pwm_gpio_to_slice_num(LED_R), &cfg, true);
|
|
|
|
gpio_set_function(LED_R, GPIO_FUNC_PWM);
|
|
|
|
|
|
|
|
// green
|
|
|
|
pwm_set_wrap(pwm_gpio_to_slice_num(LED_G), 65535);
|
|
|
|
pwm_init(pwm_gpio_to_slice_num(LED_G), &cfg, true);
|
|
|
|
gpio_set_function(LED_G, GPIO_FUNC_PWM);
|
|
|
|
|
|
|
|
// blue
|
|
|
|
pwm_set_wrap(pwm_gpio_to_slice_num(LED_B), 65535);
|
|
|
|
pwm_init(pwm_gpio_to_slice_num(LED_B), &cfg, true);
|
|
|
|
gpio_set_function(LED_B, GPIO_FUNC_PWM);
|
|
|
|
|
|
|
|
// setup button inputs
|
|
|
|
gpio_set_function(A, GPIO_FUNC_SIO); gpio_set_dir(A, GPIO_IN); gpio_pull_up(A);
|
|
|
|
gpio_set_function(B, GPIO_FUNC_SIO); gpio_set_dir(B, GPIO_IN); gpio_pull_up(B);
|
|
|
|
gpio_set_function(X, GPIO_FUNC_SIO); gpio_set_dir(X, GPIO_IN); gpio_pull_up(X);
|
|
|
|
gpio_set_function(Y, GPIO_FUNC_SIO); gpio_set_dir(Y, GPIO_IN); gpio_pull_up(Y);
|
|
|
|
|
|
|
|
// initialise the screen
|
|
|
|
screen.init();
|
|
|
|
}
|
|
|
|
|
2021-01-20 15:59:48 +00:00
|
|
|
void PicoDisplay::update() {
|
|
|
|
screen.update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PicoDisplay::set_backlight(uint8_t brightness) {
|
|
|
|
screen.set_backlight(brightness);
|
|
|
|
}
|
|
|
|
|
2021-01-16 12:26:03 +00:00
|
|
|
void PicoDisplay::set_led(uint8_t r, uint8_t g, uint8_t b) {
|
|
|
|
// gamma correct the provided 0-255 brightness value onto a
|
|
|
|
// 0-65535 range for the pwm counter
|
|
|
|
static const float gamma = 2.8;
|
|
|
|
|
|
|
|
uint16_t value;
|
|
|
|
|
|
|
|
// red
|
2021-01-20 15:59:48 +00:00
|
|
|
value = (uint16_t)(pow((float)(r) / 255.0f, gamma) * 65535.0f + 0.5f);
|
2021-01-16 12:26:03 +00:00
|
|
|
pwm_set_gpio_level(LED_R, value);
|
|
|
|
|
|
|
|
// green
|
2021-01-20 15:59:48 +00:00
|
|
|
value = (uint16_t)(pow((float)(g) / 255.0f, gamma) * 65535.0f + 0.5f);
|
2021-01-16 12:26:03 +00:00
|
|
|
pwm_set_gpio_level(LED_G, value);
|
|
|
|
|
|
|
|
// blue
|
2021-01-20 15:59:48 +00:00
|
|
|
value = (uint16_t)(pow((float)(b) / 255.0f, gamma) * 65535.0f + 0.5f);
|
2021-01-16 12:26:03 +00:00
|
|
|
pwm_set_gpio_level(LED_B, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PicoDisplay::is_pressed(uint8_t button) {
|
|
|
|
return !gpio_get(button);
|
|
|
|
}
|
|
|
|
|
2021-03-06 17:12:47 +00:00
|
|
|
void PicoDisplay::flip() {
|
2021-03-06 16:45:38 +00:00
|
|
|
screen.flip();
|
|
|
|
}
|
|
|
|
}
|