Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

17-introduce-ssd1306 #25

Merged
merged 10 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ defmt-rtt = "0.4"
defmt-test = "0.3"
fixed = "1.23.1"
fixed-macro = "1.2"

cortex-m = { version = "0.7.6", features = ["inline-asm"] }
cortex-m-rt = "0.7.0"
critical-section = "1.1"
Expand All @@ -73,7 +72,7 @@ st7789 = "0.7.0"
display-interface = "0.5.0"
byte-slice-cast = { version = "1.2.0", default-features = false }
smart-leds = "0.4.0"
#attention, in version 0.2.0 of ws2812-async there is a bug requiring to interchange red and green in the color struct
ssd1306-async = { git = "https://github.com/kalkyl/ssd1306-async" }
ws2812-async = "0.2.0"
heapless = "0.8"
usbd-hid = "0.7.0"
Expand All @@ -92,6 +91,9 @@ pio = "0.2.1"
rand = { version = "0.8.5", default-features = false }
embedded-sdmmc = "0.7.0"
reqwless = { version = "0.12.0", features = ["defmt"] }
tinybmp = "0.6.0"
assign-resources = { git = "https://github.com/adamgreig/assign-resources", rev = "94ad10e2729afdf0fd5a77cd12e68409a982f58a" }


[build-dependencies]
serde = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion flash_wifi_firmware.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Navigate to the cyw43-firmware directory
Set-Location -Path "C:\Users\rafae\git\pi-pico-alarmclock-rust\cargo-generate\cyw43-firmware"
Set-Location -Path "C:\Users\rafae\git\pi-pico-alarmclock-rust\wifi-firmware\cyw43-firmware"

# Download firmware using probe-rs
Write-Host "Downloading firmware..."
Expand Down
Binary file added media/0.bmp
Binary file not shown.
Binary file added media/1.bmp
Binary file not shown.
Binary file added media/2.bmp
Binary file not shown.
Binary file added media/3.bmp
Binary file not shown.
Binary file added media/4.bmp
Binary file not shown.
Binary file added media/5.bmp
Binary file not shown.
Binary file added media/6.bmp
Binary file not shown.
Binary file added media/7.bmp
Binary file not shown.
Binary file added media/8.bmp
Binary file not shown.
Binary file added media/9.bmp
Binary file not shown.
Binary file added media/bat_000.bmp
Binary file not shown.
Binary file added media/bat_020.bmp
Binary file not shown.
Binary file added media/bat_040.bmp
Binary file not shown.
Binary file added media/bat_060.bmp
Binary file not shown.
Binary file added media/bat_080.bmp
Binary file not shown.
Binary file added media/bat_100.bmp
Binary file not shown.
Binary file added media/bat_mains.bmp
Binary file not shown.
Binary file added media/colon.bmp
Binary file not shown.
Binary file added media/saber.bmp
Binary file not shown.
Binary file added media/settings.bmp
Binary file not shown.
157 changes: 31 additions & 126 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@
#![no_std]
#![no_main]

use crate::task::alarm_mgr::AlarmManager;
use crate::task::time_updater::TimeUpdater;
use crate::task::btn_mgr::{blue_button, green_button, yellow_button};
use crate::task::display::display;
use crate::task::resources::{
AssignedResources, BlueButtonResources, DisplayResources, GreenButtonResources,
NeopixelResources, RtcResources, WifiResources, YellowButtonResources,
};
use crate::task::time_updater::connect_and_update_rtc;
use core::cell::RefCell;
use cyw43_pio::PioSpi; // for WiFi
// for WiFi
use defmt::*; // global logger
use embassy_executor::Executor;
use embassy_executor::Spawner; // executor
use embassy_rp::gpio::{self, Input};
use embassy_executor::Spawner;
use embassy_rp::multicore::{spawn_core1, Stack};
use embassy_rp::peripherals::PIO0;
use embassy_rp::pio::{InterruptHandler, Pio};
use embassy_rp::peripherals;
use embassy_rp::rtc::Rtc;
use embassy_rp::spi::{Config, Phase, Polarity, Spi};
use embassy_rp::{bind_interrupts, peripherals};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel;
use embassy_sync::mutex::Mutex;
use embassy_time::{Duration, Timer}; // time
use gpio::{Level, Output};
use embassy_time::{Duration, Timer};
use static_cell::StaticCell;
use task::alarm_mgr;
use {defmt_rtt as _, panic_probe as _}; // panic handler
use {defmt_rtt as _, panic_probe as _};

// import the task module (submodule of src)
mod task;
Expand All @@ -38,102 +34,34 @@ async fn main(spawner: Spawner) {

// Initialize the peripherals for the RP2040
let p = embassy_rp::init(Default::default());
// and assign the peripherals to the places, where we will use them
let r = split_resources!(p);

// bind the interrupts
bind_interrupts!(struct Irqs {
PIO0_IRQ_0 => InterruptHandler<PIO0>;
});
// Buttons
spawner.spawn(green_button(spawner, r.btn_green)).unwrap();
spawner.spawn(blue_button(spawner, r.btn_blue)).unwrap();
spawner.spawn(yellow_button(spawner, r.btn_yellow)).unwrap();

// Alarm Manager
let alarm_mgr = AlarmManager::new();
// RTC

// buttons
// green_button
info!("init green button");
let green_button_input = Input::new(p.PIN_20, gpio::Pull::Up);
spawner
.spawn(task::btn_mgr::green_button(spawner, green_button_input))
.unwrap();

//blue_button
info!("init blue button");
let blue_button_input = Input::new(p.PIN_21, gpio::Pull::Up);
spawner
.spawn(task::btn_mgr::blue_button(spawner, blue_button_input))
.unwrap();

//yellow_button
info!("init yellow button");
let yellow_button_input = Input::new(p.PIN_22, gpio::Pull::Up);
spawner
.spawn(task::btn_mgr::yellow_button(spawner, yellow_button_input))
.unwrap();

// Real Time Clock
// Setup for WiFi connection and RTC update
info!("init wifi");
let pwr = Output::new(p.PIN_23, Level::Low);
let cs = Output::new(p.PIN_25, Level::High);
let mut pio_wifi = Pio::new(p.PIO0, Irqs);
let spi_wifi = PioSpi::new(
&mut pio_wifi.common,
pio_wifi.sm0,
pio_wifi.irq0,
cs,
p.PIN_24,
p.PIN_29,
p.DMA_CH0,
);

// Initialize the RTC in a static cell to be used in the time_updater module
// Initialize the RTC in a static cell, we will need it in multiple places
static RTC: StaticCell<RefCell<Rtc<'static, peripherals::RTC>>> = StaticCell::new();
let rtc_instance: Rtc<'static, peripherals::RTC> = Rtc::new(p.RTC);
let rtc_instance: Rtc<'static, peripherals::RTC> = Rtc::new(r.rtc.rtc_inst);
let rtc_ref = RTC.init(RefCell::new(rtc_instance));

// Initialize TimeUpdater
let time_updater = TimeUpdater::new();

// Call connect_wifi with the necessary parameters
// update the RTC
spawner
.spawn(task::time_updater::connect_and_update_rtc(
spawner,
time_updater,
pwr,
spi_wifi,
rtc_ref,
))
.spawn(connect_and_update_rtc(spawner, r.wifi, rtc_ref))
.unwrap();

// Neopixel
// Spi configuration for the neopixel
let mut config = Config::default();
config.frequency = 3_800_000;
config.phase = Phase::CaptureOnFirstTransition;
config.polarity = Polarity::IdleLow;
let spi_np = Spi::new_txonly(p.SPI0, p.PIN_18, p.PIN_19, p.DMA_CH1, config);

// Initialize the mutex for the spi_np, to be used in the neopixel module
static SPI_NP: task::neopixel::SpiType = Mutex::new(None);
static NP_MGR: task::neopixel::NeopixelManagerType = Mutex::new(None);

let neopixel_mgr = task::neopixel::NeopixelManager::new(100, 10);

{
// Lock the mutex to access its content
*(SPI_NP.lock().await) = Some(spi_np);
*(NP_MGR.lock().await) = Some(neopixel_mgr);
}
// Note! -> we may need more than one neopixel task eventually, in that case we will need mutexes around the resources
// i want to keep it simple for now

// the neopixel task will be spawned on core1, because it will run in parallel to the other tasks and it may block
// spawn the neopixel tasks, on core1 as opposed to the other tasks
static mut CORE1_STACK: Stack<4096> = Stack::new();
static EXECUTOR1: StaticCell<Executor> = StaticCell::new();
static ALARM_IDLE_CHANNEL: Channel<CriticalSectionRawMutex, task::alarm_mgr::AlarmState, 1> =
Channel::new();
static ALARM_TRIGGERED_CHANNEL: Channel<
CriticalSectionRawMutex,
task::alarm_mgr::AlarmState,
1,
> = Channel::new();

spawn_core1(
p.CORE1,
Expand All @@ -142,25 +70,16 @@ async fn main(spawner: Spawner) {
let executor1 = EXECUTOR1.init(Executor::new());
executor1.run(|spawner| {
spawner
.spawn(task::neopixel::analog_clock(
spawner,
&SPI_NP,
&NP_MGR,
ALARM_IDLE_CHANNEL.receiver(),
))
.unwrap();
spawner
.spawn(task::neopixel::sunrise(
spawner,
&SPI_NP,
&NP_MGR,
ALARM_TRIGGERED_CHANNEL.receiver(),
))
.spawn(task::neopixel::analog_clock(spawner, r.neopixel))
.unwrap();
});
},
);

// Display

spawner.spawn(display(spawner, r.display)).unwrap();

// Main loop, doing very little
loop {
if let Ok(dt) = rtc_ref.borrow_mut().now() {
Expand All @@ -170,19 +89,5 @@ async fn main(spawner: Spawner) {
);
}
Timer::after(Duration::from_secs(10)).await;

info!("Sending idle signal to neopixel tasks");
ALARM_IDLE_CHANNEL
.sender()
.send(alarm_mgr::AlarmState::Idle)
.await;

Timer::after(Duration::from_secs(10)).await;

info!("Sending triggered signal to neopixel tasks");
ALARM_TRIGGERED_CHANNEL
.sender()
.send(alarm_mgr::AlarmState::Triggered)
.await;
}
}
37 changes: 0 additions & 37 deletions src/task/alarm_mgr.rs

This file was deleted.

25 changes: 16 additions & 9 deletions src/task/btn_mgr.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
use crate::{BlueButtonResources, YellowButtonResources, GreenButtonResources};
use defmt::info;
use embassy_executor::Spawner;
use embassy_rp::gpio::{Input, Level};
use embassy_rp::gpio::{self, Input, Level};
use embassy_time::{with_deadline, Duration, Instant, Timer};
use {defmt_rtt as _, panic_probe as _};

// Button Manager
// Handles button press, hold, and long hold
// Debounces button press
pub struct ButtonManager<'a> {
input: Input<'a>,
debounce_duration: Duration,
id: &'a str,
}

impl<'a> ButtonManager<'a> {
pub fn new(input: Input<'a>, debounce_duration: Duration, id: &'a str) -> Self {
pub fn new(input: Input<'a>, id: &'a str) -> Self {
Self {
input,
debounce_duration,
debounce_duration: Duration::from_millis(100),
id,
}
}
Expand Down Expand Up @@ -73,22 +77,25 @@ impl<'a> ButtonManager<'a> {
}

#[embassy_executor::task]
pub async fn green_button(_spawner: Spawner, input: Input<'static>) {
let mut btn = ButtonManager::new(input, Duration::from_millis(100), "green_button");
pub async fn green_button(_spawner: Spawner, r: GreenButtonResources) {
let input = gpio::Input::new(r.button_pin, gpio::Pull::Up);
let mut btn = ButtonManager::new(input, "green_button");
info!("{} task started", btn.id);
btn.handle_button_press().await;
}

#[embassy_executor::task]
pub async fn yellow_button(_spawner: Spawner, input: Input<'static>) {
let mut btn = ButtonManager::new(input, Duration::from_millis(100), "yellow_button");
pub async fn blue_button(_spawner: Spawner, r: BlueButtonResources) {
let input = gpio::Input::new(r.button_pin, gpio::Pull::Up);
let mut btn = ButtonManager::new(input, "blue_button");
info!("{} task started", btn.id);
btn.handle_button_press().await;
}

#[embassy_executor::task]
pub async fn blue_button(_spawner: Spawner, input: Input<'static>) {
let mut btn = ButtonManager::new(input, Duration::from_millis(100), "blue_button");
pub async fn yellow_button(_spawner: Spawner, r: YellowButtonResources) {
let input = gpio::Input::new(r.button_pin, gpio::Pull::Up);
let mut btn = ButtonManager::new(input, "yellow_button");
info!("{} task started", btn.id);
btn.handle_button_press().await;
}
Loading
Loading