Skip to content

Commit

Permalink
feat: implement custom hydrozoa::panic call
Browse files Browse the repository at this point in the history
  • Loading branch information
doinkythederp committed Dec 16, 2024
1 parent daa14a0 commit d357f47
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ lto = true
vex-sdk = "0.23.0"

[workspace.dependencies.vexide]
version = "0.5.0-rc.2"
version = "0.5.1"
default-features = false
features = ["core", "devices", "panic"]
features = ["core", "devices", "panic", "display_panics"]
70 changes: 70 additions & 0 deletions packages/runtime/src/platform.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
use alloc::string::String;
use core::ptr;

use vexide::{
devices::{
display::{Font, FontFamily, FontSize, Rect, RenderMode, Text},
math::Point2,
},
prelude::*,
};

const LINKED_FILE: *const u32 = 0x7800000 as *const u32;

pub fn read_user_program() -> &'static [u8] {
Expand All @@ -17,3 +26,64 @@ pub fn flush_serial() {
}
}
}

pub fn draw_error(display: &mut Display, msg: &str) {
const ERROR_BOX_MARGIN: i16 = 8;
const ERROR_BOX_PADDING: i16 = 8;
const LINE_HEIGHT: i16 = 16;
const LINE_MAX_WIDTH: usize = 56;

fn draw_text(screen: &mut Display, buffer: &str, line: i16) {
screen.draw_text(
&Text::new(
buffer,
Font::new(FontSize::SMALL, FontFamily::Monospace),
Point2 {
x: ERROR_BOX_MARGIN + ERROR_BOX_PADDING,
y: ERROR_BOX_MARGIN + ERROR_BOX_PADDING + (line * LINE_HEIGHT),
},
),
(255, 255, 255),
Some(Rgb::new(0, 0, 0)),
);
}

display.set_render_mode(RenderMode::Immediate);

let error_box_rect = Rect::new(
Point2 {
x: ERROR_BOX_MARGIN,
y: ERROR_BOX_MARGIN,
},
Point2 {
x: Display::HORIZONTAL_RESOLUTION - ERROR_BOX_MARGIN,
y: Display::VERTICAL_RESOLUTION - ERROR_BOX_MARGIN,
},
);

display.fill(&error_box_rect, (255, 0, 0));
display.stroke(&error_box_rect, (255, 255, 255));

let mut buffer = String::new();
let mut line: i16 = 0;

for (i, character) in msg.char_indices() {
if character == '\t' {
buffer += " ";
} else if !character.is_ascii_control() {
buffer.push(character);
}

if character == '\n' || ((buffer.len() % LINE_MAX_WIDTH == 0) && (i > 0)) {
draw_text(display, &buffer, line);
line += 1;
buffer.clear();
}
}

if !buffer.is_empty() {
draw_text(display, &buffer, line);

line += 1;
}
}
26 changes: 24 additions & 2 deletions packages/runtime/src/sdk.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
#![allow(non_snake_case)]

use alloc::{borrow::ToOwned, string::ToString};
use core::ffi::c_double;

use vex_sdk::{
V5MotorBrakeMode, V5MotorControlMode, V5MotorEncoderUnits, V5MotorGearset, V5_ControllerId,
V5_ControllerIndex, V5_DeviceType,
};
use wasm3::{store::AsContextMut, Instance, Store};
use vexide::{core::println, prelude::Display};
use wasm3::{error::Trap, store::AsContextMut, Instance, Store};

use crate::{teavm::get_cstring, Data};
use crate::{platform::draw_error, teavm::get_cstring, Data};

macro_rules! link {
($instance:ident, $store:ident, mod $module:literal {
Expand Down Expand Up @@ -67,6 +69,26 @@ macro_rules! printf_style {
}

pub fn link(store: &mut Store<Data>, instance: &mut Instance<Data>) -> anyhow::Result<()> {
instance.link_closure(
&mut *store,
"hydrozoa",
"panic",
|mut ctx, string: i32| -> Result<(), Trap> {
let string = get_cstring(&mut ctx, string);
let msg = string.to_string_lossy();

let mut display = unsafe { Display::new() };

draw_error(&mut display, msg.as_ref());

loop {
unsafe {
vex_sdk::vexTasksRun();
}
}
},
)?;

link!(instance, store, mod "vex" {
// Display
fn vexDisplayForegroundColor(col: u32);
Expand Down
2 changes: 1 addition & 1 deletion packages/startup/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ pub unsafe fn startup() {
);

// Initialize the heap allocator
vexide::core::allocator::vexos::init_heap();
vexide::core::allocator::init_heap();
}
}

0 comments on commit d357f47

Please sign in to comment.