pico-dht11/display.c
2024-08-12 15:38:18 +02:00

73 lines
No EOL
1.8 KiB
C

#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/pwm.h"
#define STEPS 50
#define ANIMATION_DELAY 1
// AAA
// F B
// GGG
// E C
// DDD o (DP)
// order: A B C D E F G DP
const uint8_t LED_PINS[] = {15, 14, 16, 19, 18, 12, 13, 17};
// I probably could make each pattern byte
const bool DIGITS[12][8] = {
{1, 1, 1, 1, 1, 1, 0, 1}, // 0
{0, 1, 1, 0, 0, 0, 0, 1}, // 1
{1, 1, 0, 1, 1, 0, 1, 1}, // 2
{1, 1, 1, 1, 0, 0, 1, 1}, // 3
{0, 1, 1, 0, 0, 1, 1, 1}, // 4
{1, 0, 1, 1, 0, 1, 1, 1}, // 5
{1, 0, 1, 1, 1, 1, 1, 1}, // 6
{1, 1, 1, 0, 0, 0, 0, 1}, // 7
{1, 1, 1, 1, 1, 1, 1, 1}, // 8
{1, 1, 1, 1, 0, 1, 1, 1}, // 9
{0, 0, 0, 0, 0, 0, 1, 0}, // -
{0, 0, 0, 0, 0, 0, 0, 1}, // .
};
void initialize_display() {
for (int i=0; i<8; i++) {
//gpio_init(LED_PINS[i]);
//gpio_set_dir(LED_PINS[i], GPIO_OUT);
//gpio_put(LED_PINS[i], true);
gpio_set_function(LED_PINS[i], GPIO_FUNC_PWM);
pwm_set_gpio_level(LED_PINS[i], STEPS);
uint8_t slice_num = pwm_gpio_to_slice_num(LED_PINS[i]);
pwm_set_wrap(slice_num, STEPS);
pwm_set_enabled(slice_num, true);
}
}
void display_digit(int digit) {
for (int i=0; i<8; i++) {
pwm_set_gpio_level(LED_PINS[i], DIGITS[digit][i] ? 0 : STEPS);
}
}
void display_animate(int from, int to) {
int delta[9];
for (int i=0; i<8; i++) {
delta[i] = DIGITS[to][i] - DIGITS[from][i];
}
for (int step=0; step<=STEPS; step++) {
for (int i=0; i<8; i++) {
if (delta[i] == 0) continue;
if (delta[i] == 1) {
pwm_set_gpio_level(LED_PINS[i], STEPS-step);
}
if (delta[i] == -1) {
pwm_set_gpio_level(LED_PINS[i], step);
}
}
sleep_ms(ANIMATION_DELAY);
}
}