Skip to content

Commit

Permalink
Lightning time and stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
ImTheSquid committed Sep 12, 2024
1 parent 6ca0bd1 commit 670136e
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 114 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.embuild
/target
/Cargo.lock
/.env
48 changes: 42 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,30 @@ authors = ["Jack Hogan <[email protected]>"]
edition = "2021"
license = "MIT OR Apache-2.0"

[[bin]]
name = "sign-firmware"
harness = false

[dependencies]
esp-backtrace = { version = "0.14.0", features = [
"esp32s3",
"exception-handler",
"panic-handler",
"println",
] }
esp-hal = { version = "0.20.1", features = ["esp32s3"] }
esp-hal = { git = "https://github.com/esp-rs/esp-hal.git", features = [
"esp32s3",
] }
esp-println = { version = "0.11.0", features = ["esp32s3", "log"] }
log = { version = "0.4.21" }
esp-alloc = { version = "0.4.0" }
esp-wifi = { version = "0.8.0", features = [
esp-alloc = { git = "https://github.com/esp-rs/esp-hal.git" }
esp-wifi = { git = "https://github.com/esp-rs/esp-hal.git", features = [
"esp32s3",
"phy-enable-usb",
"utils",
"wifi",
"embassy-net",
"async",
] }
heapless = { version = "0.8.0", default-features = false }
smoltcp = { version = "0.11.0", default-features = false, features = [
Expand All @@ -36,16 +44,41 @@ smoltcp = { version = "0.11.0", default-features = false, features = [
] }
embassy-executor = { version = "0.6.0", features = ["task-arena-size-12288"] }
embassy-sync = "0.6.0"
embassy-time = { version = "0.3.1", features = ["generic-queue"] }
embassy-time = { version = "*", features = ["generic-queue"] }
embedded-hal = "1.0.0"
static_cell = "2.1.0"
esp-hal-embassy = { version = "0.3.0", features = ["esp32s3"] }
static_cell = { version = "2.1.0", features = ["nightly"] }
esp-hal-embassy = { git = "https://github.com/esp-rs/esp-hal.git", features = [
"esp32s3",
] }
palette = { version = "0.7.6", default-features = false, features = [
"alloc",
"libm",
] }
lightning-time = { version = "0.2.0", default-features = false }
chrono = { version = "0.4.38", default-features = false, features = ["alloc"] }
dotenvy_macro = "0.15.7"
embassy-net = { version = "*", features = [
"dhcpv4",
"dns",
"log",
"medium-ip",
"proto-ipv4",
"tcp",
"udp",
] }
reqwless = { version = "*", default-features = false, features = [
"log",
"embedded-tls",
"alloc",
] }
const-random = "0.1.18"
serde = { version = "1.0.210", default-features = false, features = [
"derive",
"alloc",
] }
serde_json = { version = "1.0.128", default-features = false, features = [
"alloc",
] }

[profile.dev]
# Rust debug is too slow.
Expand All @@ -60,3 +93,6 @@ incremental = false
lto = 'fat'
opt-level = 's'
overflow-checks = false

[build-dependencies]
embuild = "0.32.0"
63 changes: 17 additions & 46 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// pub mod eeprom;
// pub mod schema;

use embassy_sync::{blocking_mutex::raw::CriticalSectionRawMutex, channel::Channel};
use embassy_time::{Duration, Ticker};
use esp_hal::gpio::AnyOutput;
use esp_hal::gpio::{AnyPin, Output};

pub struct Leds {
pub buffer: [u8; 15],
Expand Down Expand Up @@ -47,21 +48,11 @@ const GAMMA_LUT: [u8; 256] = [
249, 251, 253, 255,
];

static mut TARGET_DATA_BUFFER: usize = 0;

static mut LED_DATA_BUFFER_0: [u8; 15] = [
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
];

static mut LED_DATA_BUFFER_1: [u8; 15] = [
0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8,
];
static PWM_CONTROL: Channel<CriticalSectionRawMutex, [u8; 15], 1> = Channel::new();

impl Leds {
pub fn create() -> Leds {
Leds {
buffer: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
}
Leds { buffer: [0; 15] }
}

pub async fn set_color(&mut self, color: palette::Srgb<u8>, block: Block) {
Expand All @@ -75,56 +66,36 @@ impl Leds {
self.buffer[b] = GAMMA_LUT[color.blue as usize];
}

pub async fn swap(&mut self) {
unsafe {
let mut copied_data_buffer_target = TARGET_DATA_BUFFER;

match copied_data_buffer_target {
0 => {
LED_DATA_BUFFER_1 = self.buffer;
}
1 => {
LED_DATA_BUFFER_0 = self.buffer;
}
_ => {
copied_data_buffer_target = 0;
}
}

TARGET_DATA_BUFFER = 1 - copied_data_buffer_target;
}
pub async fn swap(&self) {
PWM_CONTROL.send(self.buffer).await;
}
}

#[embassy_executor::task]
pub async fn leds_software_pwm(mut led_pins: [AnyOutput<'static>; 15]) {
pub async fn leds_software_pwm(mut led_pins: [Output<'static, AnyPin>; 15]) {
let mut timer_value: u8 = 0;
let mut last_buffer = [0_u8; 15];

// Update at 120hz
let mut ticker = Ticker::every(Duration::from_hz(256 * 120));
let mut ticker = Ticker::every(Duration::from_hz(256 * 200));

loop {
timer_value += 1;
let pwm_receiver = PWM_CONTROL.receiver();

let copied_data_buffer_target = unsafe { TARGET_DATA_BUFFER };
loop {
last_buffer = pwm_receiver.try_receive().unwrap_or(last_buffer);

for n in 0..15 {
if timer_value == 0 {
led_pins[n].set_high();
} else if unsafe {
match copied_data_buffer_target {
0 => LED_DATA_BUFFER_0[n],
1 => LED_DATA_BUFFER_1[n],
_ => {
unreachable!("Woah, that's an invalid buffer pointer you got there.");
}
}
} >= timer_value
{
}

if last_buffer[n] == timer_value {
led_pins[n].set_low();
}
}

timer_value += 1;

ticker.next().await;
}
}
Loading

0 comments on commit 670136e

Please sign in to comment.