wip calculator

This commit is contained in:
2025-09-18 11:50:28 -06:00
parent 1f1aa5ada5
commit e07700beea
3 changed files with 56 additions and 18 deletions

1
Cargo.lock generated
View File

@@ -232,6 +232,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"abi", "abi",
"embedded-graphics", "embedded-graphics",
"embedded-layout",
] ]
[[package]] [[package]]

View File

@@ -6,3 +6,4 @@ edition = "2024"
[dependencies] [dependencies]
abi = { path = "../../abi" } abi = { path = "../../abi" }
embedded-graphics = "0.8.1" embedded-graphics = "0.8.1"
embedded-layout = "0.4.2"

View File

@@ -11,12 +11,22 @@ use embedded_graphics::{
mono_font::{ mono_font::{
MonoTextStyle, MonoTextStyle,
ascii::{self, FONT_6X10}, ascii::{self, FONT_6X10},
iso_8859_1::FONT_10X20,
}, },
pixelcolor::Rgb565, pixelcolor::Rgb565,
prelude::{Primitive, RgbColor, Size}, prelude::{Primitive, RgbColor, Size},
primitives::{PrimitiveStyle, Rectangle}, primitives::{PrimitiveStyle, Rectangle},
text::{Alignment, Text}, text::{Alignment, Text},
}; };
use embedded_layout::{
align::{horizontal, vertical},
layout::linear::{
LinearLayout,
spacing::{DistributeFill, FixedMargin},
},
object_chain::Chain,
prelude::*,
};
#[panic_handler] #[panic_handler]
fn panic(info: &PanicInfo) -> ! { fn panic(info: &PanicInfo) -> ! {
@@ -28,13 +38,16 @@ fn panic(info: &PanicInfo) -> ! {
loop {} loop {}
} }
#[unsafe(no_mangle)]
pub extern "Rust" fn _start() {
main()
}
pub fn main() { pub fn main() {
print("Starting Async Calculator app"); print("Starting Async Calculator app");
let mut display = Display; let mut display = Display;
let character_style = MonoTextStyle::new(&FONT_6X10, Rgb565::RED); let mut input = vec!['e', 'x', 'p', 'r', ':', ' '];
let mut text = vec!['T', 'y', 'p', 'e'];
let mut dirty = true; let mut dirty = true;
let mut last_area: Option<Rectangle> = None; let mut last_area: Option<Rectangle> = None;
@@ -47,17 +60,45 @@ pub fn main() {
.unwrap(); .unwrap();
} }
let text = text.iter().cloned().collect::<String>(); let text = input.iter().cloned().collect::<String>();
let aligned_text = Text::with_alignment(
let expr = Text::new(
&text, &text,
display.bounding_box().center() + Point::new(0, 15), display.bounding_box().center(),
character_style, MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE),
Alignment::Center,
); );
last_area = Some(aligned_text.bounding_box()); let layout = LinearLayout::vertical(
Chain::new(Text::new(
"Calculator!",
Point::zero(),
MonoTextStyle::new(&FONT_10X20, Rgb565::WHITE),
))
.append(
LinearLayout::horizontal(Chain::new(expr).append(Text::new(
" = 901",
Point::zero(),
MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE),
)))
.with_spacing(DistributeFill(expr.size().width))
.arrange()
.align_to(
&display.bounding_box(),
horizontal::Center,
vertical::Center,
),
),
)
.with_spacing(DistributeFill(50))
.arrange()
.align_to(
&display.bounding_box(),
horizontal::Center,
vertical::Center,
);
aligned_text.draw(&mut display).unwrap(); last_area = Some(layout.bounds());
layout.draw(&mut display).unwrap();
dirty = false; dirty = false;
} }
@@ -65,13 +106,13 @@ pub fn main() {
if let Some(event) = get_key() { if let Some(event) = get_key() {
match event.key { match event.key {
KeyCode::Char(ch) => { KeyCode::Char(ch) => {
text.push(ch); input.push(ch);
} }
KeyCode::Del => { KeyCode::Del => {
text.clear(); input.clear();
} }
KeyCode::Backspace => { KeyCode::Backspace => {
text.pop(); input.pop();
} }
KeyCode::Esc => return, KeyCode::Esc => return,
_ => (), _ => (),
@@ -80,8 +121,3 @@ pub fn main() {
} }
} }
} }
#[unsafe(no_mangle)]
pub extern "Rust" fn _start() {
main()
}