Skip to content

Commit

Permalink
Merge pull request #14 from Sir-Thomas/brightness_selector
Browse files Browse the repository at this point in the history
add brightness selector
  • Loading branch information
lulf authored Jan 5, 2025
2 parents b2b1bfe + b6f983a commit 2d61c4e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
81 changes: 76 additions & 5 deletions firmware/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use display_interface_spi::SPIInterface;
use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
use embassy_embedded_hal::shared_bus::blocking::spi::SpiDevice;
use embassy_futures::select::{select, Either};
use embassy_nrf::gpio::{Input, Output};
use embassy_nrf::gpio::{AnyPin, Input, Level, Output, OutputDrive};
use embassy_nrf::peripherals::{TWISPI0, TWISPI1};
use embassy_nrf::spim::Spim;
use embassy_nrf::{saadc, twim};
Expand Down Expand Up @@ -76,13 +76,76 @@ impl<'a> Battery<'a> {
}
}

pub enum BacklightLevel {
Low,
Medium,
High,
}

pub struct Backlight<'a> {
low: Output<'a>,
med: Output<'a>,
high: Output<'a>,
level: BacklightLevel,
}

impl<'a> Backlight<'a> {
pub fn new(low_pin: AnyPin, med_pin: AnyPin, high_pin: AnyPin) -> Self {
let backlight_low = Output::new(low_pin, Level::High, OutputDrive::Standard); // Low backlight
let backlight_med = Output::new(med_pin, Level::High, OutputDrive::Standard); // Medium backlight
let backlight_high = Output::new(high_pin, Level::High, OutputDrive::Standard); // High backlight
Self {
low: backlight_low,
med: backlight_med,
high: backlight_high,
level: BacklightLevel::Medium,
}
}

fn set_level(&mut self, level: BacklightLevel) {
self.level = level;
}

fn off(&mut self) {
self.low.set_high();
self.med.set_high();
self.high.set_high();
}

fn on(&mut self) {
match self.level {
BacklightLevel::Low => self.set_low(),
BacklightLevel::Medium => self.set_medium(),
BacklightLevel::High => self.set_high(),
}
}

fn set_low(&mut self) {
self.low.set_low();
self.med.set_high();
self.high.set_high();
}

fn set_medium(&mut self) {
self.low.set_high();
self.med.set_low();
self.high.set_high();
}

fn set_high(&mut self) {
self.low.set_high();
self.med.set_high();
self.high.set_low();
}
}

pub struct Screen<'a> {
display: Display<'a>,
backlight: Output<'a>,
backlight: Backlight<'a>,
}

impl<'a> Screen<'a> {
pub fn new(display: Display<'a>, backlight: Output<'a>) -> Self {
pub fn new(display: Display<'a>, backlight: Backlight<'a>) -> Self {
Self { display, backlight }
}

Expand All @@ -91,11 +154,19 @@ impl<'a> Screen<'a> {
}

pub fn on(&mut self) {
self.backlight.set_low();
self.backlight.on();
}

pub fn off(&mut self) {
self.backlight.set_high();
self.backlight.off();
}

pub fn change_brightness(&mut self) {
match self.backlight.level {
BacklightLevel::Low => self.backlight.set_level(BacklightLevel::Medium),
BacklightLevel::Medium => self.backlight.set_level(BacklightLevel::High),
BacklightLevel::High => self.backlight.set_level(BacklightLevel::Low),
}
}
}

Expand Down
6 changes: 2 additions & 4 deletions firmware/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use core::cell::RefCell;

use defmt::unwrap;
use defmt_rtt as _;
use device::Backlight;
use display_interface_spi::SPIInterface;
use embassy_embedded_hal::flash::partition::{BlockingPartition, Partition};
use embassy_embedded_hal::shared_bus::blocking::i2c::I2cDevice;
Expand Down Expand Up @@ -188,8 +189,7 @@ async fn main(s: Spawner) {
ble::start(s, sdc, dfu_config);

// Display

let mut backlight = Output::new(p.P0_22.degrade(), Level::Low, OutputDrive::Standard); // Medium backlight
let backlight = Backlight::new(p.P0_14.degrade(), p.P0_22.degrade(), p.P0_23.degrade());
let rst = Output::new(p.P0_26, Level::Low, OutputDrive::Standard);
let display_cs = Output::new(p.P0_25, Level::High, OutputDrive::Standard); // Keep low while driving display
let display_spi = SpiDevice::new(spi_bus, display_cs);
Expand All @@ -203,8 +203,6 @@ async fn main(s: Spawner) {
.unwrap();
display.set_orientation(Orientation::new()).unwrap();

backlight.set_high();

let screen = Screen::new(display, backlight);
let mut device: Device<'_> = Device {
clock: &CLOCK,
Expand Down
4 changes: 4 additions & 0 deletions firmware/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ impl MenuState {
WatchState::Time(TimeState::new(device, Timeout::new(IDLE_TIMEOUT)).await)
}
MenuAction::Settings => WatchState::Menu(MenuState::new(MenuView::settings())),
MenuAction::Brightness => {
device.screen.change_brightness();
WatchState::Menu(MenuState::new(MenuView::settings()))
},
MenuAction::Reset => {
cortex_m::peripheral::SCB::sys_reset();
}
Expand Down
10 changes: 8 additions & 2 deletions watchful-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ pub enum MenuAction {
Settings,
FirmwareSettings,
ValidateFirmware,
Brightness,
Reset,
}

Expand All @@ -207,6 +208,7 @@ pub enum MenuView {
},
Settings {
firmware: MenuItem,
brightness: MenuItem,
reset: MenuItem,
},
Firmware {
Expand All @@ -227,6 +229,7 @@ impl MenuView {
pub fn settings() -> Self {
Self::Settings {
firmware: MenuItem::new("Firmware", 0),
brightness: MenuItem::new("Brightness", 1),
reset: MenuItem::new("Reset", 2),
}
}
Expand All @@ -253,8 +256,9 @@ impl MenuView {
settings.draw(display)?;
}

Self::Settings { firmware, reset } => {
Self::Settings { firmware, brightness, reset } => {
firmware.draw(display)?;
brightness.draw(display)?;
reset.draw(display)?;
}

Expand Down Expand Up @@ -284,9 +288,11 @@ impl MenuView {
None
}
}
Self::Settings { firmware, reset } => {
Self::Settings { firmware, brightness, reset } => {
if firmware.is_clicked(input) {
Some(MenuAction::FirmwareSettings)
} else if brightness.is_clicked(input) {
Some(MenuAction::Brightness)
} else if reset.is_clicked(input) {
Some(MenuAction::Reset)
} else {
Expand Down

0 comments on commit 2d61c4e

Please sign in to comment.