2021-02-03 14:20:31 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
// **************************************************************************
|
|
|
|
// Demonstrate the Pimoroni RTC module (RV3028)
|
|
|
|
// Assumes that a Pico Display Pack (a 1.14inch IPS LCD screen with four
|
|
|
|
// useful buttons) is installed, and that the RV3028 I2C module has
|
|
|
|
// sda, scl and int on GPIO 20, 21 and 22
|
|
|
|
// Displays a clock showing date and time (no code yet to set the time,
|
|
|
|
// so do it on a separate machine), or a countdown timer.
|
|
|
|
// Button A selects the timer setting function, and runs the timer once set
|
|
|
|
// Button B selects the clock (the selection on startup)
|
|
|
|
// Button X increments the timer delay in seconds
|
|
|
|
// Button Y decrements it
|
|
|
|
// (There are on-screen reminders of the active buttons)
|
|
|
|
// **************************************************************************
|
|
|
|
|
2021-02-08 16:40:02 +00:00
|
|
|
// To use PicoExplorer rather than PicoDisplay, uncomment the following line
|
|
|
|
// #define USE_PICO_EXPLORER 1
|
|
|
|
// This:
|
2022-05-29 14:42:31 +01:00
|
|
|
// - Includes pico_explorer.hpp rather than display.hpp
|
2021-02-08 16:40:02 +00:00
|
|
|
// - Replaces all PicoDisplay references with PicoExplorer
|
|
|
|
// - Leaves out the .set_led() calls in flash_led()
|
|
|
|
#ifdef USE_PICO_EXPLORER
|
|
|
|
#include "pico_explorer.hpp"
|
|
|
|
#else
|
2021-02-03 14:20:31 +00:00
|
|
|
#include "pico_display.hpp"
|
2021-02-08 16:40:02 +00:00
|
|
|
#endif
|
2021-05-10 06:23:34 +01:00
|
|
|
#include "breakout_rtc.hpp"
|
2021-02-03 14:20:31 +00:00
|
|
|
|
2022-06-07 19:02:30 +01:00
|
|
|
#include "drivers/st7789/st7789.hpp"
|
|
|
|
#include "libraries/pico_graphics/pico_graphics.hpp"
|
2022-05-29 14:42:31 +01:00
|
|
|
#include "drivers/button/button.hpp"
|
|
|
|
#include "drivers/rgbled/rgbled.hpp"
|
|
|
|
|
2021-02-03 14:20:31 +00:00
|
|
|
#define MODE_DISP_CLOCK 0
|
|
|
|
#define MODE_DISP_TIMER 1
|
|
|
|
#define MODE_SET_TIMER 2
|
|
|
|
|
|
|
|
#define DEFAULT_TIMER_COUNT 5
|
|
|
|
|
|
|
|
using namespace pimoroni;
|
|
|
|
|
2021-02-08 16:40:02 +00:00
|
|
|
#ifdef USE_PICO_EXPLORER
|
|
|
|
uint16_t screen_width = PicoExplorer::WIDTH;
|
|
|
|
uint16_t screen_height = PicoExplorer::HEIGHT;
|
2022-05-29 14:42:31 +01:00
|
|
|
|
|
|
|
Button button_a(PicoExplorer::A);
|
|
|
|
Button button_b(PicoExplorer::B);
|
|
|
|
Button button_x(PicoExplorer::X);
|
|
|
|
Button button_y(PicoExplorer::Y);
|
|
|
|
|
2021-02-08 16:40:02 +00:00
|
|
|
#else
|
|
|
|
uint16_t screen_width = PicoDisplay::WIDTH;
|
|
|
|
uint16_t screen_height = PicoDisplay::HEIGHT;
|
2022-05-29 14:42:31 +01:00
|
|
|
|
|
|
|
Button button_a(PicoDisplay::A);
|
|
|
|
Button button_b(PicoDisplay::B);
|
|
|
|
Button button_x(PicoDisplay::X);
|
|
|
|
Button button_y(PicoDisplay::Y);
|
|
|
|
|
|
|
|
RGBLED led(PicoDisplay::LED_R, PicoDisplay::LED_G, PicoDisplay::LED_B);
|
2021-02-08 16:40:02 +00:00
|
|
|
#endif
|
2021-02-03 14:20:31 +00:00
|
|
|
|
2022-06-07 19:02:30 +01:00
|
|
|
ST7789 st7789(screen_width, screen_height, ROTATE_0, false, get_spi_pins(BG_SPI_FRONT));
|
|
|
|
PicoGraphics_PenRGB332 graphics(st7789.width, st7789.height, nullptr);
|
2022-05-29 14:42:31 +01:00
|
|
|
|
2021-05-10 06:23:34 +01:00
|
|
|
BreakoutRTC rtc;
|
2021-02-03 14:20:31 +00:00
|
|
|
|
2021-02-07 10:49:21 +00:00
|
|
|
#define LOW_COUNT_MOD 40
|
|
|
|
#define HIGH_COUNT_MOD 20
|
2021-05-10 06:23:34 +01:00
|
|
|
|
2021-02-07 10:49:21 +00:00
|
|
|
bool repeat_count_reached(uint16_t curr_count) {
|
|
|
|
// Check whether the current counter means that a key has repeated
|
2021-05-10 06:23:34 +01:00
|
|
|
if(curr_count <= 10*LOW_COUNT_MOD) {
|
2021-02-07 10:49:21 +00:00
|
|
|
return (0 == (curr_count % LOW_COUNT_MOD));
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-02-07 10:49:21 +00:00
|
|
|
return (0 == (curr_count % HIGH_COUNT_MOD));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define FLASH_MOD 20
|
|
|
|
void flash_led(uint32_t curr_count) {
|
|
|
|
// Flash the LED based on the current loop counter
|
|
|
|
// curr_count=0 will turn LED off
|
2021-02-08 16:40:02 +00:00
|
|
|
#ifndef USE_PICO_EXPLORER
|
2021-05-10 06:23:34 +01:00
|
|
|
if((curr_count % FLASH_MOD) < (FLASH_MOD / 2)) {
|
2021-02-07 10:49:21 +00:00
|
|
|
// value less than half modded number - LED off
|
2022-05-29 14:42:31 +01:00
|
|
|
led.set_rgb(0, 0, 0);
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-02-07 10:49:21 +00:00
|
|
|
// value more than half modded number - LED on
|
2022-05-29 14:42:31 +01:00
|
|
|
led.set_rgb(128, 128, 128);
|
2021-02-07 10:49:21 +00:00
|
|
|
}
|
2021-02-08 16:40:02 +00:00
|
|
|
#endif
|
2021-02-07 10:49:21 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 14:20:31 +00:00
|
|
|
int main() {
|
2022-06-07 19:02:30 +01:00
|
|
|
Pen WHITE = graphics.create_pen(255, 255, 255);
|
|
|
|
Pen BG = graphics.create_pen(55, 65, 75);
|
|
|
|
Pen RED = graphics.create_pen(255, 0, 0);
|
|
|
|
Pen GREEN = graphics.create_pen(0, 255, 0);
|
2021-02-03 14:20:31 +00:00
|
|
|
|
2021-05-10 06:23:34 +01:00
|
|
|
rtc.init();
|
|
|
|
// rtc.setup(false);
|
2021-02-03 14:20:31 +00:00
|
|
|
|
2021-05-10 06:23:34 +01:00
|
|
|
// Make sure we have 24-hour time (smaller display!)
|
|
|
|
if(rtc.is_12_hour())
|
|
|
|
rtc.set_24_hour();
|
2021-02-03 14:20:31 +00:00
|
|
|
|
|
|
|
// Use these variables to make the buttons single-shot
|
2021-02-07 10:49:21 +00:00
|
|
|
// Counts number of loops pressed, 0 if not pressed
|
|
|
|
// Only for x and y - a and b are single-shot
|
|
|
|
uint16_t a_pressed = 0;
|
|
|
|
uint16_t b_pressed = 0;
|
|
|
|
uint16_t x_pressed = 0;
|
|
|
|
uint16_t y_pressed = 0;
|
2021-02-03 14:20:31 +00:00
|
|
|
|
|
|
|
struct pt {
|
|
|
|
float x;
|
|
|
|
float y;
|
|
|
|
uint8_t r;
|
|
|
|
float dx;
|
|
|
|
float dy;
|
|
|
|
uint16_t pen;
|
|
|
|
};
|
|
|
|
|
|
|
|
uint32_t i = 0;
|
|
|
|
char buf[256];
|
|
|
|
uint8_t display_mode = MODE_DISP_CLOCK;
|
|
|
|
uint16_t timer_count = DEFAULT_TIMER_COUNT;
|
|
|
|
|
|
|
|
while(true) {
|
|
|
|
|
2022-05-29 14:42:31 +01:00
|
|
|
if(a_pressed == 0 && button_a.read()) {
|
2021-02-07 10:49:21 +00:00
|
|
|
a_pressed = 1;
|
2021-05-10 06:23:34 +01:00
|
|
|
if(display_mode == MODE_DISP_CLOCK) {
|
2021-02-03 14:20:31 +00:00
|
|
|
// We were displaying clock = set up timer
|
|
|
|
display_mode = MODE_SET_TIMER;
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
|
|
|
else if(display_mode == MODE_SET_TIMER) {
|
2021-02-03 14:20:31 +00:00
|
|
|
// We were setting up the timer - run it
|
2021-05-10 06:23:34 +01:00
|
|
|
rtc.set_timer(false, 1, timer_count, true, true, false);
|
2021-02-03 14:20:31 +00:00
|
|
|
display_mode = MODE_DISP_TIMER;
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
|
|
|
else if(display_mode == MODE_DISP_TIMER) {
|
2021-02-03 14:20:31 +00:00
|
|
|
// We were running the timer - go back to setting up
|
2021-05-10 06:23:34 +01:00
|
|
|
if(0 == (timer_count = rtc.get_timer_count())) {
|
2021-02-03 14:20:31 +00:00
|
|
|
timer_count = DEFAULT_TIMER_COUNT;
|
|
|
|
}
|
2021-05-10 06:23:34 +01:00
|
|
|
rtc.disable_timer_interrupt();
|
|
|
|
rtc.disable_timer();
|
|
|
|
rtc.clear_timer_interrupt_flag();
|
2021-02-03 14:20:31 +00:00
|
|
|
display_mode = MODE_SET_TIMER;
|
|
|
|
}
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
2022-05-29 14:42:31 +01:00
|
|
|
else if(a_pressed >= 1 && !button_a.read()) {
|
2021-02-07 10:49:21 +00:00
|
|
|
a_pressed = 0;
|
2021-02-03 14:20:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 14:42:31 +01:00
|
|
|
if(b_pressed == 0 && button_b.read()) {
|
2021-02-07 10:49:21 +00:00
|
|
|
b_pressed = 1;
|
2021-05-10 06:23:34 +01:00
|
|
|
if((display_mode == MODE_DISP_TIMER)
|
2021-02-03 14:20:31 +00:00
|
|
|
|| (display_mode == MODE_SET_TIMER)) {
|
|
|
|
// We were setting or displaying timer - revert to clock
|
2021-05-10 06:23:34 +01:00
|
|
|
rtc.disable_timer_interrupt();
|
|
|
|
rtc.disable_timer();
|
|
|
|
rtc.clear_timer_interrupt_flag();
|
2021-02-03 14:20:31 +00:00
|
|
|
display_mode = MODE_DISP_CLOCK;
|
|
|
|
timer_count = DEFAULT_TIMER_COUNT;
|
|
|
|
}
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
2022-05-29 14:42:31 +01:00
|
|
|
else if(b_pressed >= 1 && !button_b.read()) {
|
2021-02-07 10:49:21 +00:00
|
|
|
b_pressed = 0;
|
2021-02-03 14:20:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 14:42:31 +01:00
|
|
|
if(x_pressed == 0 && button_x.read()) {
|
2021-02-07 10:49:21 +00:00
|
|
|
x_pressed = 1;
|
2021-05-10 06:23:34 +01:00
|
|
|
if(display_mode == MODE_SET_TIMER) {
|
2021-02-03 14:20:31 +00:00
|
|
|
// Setting timer - Increment count
|
|
|
|
timer_count++;
|
|
|
|
}
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
2022-05-29 14:42:31 +01:00
|
|
|
else if(x_pressed >= 1 && button_x.read()) {
|
2021-02-07 10:49:21 +00:00
|
|
|
// Button still pressed - check if has reached repeat count
|
2021-05-10 06:23:34 +01:00
|
|
|
if(repeat_count_reached(x_pressed++)) {
|
2021-02-07 10:49:21 +00:00
|
|
|
timer_count++;
|
|
|
|
}
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
2022-05-29 14:42:31 +01:00
|
|
|
else if(x_pressed >= 1 && !button_x.read()) {
|
2021-02-07 10:49:21 +00:00
|
|
|
x_pressed = 0;
|
2021-02-03 14:20:31 +00:00
|
|
|
}
|
|
|
|
|
2022-05-29 14:42:31 +01:00
|
|
|
if(y_pressed == 0 && button_y.read()) {
|
2021-02-07 10:49:21 +00:00
|
|
|
y_pressed = 1;
|
2021-05-10 06:23:34 +01:00
|
|
|
if(display_mode == MODE_SET_TIMER) {
|
2021-02-03 14:20:31 +00:00
|
|
|
// Setting timer - Decrement count
|
|
|
|
if (timer_count >= 1) timer_count--;
|
|
|
|
}
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
2022-05-29 14:42:31 +01:00
|
|
|
else if(y_pressed >= 1 && button_y.read()) {
|
2021-02-07 10:49:21 +00:00
|
|
|
// Button still pressed - check if has reached repeat count
|
2021-05-10 06:23:34 +01:00
|
|
|
if(repeat_count_reached(y_pressed++)) {
|
|
|
|
if(timer_count >= 1)
|
|
|
|
timer_count--;
|
2021-02-07 10:49:21 +00:00
|
|
|
}
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
2022-05-29 14:42:31 +01:00
|
|
|
else if(y_pressed >= 1 && !button_y.read()) {
|
2021-02-07 10:49:21 +00:00
|
|
|
y_pressed = 0;
|
2021-02-03 14:20:31 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 16:40:02 +00:00
|
|
|
Rect text_box(5, 5, screen_width-10, screen_height-10);
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.set_pen(BG);
|
|
|
|
graphics.rectangle(text_box);
|
2021-02-03 14:20:31 +00:00
|
|
|
// text_box.deflate(10);
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.set_clip(text_box);
|
|
|
|
graphics.set_pen(WHITE);
|
2021-05-10 06:23:34 +01:00
|
|
|
switch(display_mode) {
|
2021-02-03 14:20:31 +00:00
|
|
|
case MODE_DISP_CLOCK:
|
|
|
|
// Show the clock face
|
2021-02-07 10:49:21 +00:00
|
|
|
flash_led(0);
|
2021-05-10 06:23:34 +01:00
|
|
|
if(rtc.update_time()) {
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.text("Set Timer",
|
2021-02-03 14:20:31 +00:00
|
|
|
Point(text_box.x, text_box.y+2), 230, 1);
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.set_pen(GREEN);
|
|
|
|
graphics.text(rtc.string_date(),
|
2021-02-03 14:20:31 +00:00
|
|
|
Point(text_box.x, text_box.y+20), 230, 4);
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.set_pen(RED);
|
|
|
|
graphics.text(rtc.string_time(),
|
2021-02-03 14:20:31 +00:00
|
|
|
Point(text_box.x, text_box.y+60), 230, 6);
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.set_pen(WHITE);
|
|
|
|
graphics.text("Clock",
|
2021-02-08 16:40:02 +00:00
|
|
|
Point(text_box.x, text_box.y+screen_height-20), 230, 1);
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(buf, "Time: rtc.updateTime() ret err");
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.text(buf,
|
2021-02-03 14:20:31 +00:00
|
|
|
Point(text_box.x, text_box.y), 30, 2);
|
|
|
|
}
|
|
|
|
break;
|
2021-05-10 06:23:34 +01:00
|
|
|
|
2021-02-03 14:20:31 +00:00
|
|
|
case MODE_DISP_TIMER:
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.text("Set Timer",
|
2021-02-03 14:20:31 +00:00
|
|
|
Point(text_box.x, text_box.y+2), 230, 1);
|
2021-05-10 06:23:34 +01:00
|
|
|
if(rtc.read_timer_interrupt_flag()) {
|
2021-02-03 14:20:31 +00:00
|
|
|
// Go periodic time interupt - say loop ended
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.set_pen(RED);
|
2021-02-03 14:20:31 +00:00
|
|
|
sprintf(buf, "%s", "Timer complete");
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.text(buf,
|
2021-02-03 14:20:31 +00:00
|
|
|
Point(text_box.x, text_box.y+30), 230, 4);
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.set_pen(WHITE);
|
2021-02-07 10:49:21 +00:00
|
|
|
flash_led(i);
|
2021-05-10 06:23:34 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sprintf(buf, "%s %d", "Timer running", rtc.get_timer_count());
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.text(buf,
|
2021-02-08 09:04:54 +00:00
|
|
|
Point(text_box.x, text_box.y+30), 230, 3);
|
2021-02-03 14:20:31 +00:00
|
|
|
}
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.text("Clock",
|
2021-02-08 16:40:02 +00:00
|
|
|
Point(text_box.x, text_box.y+screen_height-20), 230, 1);
|
2021-02-03 14:20:31 +00:00
|
|
|
break;
|
2021-05-10 06:23:34 +01:00
|
|
|
|
2021-02-03 14:20:31 +00:00
|
|
|
case MODE_SET_TIMER:
|
2021-02-07 10:49:21 +00:00
|
|
|
flash_led(0);
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.text("Run Timer",
|
2021-02-03 14:20:31 +00:00
|
|
|
Point(text_box.x, text_box.y+2), 230, 1);
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.text("+ Time",
|
2021-02-08 16:40:02 +00:00
|
|
|
Point(text_box.x+screen_width-42, text_box.y+2), 230, 1);
|
2021-02-03 14:20:31 +00:00
|
|
|
sprintf(buf, "Time %d secs", timer_count);
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.text(buf,
|
2021-02-03 14:20:31 +00:00
|
|
|
Point(text_box.x, text_box.y+30), 230, 3);
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.text("Clock",
|
2021-02-08 16:40:02 +00:00
|
|
|
Point(text_box.x, text_box.y+screen_height-20), 230, 1);
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.text("- Time",
|
2021-02-08 16:40:02 +00:00
|
|
|
Point(text_box.x+screen_width-42,
|
|
|
|
text_box.y+screen_height-20), 230, 1);
|
2021-02-03 14:20:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-06-07 19:02:30 +01:00
|
|
|
graphics.remove_clip();
|
2021-02-03 14:20:31 +00:00
|
|
|
|
|
|
|
// update screen
|
2022-06-07 19:02:30 +01:00
|
|
|
st7789.update(&graphics);
|
2021-02-03 14:20:31 +00:00
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2021-05-10 06:23:34 +01:00
|
|
|
return 0;
|
2021-02-03 14:20:31 +00:00
|
|
|
}
|