feat: bachelor test impl

This commit is contained in:
2024-11-25 21:53:08 +01:00
parent d0b0de550b
commit 061abd74b9
24 changed files with 3079 additions and 425 deletions

View File

@ -4,14 +4,14 @@ target = "xtensa-esp32-espidf"
[target.xtensa-esp32-espidf]
linker = "ldproxy"
# runner = "espflash --monitor" # Select this runner for espflash v1.x.x
runner = "espflash flash --monitor --baud 921600" # Select this runner for espflash v2.x.x
runner = "espflash flash --monitor" # --baud 921600" # Select this runner for espflash v2.x.x
rustflags = [
# Extending time_t for ESP IDF 5: https://github.com/esp-rs/rust/issues/110
"--cfg",
"espidf_time64",
# Added the following 2 entries so lvgl will build without getting string.h file not found
"--sysroot",
"/home/ed/.rustup/toolchains/esp/xtensa-esp32s3-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include",
"/home/yannik/.rustup/toolchains/esp/xtensa-esp32s3-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include",
]
[unstable]

19
lvgl-based/rustfmt.toml Normal file
View File

@ -0,0 +1,19 @@
# Edition
edition = "2021"
# Comments
format_code_in_doc_comments = true
normalize_comments = true
wrap_comments = true
# Imports
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
imports_layout = "HorizontalVertical"
# Miscellaneous
enum_discrim_align_threshold = 25
hex_literal_case = "Upper"
# Length
max_width=60

View File

@ -9,14 +9,22 @@ use std::{
use cstr_core::CString;
use display_interface_spi::SPIInterface;
use embedded_graphics_core::{draw_target::DrawTarget, prelude::Point};
use embedded_graphics_core::{
draw_target::DrawTarget,
prelude::Point,
};
use embedded_graphics_profiler_display::ProfilerDisplay;
use esp_idf_hal::spi::SpiSingleDeviceDriver;
use esp_idf_hal::{
delay::{self, Delay},
gpio::*,
peripherals::Peripherals,
spi::{config::DriverConfig, Dma, SpiConfig, SpiDeviceDriver},
spi::{
config::DriverConfig,
Dma,
SpiConfig,
SpiDeviceDriver,
},
units::FromValueType, // for converting 26MHz to value
};
use lvgl::{
@ -42,16 +50,24 @@ use lvgl::{
Widget,
};
use mipidsi::{
models::ILI9486Rgb565,
options::{ColorInversion, ColorOrder, Orientation, Rotation},
models::ILI9341Rgb565,
options::{
ColorInversion,
ColorOrder,
Orientation,
Rotation,
},
Builder,
};
use xpt2046::Xpt2046;
fn lerp_fixed(start: u8, end: u8, t: u8, max_t: u8) -> u8 {
let (start, end, t, max_t) = (start as u16, end as u16, t as u16, max_t as u16);
let (start, end, t, max_t) =
(start as u16, end as u16, t as u16, max_t as u16);
let t = t.min(max_t);
let result = start + ((end - start.min(end)) * t + (max_t / 2)) / max_t;
let result = start
+ ((end - start.min(end)) * t + (max_t / 2))
/ max_t;
result as u8
}
@ -65,7 +81,9 @@ struct Lamp {
impl Lamp {
pub fn new(name: &str) -> Self {
Self {
name: heapless::String::from(heapless::String::from_str(name).unwrap()),
name: heapless::String::from(
heapless::String::from_str(name).unwrap(),
),
on: false,
brightness: 255,
}
@ -97,8 +115,8 @@ fn main() -> Result<(), LvError> {
const VER_RES: u32 = 240;
const LINES: u32 = 20;
// It is necessary to call this function once. Otherwise some patches to the
// runtime implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
// It is necessary to call this function once. Otherwise
// some patches to the runtime implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();
// Initialize lvgl
@ -127,7 +145,9 @@ fn main() -> Result<(), LvError> {
Some(miso), // sdi
Some(cs), // cs
&DriverConfig::new().dma(Dma::Channel1(4096)),
&SpiConfig::new().write_only(true).baudrate(10.MHz().into()),
&SpiConfig::new()
.write_only(true)
.baudrate(10.MHz().into()),
)
.unwrap();
@ -140,8 +160,9 @@ fn main() -> Result<(), LvError> {
bklt.set_high().unwrap();
// Configuration for M5Stack Core Development Kit V1.0
// Puts display in landscape mode with the three buttons at the bottom of screen
// let mut m5stack_display = Builder::ili9342c_rgb565(di)
// Puts display in landscape mode with the three buttons
// at the bottom of screen let mut m5stack_display =
// Builder::ili9342c_rgb565(di)
// .with_display_size(320, 240)
// .with_color_order(ColorOrder::Bgr)
// .with_orientation(Orientation::Portrait(false))
@ -154,23 +175,25 @@ fn main() -> Result<(), LvError> {
// .with_orientation(Orientation::Portrait(false))
// .with_color_order(ColorOrder::Bgr)
// .with_invert_colors(true)
// .init(&mut Delay::new_default(), None::<PinDriver<AnyOutputPin, Output>>)
// .init(&mut Delay::new_default(),
// None::<PinDriver<AnyOutputPin, Output>>)
// .unwrap();
let raw_display = Builder::new(ILI9486Rgb565, di)
let raw_display = Builder::new(ILI9341Rgb565, di)
.orientation(Orientation {
rotation: Rotation::Deg90,
mirrored: true,
})
.color_order(ColorOrder::Bgr)
.invert_colors(ColorInversion::Inverted)
// .invert_colors(ColorInversion::Inverted)
.init(&mut Delay::new_default())
.unwrap();
let mut raw_display = ProfilerDisplay::new(raw_display);
// Stack size value - 20,000 for 10 lines, 40,000 for 20 lines
// let (touch_send, touch_recv) = channel();
// Stack size value - 20,000 for 10 lines, 40,000 for
// 20 lines let (touch_send, touch_recv) =
// channel();
let touch_irq = pins.gpio36;
let touch_mosi = pins.gpio32;
let touch_miso = pins.gpio39;
@ -185,7 +208,9 @@ fn main() -> Result<(), LvError> {
Some(touch_miso),
Some(touch_cs),
&DriverConfig::new(),
&SpiConfig::new().write_only(true).baudrate(2.MHz().into()),
&SpiConfig::new()
.write_only(true)
.baudrate(2.MHz().into()),
)
.unwrap(),
PinDriver::input(touch_irq).unwrap(),
@ -317,7 +342,7 @@ fn main() -> Result<(), LvError> {
cont.on_event(|_btn, event| {
if let Event::Pressed = event {
println!("lamp {:?}", lamp.name);
// println!("lamp {:?}", lamp.name);
// page = Page::LampCtrl(lamp);
brightness_slider.set_value(lerp_fixed(0, 100, lamp.brightness, 255).into(), AnimationState::OFF);

View File

@ -9,14 +9,22 @@ use std::{
use cstr_core::CString;
use display_interface_spi::SPIInterface;
use embedded_graphics_core::{draw_target::DrawTarget, prelude::Point};
use embedded_graphics_core::{
draw_target::DrawTarget,
prelude::Point,
};
use embedded_graphics_profiler_display::ProfilerDisplay;
use esp_idf_hal::spi::SpiSingleDeviceDriver;
use esp_idf_hal::{
delay::{self, Delay},
gpio::*,
peripherals::Peripherals,
spi::{config::DriverConfig, Dma, SpiConfig, SpiDeviceDriver},
spi::{
config::DriverConfig,
Dma,
SpiConfig,
SpiDeviceDriver,
},
units::FromValueType, // for converting 26MHz to value
};
use lvgl::{
@ -42,25 +50,26 @@ use lvgl::{
Widget,
};
use mipidsi::{
models::ILI9486Rgb565,
options::{ColorInversion, ColorOrder, Orientation, Rotation},
models::ILI9341Rgb565,
options::{
ColorInversion,
ColorOrder,
Orientation,
Rotation,
},
Builder,
};
use xpt2046::Xpt2046;
fn lerp_fixed(start: u8, end: u8, t: u8, max_t: u8) -> u8 {
let (start, end, t, max_t) = (start as u16, end as u16, t as u16, max_t as u16);
let (start, end, t, max_t) =
(start as u16, end as u16, t as u16, max_t as u16);
let t = t.min(max_t);
let result = start + ((end - start.min(end)) * t + (max_t / 2)) / max_t;
let result = start
+ ((end - start.min(end)) * t + (max_t / 2))
/ max_t;
result as u8
}
#[derive(Debug, Clone)]
struct Lamp {
pub name: heapless::String<64>,
pub on: bool,
pub brightness: u8,
}
struct AppData {
timer_start: Instant,
timer_set_duration: Duration,
@ -75,7 +84,9 @@ impl AppData {
Self {
timer_start: Instant::now(),
timer_set_duration: Duration::from_secs(10),
timer_remaining_duration: Duration::from_secs(10),
timer_remaining_duration: Duration::from_secs(
10,
),
timer_running: false,
timer_paused: false,
wattage_level: 5,
@ -106,7 +117,8 @@ impl AppData {
fn start_timer(&mut self) {
if !self.timer_paused {
self.timer_remaining_duration = self.timer_set_duration;
self.timer_remaining_duration =
self.timer_set_duration;
}
self.timer_start = Instant::now();
@ -126,7 +138,8 @@ impl AppData {
self.timer_start = Instant::now();
self.timer_paused = false;
self.timer_running = false;
self.timer_remaining_duration = self.timer_set_duration;
self.timer_remaining_duration =
self.timer_set_duration;
}
fn remaining(&self) -> Duration {
@ -154,11 +167,15 @@ impl AppData {
}
fn timer_finished(&self) -> bool {
self.timer_running && self.remaining() == Duration::from_secs(0)
self.timer_running
&& self.remaining() == Duration::from_secs(0)
}
fn set_wattage_level(&mut self, level: u8) {
assert!(level < 6, "wattage level cannot be over 6");
assert!(
level < 6,
"wattage level cannot be over 6"
);
self.wattage_level = level;
}
@ -180,8 +197,8 @@ fn main() -> Result<(), LvError> {
const VER_RES: u32 = 240;
const LINES: u32 = 20;
// It is necessary to call this function once. Otherwise some patches to the
// runtime implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
// It is necessary to call this function once. Otherwise
// some patches to the runtime implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();
// Initialize lvgl
@ -210,7 +227,9 @@ fn main() -> Result<(), LvError> {
Some(miso), // sdi
Some(cs), // cs
&DriverConfig::new().dma(Dma::Channel1(4096)),
&SpiConfig::new().write_only(true).baudrate(10.MHz().into()),
&SpiConfig::new()
.write_only(true)
.baudrate(10.MHz().into()),
)
.unwrap();
@ -223,8 +242,9 @@ fn main() -> Result<(), LvError> {
bklt.set_high().unwrap();
// Configuration for M5Stack Core Development Kit V1.0
// Puts display in landscape mode with the three buttons at the bottom of screen
// let mut m5stack_display = Builder::ili9342c_rgb565(di)
// Puts display in landscape mode with the three buttons
// at the bottom of screen let mut m5stack_display =
// Builder::ili9342c_rgb565(di)
// .with_display_size(320, 240)
// .with_color_order(ColorOrder::Bgr)
// .with_orientation(Orientation::Portrait(false))
@ -237,23 +257,25 @@ fn main() -> Result<(), LvError> {
// .with_orientation(Orientation::Portrait(false))
// .with_color_order(ColorOrder::Bgr)
// .with_invert_colors(true)
// .init(&mut Delay::new_default(), None::<PinDriver<AnyOutputPin, Output>>)
// .init(&mut Delay::new_default(),
// None::<PinDriver<AnyOutputPin, Output>>)
// .unwrap();
let raw_display = Builder::new(ILI9486Rgb565, di)
let raw_display = Builder::new(ILI9341Rgb565, di)
.orientation(Orientation {
rotation: Rotation::Deg90,
mirrored: true,
})
.color_order(ColorOrder::Bgr)
.invert_colors(ColorInversion::Inverted)
// .invert_colors(ColorInversion::Inverted)
.init(&mut Delay::new_default())
.unwrap();
let mut raw_display = ProfilerDisplay::new(raw_display);
// Stack size value - 20,000 for 10 lines, 40,000 for 20 lines
// let (touch_send, touch_recv) = channel();
// Stack size value - 20,000 for 10 lines, 40,000 for
// 20 lines let (touch_send, touch_recv) =
// channel();
let touch_irq = pins.gpio36;
let touch_mosi = pins.gpio32;
let touch_miso = pins.gpio39;
@ -268,7 +290,9 @@ fn main() -> Result<(), LvError> {
Some(touch_miso),
Some(touch_cs),
&DriverConfig::new(),
&SpiConfig::new().write_only(true).baudrate(2.MHz().into()),
&SpiConfig::new()
.write_only(true)
.baudrate(2.MHz().into()),
)
.unwrap(),
PinDriver::input(touch_irq).unwrap(),

View File

@ -8,14 +8,22 @@ use std::{
use cstr_core::CString;
use display_interface_spi::SPIInterface;
use embedded_graphics_core::{draw_target::DrawTarget, prelude::Point};
use embedded_graphics_core::{
draw_target::DrawTarget,
prelude::Point,
};
use embedded_graphics_profiler_display::ProfilerDisplay;
use esp_idf_hal::spi::SpiSingleDeviceDriver;
use esp_idf_hal::{
delay::{self, Delay},
gpio::*,
peripherals::Peripherals,
spi::{config::DriverConfig, Dma, SpiConfig, SpiDeviceDriver},
spi::{
config::DriverConfig,
Dma,
SpiConfig,
SpiDeviceDriver,
},
units::FromValueType, // for converting 26MHz to value
};
use lvgl::{
@ -38,8 +46,13 @@ use lvgl::{
Widget,
};
use mipidsi::{
models::ILI9486Rgb565,
options::{ColorInversion, ColorOrder, Orientation, Rotation},
models::ILI9341Rgb565,
options::{
ColorInversion,
ColorOrder,
Orientation,
Rotation,
},
Builder,
};
use xpt2046::Xpt2046;
@ -57,7 +70,9 @@ impl AppData {
Self {
timer_start: Instant::now(),
timer_set_duration: Duration::from_secs(10),
timer_remaining_duration: Duration::from_secs(10),
timer_remaining_duration: Duration::from_secs(
10,
),
timer_running: false,
timer_paused: false,
}
@ -87,7 +102,8 @@ impl AppData {
fn start_timer(&mut self) {
if !self.timer_paused {
self.timer_remaining_duration = self.timer_set_duration;
self.timer_remaining_duration =
self.timer_set_duration;
}
self.timer_start = Instant::now();
@ -107,7 +123,8 @@ impl AppData {
self.timer_start = Instant::now();
self.timer_paused = false;
self.timer_running = false;
self.timer_remaining_duration = self.timer_set_duration;
self.timer_remaining_duration =
self.timer_set_duration;
}
fn remaining(&self) -> Duration {
@ -135,7 +152,8 @@ impl AppData {
}
fn timer_finished(&self) -> bool {
self.timer_running && self.remaining() == Duration::from_secs(0)
self.timer_running
&& self.remaining() == Duration::from_secs(0)
}
}
@ -144,8 +162,8 @@ fn main() -> Result<(), LvError> {
const VER_RES: u32 = 240;
const LINES: u32 = 20;
// It is necessary to call this function once. Otherwise some patches to the
// runtime implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
// It is necessary to call this function once. Otherwise
// some patches to the runtime implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
esp_idf_svc::sys::link_patches();
// Initialize lvgl
@ -174,7 +192,9 @@ fn main() -> Result<(), LvError> {
Some(miso), // sdi
Some(cs), // cs
&DriverConfig::new().dma(Dma::Channel1(4096)),
&SpiConfig::new().write_only(true).baudrate(10.MHz().into()),
&SpiConfig::new()
.write_only(true)
.baudrate(10.MHz().into()),
)
.unwrap();
@ -187,8 +207,9 @@ fn main() -> Result<(), LvError> {
bklt.set_high().unwrap();
// Configuration for M5Stack Core Development Kit V1.0
// Puts display in landscape mode with the three buttons at the bottom of screen
// let mut m5stack_display = Builder::ili9342c_rgb565(di)
// Puts display in landscape mode with the three buttons
// at the bottom of screen let mut m5stack_display =
// Builder::ili9342c_rgb565(di)
// .with_display_size(320, 240)
// .with_color_order(ColorOrder::Bgr)
// .with_orientation(Orientation::Portrait(false))
@ -201,23 +222,25 @@ fn main() -> Result<(), LvError> {
// .with_orientation(Orientation::Portrait(false))
// .with_color_order(ColorOrder::Bgr)
// .with_invert_colors(true)
// .init(&mut Delay::new_default(), None::<PinDriver<AnyOutputPin, Output>>)
// .init(&mut Delay::new_default(),
// None::<PinDriver<AnyOutputPin, Output>>)
// .unwrap();
let raw_display = Builder::new(ILI9486Rgb565, di)
let raw_display = Builder::new(ILI9341Rgb565, di)
.orientation(Orientation {
rotation: Rotation::Deg90,
mirrored: true,
})
.color_order(ColorOrder::Bgr)
.invert_colors(ColorInversion::Inverted)
// .invert_colors(ColorInversion::Inverted)
.init(&mut Delay::new_default())
.unwrap();
let mut raw_display = ProfilerDisplay::new(raw_display);
// Stack size value - 20,000 for 10 lines, 40,000 for 20 lines
// let (touch_send, touch_recv) = channel();
// Stack size value - 20,000 for 10 lines, 40,000 for
// 20 lines let (touch_send, touch_recv) =
// channel();
let touch_irq = pins.gpio36;
let touch_mosi = pins.gpio32;
let touch_miso = pins.gpio39;
@ -232,7 +255,9 @@ fn main() -> Result<(), LvError> {
Some(touch_miso),
Some(touch_cs),
&DriverConfig::new(),
&SpiConfig::new().write_only(true).baudrate(2.MHz().into()),
&SpiConfig::new()
.write_only(true)
.baudrate(2.MHz().into()),
)
.unwrap(),
PinDriver::input(touch_irq).unwrap(),
@ -401,15 +426,18 @@ fn main() -> Result<(), LvError> {
let mut was_finished = false;
let mut last_rem_time: Duration = appdata.remaining() + Duration::from_millis(10);
let mut last_time = Instant::now();
loop {
let start_time = Instant::now();
let rem_time = appdata.remaining();
let val = CString::new(format!("{:02}:{:02}:{:03}",
rem_time.as_secs() / 60,
rem_time.as_secs() % 60,
rem_time.as_millis() % 1000)).unwrap();
if rem_time != last_rem_time {
let val = CString::new(format!("{:02}:{:02}:{:03}",
rem_time.as_secs() / 60,
rem_time.as_secs() % 60,
rem_time.as_millis() % 1000)).unwrap();
time.set_text(&val).unwrap();
time.set_text(&val).unwrap();
}
last_rem_time = rem_time;
@ -426,7 +454,9 @@ fn main() -> Result<(), LvError> {
// seconds
// delay::FreeRtos::delay_ms(1);
lvgl::tick_inc(Instant::now().duration_since(start_time));
let now_time = Instant::now();
lvgl::tick_inc(now_time.duration_since(last_time));
last_time = now_time;
let end_time = Instant::now();