Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
22aaa5b9f7 | ||
|
|
74a964985d | ||
|
|
35bd3ab945 | ||
|
|
33cb9a7a4a |
@@ -1,15 +0,0 @@
|
|||||||
[target.xtensa-esp32-none-elf]
|
|
||||||
runner = "espflash flash --monitor --chip esp32"
|
|
||||||
|
|
||||||
[env]
|
|
||||||
ESP_LOG="info"
|
|
||||||
|
|
||||||
[build]
|
|
||||||
rustflags = [
|
|
||||||
"-C", "link-arg=-nostartfiles",
|
|
||||||
]
|
|
||||||
|
|
||||||
target = "xtensa-esp32-none-elf"
|
|
||||||
|
|
||||||
[unstable]
|
|
||||||
build-std = ["alloc", "core"]
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
stack-size-threshold = 1024
|
|
||||||
+1
-26
@@ -1,26 +1 @@
|
|||||||
# will have compiled files and executables
|
/target
|
||||||
debug/
|
|
||||||
target/
|
|
||||||
|
|
||||||
# Editor configuration
|
|
||||||
.vscode/
|
|
||||||
.zed/
|
|
||||||
.helix/
|
|
||||||
.nvim.lua
|
|
||||||
|
|
||||||
# These are backup files generated by rustfmt
|
|
||||||
**/*.rs.bk
|
|
||||||
|
|
||||||
# MSVC Windows builds of rustc generate these, which store debugging information
|
|
||||||
*.pdb
|
|
||||||
|
|
||||||
# RustRover
|
|
||||||
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
||||||
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
||||||
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
||||||
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
||||||
#.idea/
|
|
||||||
|
|
||||||
|
|
||||||
# Ignore .DS_Store file in mac
|
|
||||||
**/.DS_Store
|
|
||||||
|
|||||||
Generated
-1761
File diff suppressed because it is too large
Load Diff
+1
-63
@@ -1,68 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
edition = "2024"
|
|
||||||
name = "creaturedex"
|
name = "creaturedex"
|
||||||
rust-version = "1.88"
|
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
[[bin]]
|
|
||||||
name = "creaturedex"
|
|
||||||
path = "./src/bin/main.rs"
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
esp-hal = { version = "~1.1.0", features = ["esp32", "log-04", "unstable"] }
|
|
||||||
|
|
||||||
esp-rtos = { version = "0.3.0", features = [
|
|
||||||
"esp-alloc",
|
|
||||||
"esp-radio",
|
|
||||||
"esp32",
|
|
||||||
"log-04",
|
|
||||||
] }
|
|
||||||
|
|
||||||
esp-bootloader-esp-idf = { version = "0.5.0", features = ["esp32", "log-04"] }
|
|
||||||
log = "0.4.27"
|
|
||||||
|
|
||||||
critical-section = "1.2.0"
|
|
||||||
embedded-io = "0.7.1"
|
|
||||||
esp-alloc = "0.10.0"
|
|
||||||
esp-println = { version = "0.17.0", features = ["esp32", "log-04"] }
|
|
||||||
esp-radio = { version = "0.18.0", features = [
|
|
||||||
"esp-alloc",
|
|
||||||
"esp32",
|
|
||||||
"log-04",
|
|
||||||
"unstable",
|
|
||||||
"wifi",
|
|
||||||
] }
|
|
||||||
smoltcp = { version = "0.13.0", default-features = false, features = [
|
|
||||||
"log",
|
|
||||||
"medium-ethernet",
|
|
||||||
"multicast",
|
|
||||||
"proto-dhcpv4",
|
|
||||||
"proto-dns",
|
|
||||||
"proto-ipv4",
|
|
||||||
"socket-dns",
|
|
||||||
"socket-icmp",
|
|
||||||
"socket-raw",
|
|
||||||
"socket-tcp",
|
|
||||||
"socket-udp",
|
|
||||||
] }
|
|
||||||
display-interface-spi = "0.5.0"
|
|
||||||
ili9341 = "0.6.0"
|
|
||||||
embedded-hal = "1.0.0"
|
|
||||||
embedded-hal-bus = "0.3.0"
|
|
||||||
embedded-graphics = "0.8.2"
|
|
||||||
embedded-graphics-core = "0.4.1"
|
|
||||||
pn532 = "0.5.0"
|
|
||||||
ndef = { version = "0.5.0", path = "./ndef-rs" }
|
|
||||||
|
|
||||||
|
|
||||||
# For fine tuning these settings, please refer to https://doc.rust-lang.org/cargo/reference/profiles.html
|
|
||||||
[profile.dev]
|
|
||||||
# The default debug profile is too slow and too big for resource-constrained devices.
|
|
||||||
# Always build with some optimizations enabled.
|
|
||||||
opt-level = "s"
|
|
||||||
|
|
||||||
[profile.release]
|
|
||||||
codegen-units = 1 # LLVM can perform better optimizations using a single thread
|
|
||||||
debug = 2 # prefer slower builds but better debugging experience
|
|
||||||
lto = 'fat'
|
|
||||||
opt-level = 's'
|
|
||||||
|
|||||||
@@ -1,70 +0,0 @@
|
|||||||
fn main() {
|
|
||||||
linker_be_nice();
|
|
||||||
// make sure linkall.x is the last linker script (otherwise might cause problems with flip-link)
|
|
||||||
println!("cargo:rustc-link-arg=-Tlinkall.x");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn linker_be_nice() {
|
|
||||||
let args: Vec<String> = std::env::args().collect();
|
|
||||||
if args.len() > 1 {
|
|
||||||
let kind = &args[1];
|
|
||||||
let what = &args[2];
|
|
||||||
|
|
||||||
match kind.as_str() {
|
|
||||||
"undefined-symbol" => match what.as_str() {
|
|
||||||
what if what.starts_with("_defmt_") => {
|
|
||||||
eprintln!();
|
|
||||||
eprintln!(
|
|
||||||
"💡 `defmt` not found - make sure `defmt.x` is added as a linker script and you have included `use defmt_rtt as _;`"
|
|
||||||
);
|
|
||||||
eprintln!();
|
|
||||||
}
|
|
||||||
"_stack_start" => {
|
|
||||||
eprintln!();
|
|
||||||
eprintln!("💡 Is the linker script `linkall.x` missing?");
|
|
||||||
eprintln!();
|
|
||||||
}
|
|
||||||
what if what.starts_with("esp_rtos_") => {
|
|
||||||
eprintln!();
|
|
||||||
eprintln!(
|
|
||||||
"💡 `esp-radio` has no scheduler enabled. Make sure you have initialized `esp-rtos` or provided an external scheduler."
|
|
||||||
);
|
|
||||||
eprintln!();
|
|
||||||
}
|
|
||||||
"embedded_test_linker_file_not_added_to_rustflags" => {
|
|
||||||
eprintln!();
|
|
||||||
eprintln!(
|
|
||||||
"💡 `embedded-test` not found - make sure `embedded-test.x` is added as a linker script for tests"
|
|
||||||
);
|
|
||||||
eprintln!();
|
|
||||||
}
|
|
||||||
"free"
|
|
||||||
| "malloc"
|
|
||||||
| "calloc"
|
|
||||||
| "get_free_internal_heap_size"
|
|
||||||
| "malloc_internal"
|
|
||||||
| "realloc_internal"
|
|
||||||
| "calloc_internal"
|
|
||||||
| "free_internal" => {
|
|
||||||
eprintln!();
|
|
||||||
eprintln!(
|
|
||||||
"💡 Did you forget the `esp-alloc` dependency or didn't enable the `compat` feature on it?"
|
|
||||||
);
|
|
||||||
eprintln!();
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
},
|
|
||||||
// we don't have anything helpful for "missing-lib" yet
|
|
||||||
_ => {
|
|
||||||
std::process::exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::process::exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
println!(
|
|
||||||
"cargo:rustc-link-arg=-Wl,--error-handling-script={}",
|
|
||||||
std::env::current_exe().unwrap().display()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
{
|
|
||||||
"version": 1,
|
|
||||||
"editor": "wokwi",
|
|
||||||
"parts": [
|
|
||||||
{ "type": "board-esp32-s3-devkitc-1", "id": "esp1", "top": 67.2, "left": -263.96, "attrs": {} },
|
|
||||||
{
|
|
||||||
"type": "board-ili9341-cap-touch",
|
|
||||||
"id": "lcd1",
|
|
||||||
"top": -191.24,
|
|
||||||
"left": -0.38,
|
|
||||||
"attrs": {}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "wokwi-led",
|
|
||||||
"id": "led1",
|
|
||||||
"top": -82.89,
|
|
||||||
"left": 196.26,
|
|
||||||
"attrs": { "color": "red" }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "wokwi-resistor",
|
|
||||||
"id": "r1",
|
|
||||||
"top": 183.86,
|
|
||||||
"left": 154.06,
|
|
||||||
"attrs": { "value": "220" }
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"connections": [
|
|
||||||
[ "lcd1:VCC", "esp1:3V3", "red", [ "v19.2", "h-57.6", "v-105.6", "h-124.95" ] ],
|
|
||||||
[ "lcd1:GND", "esp1:GND.2", "black", [ "v28.8", "h-76.8", "v-76.8" ] ],
|
|
||||||
[ "lcd1:CS", "esp1:11", "orange", [ "v0" ] ],
|
|
||||||
[ "lcd1:D/C", "esp1:12", "gold", [ "v0" ] ],
|
|
||||||
[ "lcd1:RESET", "esp1:10", "brown", [ "v0" ] ],
|
|
||||||
[ "lcd1:MOSI", "esp1:35", "violet", [ "v105.6", "h-124.8", "v-144" ] ],
|
|
||||||
[ "lcd1:SCK", "esp1:36", "white", [ "v115.2", "h-144", "v-86.4" ] ],
|
|
||||||
[ "esp1:37", "lcd1:MISO", "magenta", [ "h57.6", "v105.6", "h172.8" ] ],
|
|
||||||
[ "esp1:22", "lcd1:SCL", "yellow", [ "h48", "v163.2", "h192" ] ],
|
|
||||||
[ "esp1:21", "lcd1:SDA", "cyan", [ "h38.4", "v144", "h211.2" ] ],
|
|
||||||
[ "r1:2", "led1:A", "green", [ "h8.4", "v-230.4" ] ],
|
|
||||||
[ "esp1:GND.2", "led1:C", "black", [ "v0", "h86.4", "v76.8", "h278.4" ] ],
|
|
||||||
[ "esp1:17", "r1:1", "green", [ "h0" ] ]
|
|
||||||
],
|
|
||||||
"dependencies": {}
|
|
||||||
}
|
|
||||||
Binary file not shown.
+4
-3
@@ -1,5 +1,5 @@
|
|||||||
BASE_MODEL_SCALE = [1.1, 1, 1.1];
|
BASE_MODEL_SCALE = [1,1,1];
|
||||||
BASE_MODEL_TRANSLATE = [-0.4, 0, 16.978];
|
BASE_MODEL_TRANSLATE = [0,0,16];
|
||||||
|
|
||||||
module main_display(){
|
module main_display(){
|
||||||
// Outer dimensions, height currently unknown
|
// Outer dimensions, height currently unknown
|
||||||
@@ -30,7 +30,8 @@ module tertiary_display()
|
|||||||
}
|
}
|
||||||
|
|
||||||
module battery() {
|
module battery() {
|
||||||
// 18650
|
// 18650 with holder
|
||||||
|
// cube([75, 20, 20]);
|
||||||
rotate([0,90,0]) cylinder(d=18, h=65);
|
rotate([0,90,0]) cylinder(d=18, h=65);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ module left_half_components() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module left_half() {
|
module left_half() {
|
||||||
translate(BASE_MODEL_TRANSLATE) scale(BASE_MODEL_SCALE) import(file = "left-half.3mf");
|
translate(BASE_MODEL_TRANSLATE) import(file = "left-half.3mf");
|
||||||
}
|
}
|
||||||
|
|
||||||
left_half();
|
left_half();
|
||||||
|
|||||||
+2
-105
@@ -1,115 +1,12 @@
|
|||||||
include <common.scad>
|
include <common.scad>
|
||||||
|
|
||||||
$fa = 1;
|
|
||||||
$fs = 0.4;
|
|
||||||
|
|
||||||
module right_half_components() {
|
module right_half_components() {
|
||||||
cube([1,1,1]);
|
cube([1,1,1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
module right_half_orig() {
|
|
||||||
translate(BASE_MODEL_TRANSLATE) scale(BASE_MODEL_SCALE) import(file = "right-half.3mf");
|
|
||||||
}
|
|
||||||
// #right_half_orig();
|
|
||||||
|
|
||||||
module right_half_door_only() {
|
|
||||||
translate(BASE_MODEL_TRANSLATE+[11.3, -85, -17]) scale(BASE_MODEL_SCALE) import(file = "right-half_door-only.stl");
|
|
||||||
}
|
|
||||||
// #right_half_door_only();
|
|
||||||
|
|
||||||
module battery_spacer(cable_cube_additional_length=0) {
|
|
||||||
length = 106;
|
|
||||||
rotate([0, 90, 90]) cylinder(d=18.5, h=length);
|
|
||||||
cable_cube = 8.25;
|
|
||||||
translate([0, 0, -cable_cube]) cube([cable_cube, length+cable_cube_additional_length, cable_cube]);
|
|
||||||
}
|
|
||||||
// !battery_spacer();
|
|
||||||
|
|
||||||
module battery_contact()
|
|
||||||
{
|
|
||||||
base_height = 4;
|
|
||||||
silver_wire_d = 1.15;
|
|
||||||
|
|
||||||
module base_body() {
|
|
||||||
rotate([90, 0, 0])
|
|
||||||
resize([0, base_height, 0], auto=[false, true, false])
|
|
||||||
scale(0.99)
|
|
||||||
battery_spacer();
|
|
||||||
}
|
|
||||||
|
|
||||||
module contact_spring()
|
|
||||||
{
|
|
||||||
spring_width = 11;
|
|
||||||
translate([-2, -spring_width/2, 0])
|
|
||||||
difference() {
|
|
||||||
scale([1.75, 1, 1]) rotate([-90, 0, 0]) cylinder(d=10, h=spring_width);
|
|
||||||
scale([1.75, 1, 1.5]) rotate([-90, 0, 0]) cylinder(d=6, h=spring_width);
|
|
||||||
translate([-100, 0, -50]) cube(100);
|
|
||||||
translate([-50, 0, -100]) cube(100);
|
|
||||||
translate([0, spring_width/3-silver_wire_d, 0]) cube([silver_wire_d, silver_wire_d, 50]);
|
|
||||||
translate([0, spring_width*2/3, 0]) cube([silver_wire_d, silver_wire_d, 50]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
module silver_wire_channels()
|
|
||||||
{
|
|
||||||
translate([7.3, 0, -6])
|
|
||||||
rotate([-15, 0, 0])
|
|
||||||
cylinder(d=silver_wire_d, h=15);
|
|
||||||
|
|
||||||
translate([7.3, 0, -6])
|
|
||||||
rotate([15, 0, 0])
|
|
||||||
cylinder(d=silver_wire_d, h=15);
|
|
||||||
}
|
|
||||||
|
|
||||||
translate([0, 0, base_height])
|
|
||||||
rotate([0, 0, 45])
|
|
||||||
contact_spring();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
difference() {
|
|
||||||
base_body();
|
|
||||||
translate([0, 0, base_height])
|
|
||||||
rotate([0, 0, 45])
|
|
||||||
silver_wire_channels();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// !battery_contact();
|
|
||||||
|
|
||||||
module right_half_hinge_only() {
|
|
||||||
module original() {
|
|
||||||
translate(BASE_MODEL_TRANSLATE+[0.34, -12.3, -6]) scale(BASE_MODEL_SCALE) import(file = "right-half_hinge-only.3mf", convexity=3);
|
|
||||||
translate(BASE_MODEL_TRANSLATE+[5.92, -12.3, -11.48]) scale(BASE_MODEL_SCALE) import(file = "right-half_connector.3mf", convexity=3);
|
|
||||||
}
|
|
||||||
|
|
||||||
module cable_room() {
|
|
||||||
translate([3.5, 0, -3.5])
|
|
||||||
rotate([-10, 80, 90]) cylinder(d=8, h=20);
|
|
||||||
translate([0, 18, 0])
|
|
||||||
rotate([0, 90, 90]) cylinder(d=8, h=20);
|
|
||||||
translate([0, 0, -9.5])
|
|
||||||
cube([20, 16.5, 4]);
|
|
||||||
}
|
|
||||||
|
|
||||||
module base_model() {
|
|
||||||
difference() {
|
|
||||||
original();
|
|
||||||
translate([-20, -108.3, -10]) cube(40);
|
|
||||||
translate([0, -85, 11])
|
|
||||||
battery_spacer(18);
|
|
||||||
translate([0, 22.5, 11])
|
|
||||||
cable_room();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
base_model();
|
|
||||||
// translate([0, -40, 11]) rotate([0, 0, 90]) battery();
|
|
||||||
}
|
|
||||||
// right_half_hinge_only();
|
|
||||||
|
|
||||||
module right_half() {
|
module right_half() {
|
||||||
right_half_door_only();
|
translate(BASE_MODEL_TRANSLATE) import(file = "right-half.3mf");
|
||||||
right_half_hinge_only();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
right_half();
|
right_half();
|
||||||
// #right_half_components();
|
#right_half_components();
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,2 +0,0 @@
|
|||||||
[toolchain]
|
|
||||||
channel = "esp"
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
{
|
|
||||||
pkgs ? import <nixpkgs> { },
|
|
||||||
}:
|
|
||||||
let
|
|
||||||
host-triple = "x86_64-unknown-linux-gnu";
|
|
||||||
gcc-arch = "xtensa-esp-elf";
|
|
||||||
gcc-release = "16.1.0_20260609";
|
|
||||||
toolchain-pkg = pkgs.stdenv.mkDerivation rec {
|
|
||||||
pname = "esp32-xtensa-rust-toolchain";
|
|
||||||
version = "1.97.0.0";
|
|
||||||
srcs = [
|
|
||||||
(pkgs.fetchurl {
|
|
||||||
url = "https://github.com/esp-rs/rust-build/releases/download/v${version}/rust-${version}-${host-triple}.tar.xz";
|
|
||||||
hash = "sha256-qZv+5pIh6f9thjiPaBHuaIzUBeagQAo80XhOjUY+nZk=";
|
|
||||||
})
|
|
||||||
(pkgs.fetchurl {
|
|
||||||
url =
|
|
||||||
let
|
|
||||||
gcc-file = "${gcc-arch}-${gcc-release}-x86_64-linux-gnu.tar.xz";
|
|
||||||
in
|
|
||||||
"https://github.com/espressif/crosstool-NG/releases/download/esp-${gcc-release}/${gcc-file}";
|
|
||||||
hash = "sha256-9wh1Lrs1yrIfGEsldBFO0Rh2GdsO24pLqRPAbKL6Z14=";
|
|
||||||
})
|
|
||||||
(pkgs.fetchurl {
|
|
||||||
url = "https://github.com/esp-rs/rust-build/releases/download/v${version}/rust-src-${version}.tar.xz";
|
|
||||||
hash = "sha256-Vo1oi5+PMy7E0EZXVE+tI+mc4R6ebPWDWXnmiijGi3M=";
|
|
||||||
})
|
|
||||||
# TODO maybe I need clang but so far it works without https://github.com/esp-rs/espup/blob/main/src/toolchain/llvm.rs
|
|
||||||
];
|
|
||||||
sourceRoot = ".";
|
|
||||||
nativeBuildInputs = [ pkgs.autoPatchelfHook ];
|
|
||||||
buildInputs = [
|
|
||||||
pkgs.zlib
|
|
||||||
pkgs.stdenv.cc.cc.lib
|
|
||||||
];
|
|
||||||
installPhase = ''
|
|
||||||
runHook preInstall
|
|
||||||
|
|
||||||
patchShebangs --build rust-nightly-x86_64-unknown-linux-gnu/install.sh rust-src-nightly/install.sh
|
|
||||||
|
|
||||||
rust-nightly-x86_64-unknown-linux-gnu/install.sh --destdir="$out" --prefix="" --without=rust-docs-json-preview,rust-docs --disable-ldconfig
|
|
||||||
rust-src-nightly/install.sh --destdir="$out" --prefix="" --disable-ldconfig
|
|
||||||
|
|
||||||
cp -pr --reflink=auto -- xtensa-esp-elf "$out";
|
|
||||||
# ensure linker is in PATH
|
|
||||||
ln -s $out/xtensa-esp-elf/bin/* "$out/bin/"
|
|
||||||
|
|
||||||
runHook postInstall
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
in
|
|
||||||
(pkgs.mkShell {
|
|
||||||
packages = with pkgs; [
|
|
||||||
espflash
|
|
||||||
toolchain-pkg
|
|
||||||
];
|
|
||||||
})
|
|
||||||
-284
@@ -1,284 +0,0 @@
|
|||||||
#![no_std]
|
|
||||||
#![no_main]
|
|
||||||
#![deny(
|
|
||||||
clippy::mem_forget,
|
|
||||||
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
|
|
||||||
holding buffers for the duration of a data transfer."
|
|
||||||
)]
|
|
||||||
#![deny(clippy::large_stack_frames)]
|
|
||||||
|
|
||||||
use core::convert::Infallible;
|
|
||||||
|
|
||||||
use alloc::vec::Vec;
|
|
||||||
use display_interface_spi::SPIInterface;
|
|
||||||
use embedded_graphics::Drawable;
|
|
||||||
use embedded_graphics::mono_font::MonoTextStyle;
|
|
||||||
use embedded_graphics::mono_font::ascii::FONT_6X10;
|
|
||||||
use embedded_graphics::pixelcolor::Rgb565;
|
|
||||||
use embedded_graphics::prelude::Point;
|
|
||||||
use embedded_graphics::prelude::RgbColor;
|
|
||||||
use embedded_graphics::text::Text;
|
|
||||||
use embedded_graphics_core::draw_target::DrawTarget;
|
|
||||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
|
||||||
use esp_hal::clock::CpuClock;
|
|
||||||
use esp_hal::delay::Delay;
|
|
||||||
use esp_hal::gpio::Level;
|
|
||||||
use esp_hal::gpio::Output;
|
|
||||||
use esp_hal::gpio::OutputConfig;
|
|
||||||
use esp_hal::main;
|
|
||||||
use esp_hal::spi::{
|
|
||||||
Mode,
|
|
||||||
master::{Config, Spi},
|
|
||||||
};
|
|
||||||
use esp_hal::time::{Duration, Instant};
|
|
||||||
use esp_hal::timer::timg::TimerGroup;
|
|
||||||
use esp_println::dbg;
|
|
||||||
use ili9341::Ili9341;
|
|
||||||
use ili9341::Orientation;
|
|
||||||
use log::{error, info};
|
|
||||||
use pn532::i2c::I2CInterface;
|
|
||||||
use pn532::nb;
|
|
||||||
|
|
||||||
#[panic_handler]
|
|
||||||
fn panic(panic_info: &core::panic::PanicInfo) -> ! {
|
|
||||||
error!("{}", panic_info);
|
|
||||||
loop {}
|
|
||||||
}
|
|
||||||
|
|
||||||
extern crate alloc;
|
|
||||||
|
|
||||||
// This creates a default app-descriptor required by the esp-idf bootloader.
|
|
||||||
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
|
|
||||||
esp_bootloader_esp_idf::esp_app_desc!();
|
|
||||||
|
|
||||||
#[allow(
|
|
||||||
clippy::large_stack_frames,
|
|
||||||
reason = "it's not unusual to allocate larger buffers etc. in main"
|
|
||||||
)]
|
|
||||||
#[main]
|
|
||||||
fn main() -> ! {
|
|
||||||
// generator version: 1.3.0
|
|
||||||
// generator parameters: --chip esp32s3 -o esp32s3-wroom-1-octal-psram -o unstable-hal -o alloc -o wifi -o log -o wokwi -o neovim -o vscode -o esp
|
|
||||||
|
|
||||||
esp_println::logger::init_logger_from_env();
|
|
||||||
|
|
||||||
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
|
|
||||||
let peripherals = esp_hal::init(config);
|
|
||||||
|
|
||||||
let sck = peripherals.GPIO18;
|
|
||||||
let mosi = peripherals.GPIO23;
|
|
||||||
let miso = peripherals.GPIO19;
|
|
||||||
let cs_main = peripherals.GPIO5;
|
|
||||||
let dc = peripherals.GPIO17;
|
|
||||||
let reset_main = peripherals.GPIO16;
|
|
||||||
|
|
||||||
let sda = peripherals.GPIO21;
|
|
||||||
let scl = peripherals.GPIO22;
|
|
||||||
|
|
||||||
esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 73744);
|
|
||||||
|
|
||||||
let timg0 = TimerGroup::new(peripherals.TIMG0);
|
|
||||||
let sw_interrupt =
|
|
||||||
esp_hal::interrupt::software::SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
|
|
||||||
esp_rtos::start(timg0.timer0, sw_interrupt.software_interrupt0);
|
|
||||||
let (mut _wifi_controller, _interfaces) =
|
|
||||||
esp_radio::wifi::new(peripherals.WIFI, Default::default())
|
|
||||||
.expect("Failed to initialize Wi-Fi controller");
|
|
||||||
|
|
||||||
let spi_bus = Spi::new(
|
|
||||||
peripherals.SPI2,
|
|
||||||
Config::default()
|
|
||||||
.with_frequency(Rate::from_khz(100))
|
|
||||||
.with_mode(Mode::_0),
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
.with_sck(sck)
|
|
||||||
.with_mosi(mosi)
|
|
||||||
.with_miso(miso);
|
|
||||||
|
|
||||||
let cs_main = Output::new(cs_main, Level::High, OutputConfig::default());
|
|
||||||
|
|
||||||
let mut delay = Delay::new();
|
|
||||||
let spi_dev = ExclusiveDevice::new(spi_bus, cs_main, delay).unwrap();
|
|
||||||
let reset_main = Output::new(reset_main, Level::High, OutputConfig::default());
|
|
||||||
let spi_dc = Output::new(dc, Level::High, OutputConfig::default());
|
|
||||||
let iface = SPIInterface::new(spi_dev, spi_dc);
|
|
||||||
|
|
||||||
let mut display = Ili9341::new(
|
|
||||||
iface,
|
|
||||||
reset_main,
|
|
||||||
&mut delay,
|
|
||||||
Orientation::Landscape,
|
|
||||||
ili9341::DisplaySize240x320,
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// display.clear(Rgb565::BLACK).unwrap();
|
|
||||||
|
|
||||||
let style = MonoTextStyle::new(&FONT_6X10, Rgb565::WHITE);
|
|
||||||
Text::new("Hello, ESP32!", Point::new(20, 100), style)
|
|
||||||
.draw(&mut display)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
use pn532::{Pn532, Request, requests::SAMMode};
|
|
||||||
|
|
||||||
// spi is a struct implementing embedded_hal::spi::SpiDevice
|
|
||||||
// timer is a struct implementing pn532::CountDown
|
|
||||||
|
|
||||||
use esp_hal::{
|
|
||||||
i2c::master::{Config as I2cConfig, I2c},
|
|
||||||
time::Rate,
|
|
||||||
};
|
|
||||||
|
|
||||||
let config = I2cConfig::default().with_frequency(Rate::from_khz(20));
|
|
||||||
let i2c = I2c::new(peripherals.I2C0, config)
|
|
||||||
.expect("Failed to init I2C")
|
|
||||||
.with_sda(sda)
|
|
||||||
.with_scl(scl);
|
|
||||||
|
|
||||||
let mut pn532: Pn532<_, _, 32> = Pn532::new(I2CInterface { i2c }, TimerWrapper::new());
|
|
||||||
|
|
||||||
let ms_50 = Duration::from_millis(50);
|
|
||||||
let ms_1000 = Duration::from_millis(1000);
|
|
||||||
|
|
||||||
let delay_start = Instant::now();
|
|
||||||
while delay_start.elapsed() < Duration::from_millis(500) {}
|
|
||||||
|
|
||||||
let mut data: Vec<u8> = Vec::with_capacity(858);
|
|
||||||
|
|
||||||
if let Err(e) = pn532.process(
|
|
||||||
&Request::sam_configuration(SAMMode::Normal, false),
|
|
||||||
0,
|
|
||||||
Duration::from_millis(5000),
|
|
||||||
) {
|
|
||||||
error!("Could not initialize PN532: {e:?}")
|
|
||||||
} else {
|
|
||||||
info!("Successfully init'ed PN532");
|
|
||||||
}
|
|
||||||
|
|
||||||
loop {
|
|
||||||
info!("Hello world!");
|
|
||||||
|
|
||||||
match pn532.process(
|
|
||||||
&Request::INLIST_ONE_ISO_A_TARGET,
|
|
||||||
23,
|
|
||||||
Duration::from_secs(10),
|
|
||||||
) {
|
|
||||||
Ok(uid) => {
|
|
||||||
info!("uid = {uid:?}");
|
|
||||||
for i in (0x04..=230) {
|
|
||||||
let mut parse_result = Err(());
|
|
||||||
while parse_result.is_err() {
|
|
||||||
let Ok(result) = pn532.process(&Request::ntag_read(i), 17, ms_1000) else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
parse_result = parse_nfc(i, result);
|
|
||||||
}
|
|
||||||
let bytes = parse_result.unwrap();
|
|
||||||
data.extend(bytes);
|
|
||||||
}
|
|
||||||
info!("Full data: {data:02x?}");
|
|
||||||
match ndef::Message::try_from(&data[4..4 + 883]) {
|
|
||||||
Ok(m) => info!("Parsed: {m:02x?}"),
|
|
||||||
Err(e) => info!("Failed to parse: {e:?}"),
|
|
||||||
}
|
|
||||||
data.clear();
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
info!("No NFC card found: {e:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let delay_start = Instant::now();
|
|
||||||
while delay_start.elapsed() < Duration::from_millis(500) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.1.0/examples
|
|
||||||
}
|
|
||||||
|
|
||||||
fn parse_nfc(iteration: u8, bytes: &[u8]) -> Result<[u8; 4], ()> {
|
|
||||||
let success = bytes.first().is_some_and(|x| *x == 0);
|
|
||||||
if success {
|
|
||||||
// info!("page {i}: {:02x?}", result);
|
|
||||||
let mut arr = [0; 4];
|
|
||||||
arr.copy_from_slice(&bytes[1..5]);
|
|
||||||
Ok(arr)
|
|
||||||
} else {
|
|
||||||
info!("err {iteration}: {:02x?}", bytes);
|
|
||||||
Err(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct TimerWrapper {
|
|
||||||
// TODO Is that timer accurate enough?
|
|
||||||
start: Instant,
|
|
||||||
duration: Duration,
|
|
||||||
counter: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TimerWrapper {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
start: Instant::now(),
|
|
||||||
duration: Duration::ZERO,
|
|
||||||
counter: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl pn532::CountDown for TimerWrapper {
|
|
||||||
type Time = Duration;
|
|
||||||
|
|
||||||
fn start<T>(&mut self, timeout: T)
|
|
||||||
where
|
|
||||||
T: Into<Self::Time>,
|
|
||||||
{
|
|
||||||
// without this debug log, it works worse: stuck in NFC card search without ever returning.
|
|
||||||
self.start = Instant::now();
|
|
||||||
self.duration = timeout.into();
|
|
||||||
self.counter = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wait(&mut self) -> nb::Result<(), Infallible> {
|
|
||||||
let elapsed = self.start.elapsed();
|
|
||||||
self.counter += 1;
|
|
||||||
if self.counter.is_multiple_of(1) {
|
|
||||||
// info!("Elapsed: {elapsed}");
|
|
||||||
}
|
|
||||||
if elapsed >= self.duration {
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(nb::Error::WouldBlock)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
struct TimerWrapper<'a, T>
|
|
||||||
where
|
|
||||||
T: esp_hal::DriverMode,
|
|
||||||
{
|
|
||||||
timer: esp_hal::timer::PeriodicTimer<'a, T>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, TIM> pn532::CountDown for TimerWrapper<'a, TIM>
|
|
||||||
where
|
|
||||||
TIM: esp_hal::DriverMode,
|
|
||||||
{
|
|
||||||
type Time = Duration;
|
|
||||||
fn start<T>(&mut self, timeout: T)
|
|
||||||
where
|
|
||||||
T: Into<Self::Time>,
|
|
||||||
{
|
|
||||||
self.timer.start(timeout.into()).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn wait(&mut self) -> nb::Result<(), Infallible> {
|
|
||||||
// convert nb::Result<(), void::Void> to nb::Result<(), Infallible>
|
|
||||||
match self.timer.wait() {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
Err(_) => Err(nb::Error::WouldBlock),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
@@ -1 +0,0 @@
|
|||||||
#![no_std]
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#ifndef WOKWI_API_H
|
||||||
|
#define WOKWI_API_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef int32_t pin_t;
|
||||||
|
|
||||||
|
#define NO_PIN ((pin_t)-1)
|
||||||
|
|
||||||
|
enum pin_mode {
|
||||||
|
INPUT = 0,
|
||||||
|
OUTPUT = 1,
|
||||||
|
INPUT_PULLUP = 2,
|
||||||
|
INPUT_PULLDOWN = 3,
|
||||||
|
ANALOG = 4,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *user_data;
|
||||||
|
uint32_t edge;
|
||||||
|
void (*pin_change)(void *user_data, pin_t pin, uint32_t value);
|
||||||
|
} pin_watch_config_t;
|
||||||
|
|
||||||
|
extern __attribute__((import_name("pinInit"))) pin_t pin_init(const char *name, uint32_t mode);
|
||||||
|
extern __attribute__((import_name("pinWatch"))) bool pin_watch(pin_t pin, const pin_watch_config_t *config);
|
||||||
|
extern __attribute__((import_name("pinWatchStop"))) void pin_watch_stop(pin_t pin);
|
||||||
|
extern __attribute__((export_name("chipInit"))) void chip_init(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
SOURCES = src/main.c
|
||||||
|
TARGET = dist/chip.wasm
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
|
|
||||||
|
all: $(TARGET) dist/chip.json
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf dist
|
||||||
|
|
||||||
|
dist:
|
||||||
|
mkdir -p dist
|
||||||
|
|
||||||
|
$(TARGET): dist $(SOURCES) wokwi-api.h
|
||||||
|
podman run --rm -u root -v "$(CURDIR)":/src:Z -w /src docker.io/wokwi/builder-clang-wasm:latest \
|
||||||
|
clang --target=wasm32-unknown-wasi --sysroot /opt/wasi-libc -nostartfiles -Wl,--import-memory -Wl,--export-table -Wl,--no-entry -Werror -o $(TARGET) $(SOURCES)
|
||||||
|
|
||||||
|
dist/chip.json: dist chip.json
|
||||||
|
cp chip.json dist
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "Main Display",
|
||||||
|
"author": "GitHub Copilot",
|
||||||
|
"display": { "width": 240, "height": 320 },
|
||||||
|
"pins": ["VCC", "GND", "SCK", "MOSI", "CS", "DC", "RESET", "LED"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "Main Display",
|
||||||
|
"author": "GitHub Copilot",
|
||||||
|
"display": { "width": 240, "height": 320 },
|
||||||
|
"pins": ["VCC", "GND", "SCK", "MOSI", "CS", "DC", "RESET", "LED"]
|
||||||
|
}
|
||||||
BIN
Binary file not shown.
@@ -0,0 +1,243 @@
|
|||||||
|
#include "../wokwi-api.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
buffer_t framebuffer;
|
||||||
|
uint32_t width;
|
||||||
|
uint32_t height;
|
||||||
|
spi_dev_t spi;
|
||||||
|
pin_t cs_pin;
|
||||||
|
pin_t dc_pin;
|
||||||
|
pin_t rst_pin;
|
||||||
|
pin_t led_pin;
|
||||||
|
uint8_t spi_buffer[128];
|
||||||
|
uint8_t current_cmd;
|
||||||
|
uint8_t expected_params;
|
||||||
|
uint8_t param_index;
|
||||||
|
uint8_t params[4];
|
||||||
|
bool memory_write;
|
||||||
|
bool color_high_pending;
|
||||||
|
uint8_t color_high;
|
||||||
|
uint16_t col_start;
|
||||||
|
uint16_t col_end;
|
||||||
|
uint16_t row_start;
|
||||||
|
uint16_t row_end;
|
||||||
|
uint16_t cursor_col;
|
||||||
|
uint16_t cursor_row;
|
||||||
|
} main_display_state_t;
|
||||||
|
|
||||||
|
static main_display_state_t *chip;
|
||||||
|
|
||||||
|
static uint16_t clamp_u16(uint16_t value, uint16_t limit) {
|
||||||
|
return value > limit ? limit : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void draw_pixel(uint16_t x, uint16_t y, uint16_t color565) {
|
||||||
|
if (x >= chip->width || y >= chip->height) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t pixel[4];
|
||||||
|
pixel[0] = (uint8_t)(((color565 >> 11) & 0x1F) << 3);
|
||||||
|
pixel[1] = (uint8_t)(((color565 >> 5) & 0x3F) << 2);
|
||||||
|
pixel[2] = (uint8_t)((color565 & 0x1F) << 3);
|
||||||
|
pixel[3] = 255;
|
||||||
|
buffer_write(chip->framebuffer, ((y * chip->width) + x) * 4, pixel, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void fill_screen(uint16_t color565) {
|
||||||
|
for (uint32_t y = 0; y < chip->height; ++y) {
|
||||||
|
for (uint32_t x = 0; x < chip->width; ++x) {
|
||||||
|
draw_pixel((uint16_t)x, (uint16_t)y, color565);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void reset_state(void) {
|
||||||
|
chip->current_cmd = 0;
|
||||||
|
chip->expected_params = 0;
|
||||||
|
chip->param_index = 0;
|
||||||
|
chip->memory_write = false;
|
||||||
|
chip->color_high_pending = false;
|
||||||
|
chip->col_start = 0;
|
||||||
|
chip->col_end = (uint16_t)(chip->width - 1);
|
||||||
|
chip->row_start = 0;
|
||||||
|
chip->row_end = (uint16_t)(chip->height - 1);
|
||||||
|
chip->cursor_col = 0;
|
||||||
|
chip->cursor_row = 0;
|
||||||
|
fill_screen(0x0000);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void advance_pixel_cursor(void) {
|
||||||
|
if (chip->cursor_col < chip->col_end) {
|
||||||
|
++chip->cursor_col;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
chip->cursor_col = chip->col_start;
|
||||||
|
if (chip->cursor_row < chip->row_end) {
|
||||||
|
++chip->cursor_row;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
chip->cursor_row = chip->row_start;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void apply_params(void) {
|
||||||
|
switch (chip->current_cmd) {
|
||||||
|
case 0x2A:
|
||||||
|
chip->col_start = clamp_u16((uint16_t)((chip->params[0] << 8) | chip->params[1]), (uint16_t)(chip->width - 1));
|
||||||
|
chip->col_end = clamp_u16((uint16_t)((chip->params[2] << 8) | chip->params[3]), (uint16_t)(chip->width - 1));
|
||||||
|
if (chip->col_end < chip->col_start) {
|
||||||
|
chip->col_end = chip->col_start;
|
||||||
|
}
|
||||||
|
chip->cursor_col = chip->col_start;
|
||||||
|
break;
|
||||||
|
case 0x2B:
|
||||||
|
chip->row_start = clamp_u16((uint16_t)((chip->params[0] << 8) | chip->params[1]), (uint16_t)(chip->height - 1));
|
||||||
|
chip->row_end = clamp_u16((uint16_t)((chip->params[2] << 8) | chip->params[3]), (uint16_t)(chip->height - 1));
|
||||||
|
if (chip->row_end < chip->row_start) {
|
||||||
|
chip->row_end = chip->row_start;
|
||||||
|
}
|
||||||
|
chip->cursor_row = chip->row_start;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
chip->current_cmd = 0;
|
||||||
|
chip->expected_params = 0;
|
||||||
|
chip->param_index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void process_command(uint8_t command) {
|
||||||
|
chip->current_cmd = command;
|
||||||
|
chip->param_index = 0;
|
||||||
|
chip->expected_params = 0;
|
||||||
|
chip->memory_write = false;
|
||||||
|
|
||||||
|
switch (command) {
|
||||||
|
case 0x01:
|
||||||
|
reset_state();
|
||||||
|
break;
|
||||||
|
case 0x2A:
|
||||||
|
case 0x2B:
|
||||||
|
chip->expected_params = 4;
|
||||||
|
break;
|
||||||
|
case 0x2C:
|
||||||
|
chip->memory_write = true;
|
||||||
|
chip->cursor_col = chip->col_start;
|
||||||
|
chip->cursor_row = chip->row_start;
|
||||||
|
chip->color_high_pending = false;
|
||||||
|
break;
|
||||||
|
case 0x36:
|
||||||
|
case 0x3A:
|
||||||
|
chip->expected_params = 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void handle_data_byte(uint8_t data) {
|
||||||
|
if (chip->current_cmd != 0 && chip->expected_params > 0) {
|
||||||
|
chip->params[chip->param_index++] = data;
|
||||||
|
if (chip->param_index >= chip->expected_params) {
|
||||||
|
apply_params();
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!chip->memory_write) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!chip->color_high_pending) {
|
||||||
|
chip->color_high = data;
|
||||||
|
chip->color_high_pending = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t color565 = (uint16_t)((chip->color_high << 8) | data);
|
||||||
|
draw_pixel(chip->cursor_col, chip->cursor_row, color565);
|
||||||
|
advance_pixel_cursor();
|
||||||
|
chip->color_high_pending = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void chip_spi_done(void *user_data, uint8_t *buffer, uint32_t count) {
|
||||||
|
(void)user_data;
|
||||||
|
|
||||||
|
for (uint32_t i = 0; i < count; ++i) {
|
||||||
|
if (pin_read(chip->dc_pin) == 0) {
|
||||||
|
process_command(buffer[i]);
|
||||||
|
} else {
|
||||||
|
handle_data_byte(buffer[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pin_read(chip->cs_pin) == 0) {
|
||||||
|
spi_start(chip->spi, chip->spi_buffer, sizeof(chip->spi_buffer));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void chip_cs_changed(void *user_data, pin_t pin, uint32_t value) {
|
||||||
|
(void)user_data;
|
||||||
|
(void)pin;
|
||||||
|
|
||||||
|
if (value == 0) {
|
||||||
|
spi_start(chip->spi, chip->spi_buffer, sizeof(chip->spi_buffer));
|
||||||
|
} else {
|
||||||
|
spi_stop(chip->spi);
|
||||||
|
chip->memory_write = false;
|
||||||
|
chip->color_high_pending = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void chip_reset_changed(void *user_data, pin_t pin, uint32_t value) {
|
||||||
|
(void)user_data;
|
||||||
|
(void)pin;
|
||||||
|
|
||||||
|
if (value == 0) {
|
||||||
|
reset_state();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void chip_init(void) {
|
||||||
|
chip = (main_display_state_t *)malloc(sizeof(main_display_state_t));
|
||||||
|
chip->framebuffer = framebuffer_init(&chip->width, &chip->height);
|
||||||
|
chip->cs_pin = pin_init("CS", INPUT);
|
||||||
|
chip->dc_pin = pin_init("DC", INPUT);
|
||||||
|
chip->rst_pin = pin_init("RESET", INPUT_PULLUP);
|
||||||
|
chip->led_pin = pin_init("LED", INPUT);
|
||||||
|
|
||||||
|
const spi_config_t spi_config = {
|
||||||
|
.user_data = chip,
|
||||||
|
.sck = pin_init("SCK", INPUT),
|
||||||
|
.mosi = pin_init("MOSI", INPUT),
|
||||||
|
.miso = NO_PIN,
|
||||||
|
.mode = 0,
|
||||||
|
.done = chip_spi_done,
|
||||||
|
};
|
||||||
|
|
||||||
|
chip->spi = spi_init(&spi_config);
|
||||||
|
|
||||||
|
const pin_watch_config_t cs_watch = {
|
||||||
|
.user_data = chip,
|
||||||
|
.edge = BOTH,
|
||||||
|
.pin_change = chip_cs_changed,
|
||||||
|
};
|
||||||
|
pin_watch(chip->cs_pin, &cs_watch);
|
||||||
|
|
||||||
|
const pin_watch_config_t reset_watch = {
|
||||||
|
.user_data = chip,
|
||||||
|
.edge = BOTH,
|
||||||
|
.pin_change = chip_reset_changed,
|
||||||
|
};
|
||||||
|
pin_watch(chip->rst_pin, &reset_watch);
|
||||||
|
|
||||||
|
pin_mode(chip->led_pin, OUTPUT_HIGH);
|
||||||
|
reset_state();
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
#ifndef WOKWI_API_H
|
||||||
|
#define WOKWI_API_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef int32_t pin_t;
|
||||||
|
#define NO_PIN ((pin_t)-1)
|
||||||
|
|
||||||
|
enum pin_mode {
|
||||||
|
INPUT = 0,
|
||||||
|
OUTPUT = 1,
|
||||||
|
INPUT_PULLUP = 2,
|
||||||
|
INPUT_PULLDOWN = 3,
|
||||||
|
ANALOG = 4,
|
||||||
|
OUTPUT_LOW = 16,
|
||||||
|
OUTPUT_HIGH = 17,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum edge {
|
||||||
|
RISING = 1,
|
||||||
|
FALLING = 2,
|
||||||
|
BOTH = 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *user_data;
|
||||||
|
uint32_t edge;
|
||||||
|
void (*pin_change)(void *user_data, pin_t pin, uint32_t value);
|
||||||
|
} pin_watch_config_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *user_data;
|
||||||
|
pin_t sck;
|
||||||
|
pin_t mosi;
|
||||||
|
pin_t miso;
|
||||||
|
uint32_t mode;
|
||||||
|
void (*done)(void *user_data, uint8_t *buffer, uint32_t count);
|
||||||
|
uint32_t reserved[8];
|
||||||
|
} spi_config_t;
|
||||||
|
|
||||||
|
typedef uint32_t spi_dev_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *user_data;
|
||||||
|
uint32_t address;
|
||||||
|
pin_t scl;
|
||||||
|
pin_t sda;
|
||||||
|
bool (*connect)(void *user_data, uint32_t address, bool read);
|
||||||
|
uint8_t (*read)(void *user_data);
|
||||||
|
bool (*write)(void *user_data, uint8_t data);
|
||||||
|
void (*disconnect)(void *user_data);
|
||||||
|
uint32_t reserved[8];
|
||||||
|
} i2c_config_t;
|
||||||
|
|
||||||
|
typedef uint32_t i2c_dev_t;
|
||||||
|
|
||||||
|
typedef uint32_t buffer_t;
|
||||||
|
|
||||||
|
extern __attribute__((export_name("chipInit"))) void chip_init(void);
|
||||||
|
extern __attribute__((import_name("pinInit"))) pin_t pin_init(const char *name, uint32_t mode);
|
||||||
|
extern __attribute__((import_name("pinWatch"))) bool pin_watch(pin_t pin, const pin_watch_config_t *config);
|
||||||
|
extern __attribute__((import_name("pinWatchStop"))) void pin_watch_stop(pin_t pin);
|
||||||
|
extern __attribute__((import_name("pinMode"))) void pin_mode(pin_t pin, uint32_t value);
|
||||||
|
extern __attribute__((import_name("pinRead"))) uint32_t pin_read(pin_t pin);
|
||||||
|
extern __attribute__((import_name("pinWrite"))) void pin_write(pin_t pin, uint32_t value);
|
||||||
|
extern __attribute__((import_name("spiInit"))) spi_dev_t spi_init(const spi_config_t *config);
|
||||||
|
extern __attribute__((import_name("spiStart"))) void spi_start(const spi_dev_t spi, uint8_t *buffer, uint32_t count);
|
||||||
|
extern __attribute__((import_name("spiStop"))) void spi_stop(const spi_dev_t spi);
|
||||||
|
extern __attribute__((import_name("i2cInit"))) i2c_dev_t i2c_init(const i2c_config_t *config);
|
||||||
|
extern __attribute__((import_name("framebufferInit"))) buffer_t framebuffer_init(uint32_t *pixel_width, uint32_t *pixel_height);
|
||||||
|
extern __attribute__((import_name("bufferWrite"))) void buffer_write(buffer_t buffer, uint32_t offset, void *data, uint32_t data_len);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
SOURCES = src/main.c
|
||||||
|
TARGET = dist/chip.wasm
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
|
|
||||||
|
all: $(TARGET) dist/chip.json
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf dist
|
||||||
|
|
||||||
|
dist:
|
||||||
|
mkdir -p dist
|
||||||
|
|
||||||
|
$(TARGET): dist $(SOURCES) wokwi-api.h
|
||||||
|
podman run --rm -u root -v "$(CURDIR)":/src:Z -w /src docker.io/wokwi/builder-clang-wasm:latest \
|
||||||
|
clang --target=wasm32-unknown-wasi --sysroot /opt/wasi-libc -nostartfiles -Wl,--import-memory -Wl,--export-table -Wl,--no-entry -Werror -o $(TARGET) $(SOURCES)
|
||||||
|
|
||||||
|
dist/chip.json: dist chip.json
|
||||||
|
cp chip.json dist
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"name": "PN532 NFC Reader",
|
||||||
|
"author": "GitHub Copilot",
|
||||||
|
"pins": ["VCC", "GND", "SCL", "SDA", "IRQ", "RSTPDN", "SS", "MOSI", "MISO", "SCK"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"name": "PN532 NFC Reader",
|
||||||
|
"author": "GitHub Copilot",
|
||||||
|
"pins": ["VCC", "GND", "SCL", "SDA", "IRQ", "RSTPDN", "SS", "MOSI", "MISO", "SCK"]
|
||||||
|
}
|
||||||
BIN
Binary file not shown.
@@ -0,0 +1,127 @@
|
|||||||
|
#include "../wokwi-api.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
i2c_dev_t i2c;
|
||||||
|
spi_dev_t spi;
|
||||||
|
pin_t irq_pin;
|
||||||
|
pin_t rst_pin;
|
||||||
|
pin_t ss_pin;
|
||||||
|
uint8_t spi_buffer[16];
|
||||||
|
uint8_t ack_frame[6];
|
||||||
|
bool ready;
|
||||||
|
} nfc_reader_state_t;
|
||||||
|
|
||||||
|
static nfc_reader_state_t *chip;
|
||||||
|
|
||||||
|
static void update_irq(void) {
|
||||||
|
pin_write(chip->irq_pin, chip->ready ? 0 : 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void load_ack_frame(void) {
|
||||||
|
chip->ack_frame[0] = 0x00;
|
||||||
|
chip->ack_frame[1] = 0x00;
|
||||||
|
chip->ack_frame[2] = 0xFF;
|
||||||
|
chip->ack_frame[3] = 0x00;
|
||||||
|
chip->ack_frame[4] = 0xFF;
|
||||||
|
chip->ack_frame[5] = 0x00;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_spi_done(void *user_data, uint8_t *buffer, uint32_t count) {
|
||||||
|
(void)user_data;
|
||||||
|
(void)buffer;
|
||||||
|
(void)count;
|
||||||
|
load_ack_frame();
|
||||||
|
if (pin_read(chip->ss_pin) == 0) {
|
||||||
|
spi_start(chip->spi, chip->spi_buffer, sizeof(chip->spi_buffer));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_ss_changed(void *user_data, pin_t pin, uint32_t value) {
|
||||||
|
(void)user_data;
|
||||||
|
(void)pin;
|
||||||
|
if (value == 0) {
|
||||||
|
spi_start(chip->spi, chip->spi_buffer, sizeof(chip->spi_buffer));
|
||||||
|
} else {
|
||||||
|
spi_stop(chip->spi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_rst_changed(void *user_data, pin_t pin, uint32_t value) {
|
||||||
|
(void)user_data;
|
||||||
|
(void)pin;
|
||||||
|
chip->ready = value != 0;
|
||||||
|
update_irq();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool on_i2c_connect(void *user_data, uint32_t address, bool read) {
|
||||||
|
(void)user_data;
|
||||||
|
(void)address;
|
||||||
|
(void)read;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t on_i2c_read(void *user_data) {
|
||||||
|
(void)user_data;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool on_i2c_write(void *user_data, uint8_t data) {
|
||||||
|
(void)user_data;
|
||||||
|
(void)data;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_i2c_disconnect(void *user_data) {
|
||||||
|
(void)user_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
void chip_init(void) {
|
||||||
|
chip = (nfc_reader_state_t *)malloc(sizeof(nfc_reader_state_t));
|
||||||
|
chip->irq_pin = pin_init("IRQ", INPUT);
|
||||||
|
chip->rst_pin = pin_init("RSTPDN", INPUT_PULLUP);
|
||||||
|
chip->ss_pin = pin_init("SS", INPUT_PULLUP);
|
||||||
|
|
||||||
|
const spi_config_t spi_config = {
|
||||||
|
.user_data = chip,
|
||||||
|
.sck = pin_init("SCK", INPUT),
|
||||||
|
.mosi = pin_init("MOSI", INPUT),
|
||||||
|
.miso = pin_init("MISO", INPUT),
|
||||||
|
.mode = 0,
|
||||||
|
.done = on_spi_done,
|
||||||
|
};
|
||||||
|
chip->spi = spi_init(&spi_config);
|
||||||
|
|
||||||
|
const pin_watch_config_t ss_watch = {
|
||||||
|
.user_data = chip,
|
||||||
|
.edge = BOTH,
|
||||||
|
.pin_change = on_ss_changed,
|
||||||
|
};
|
||||||
|
pin_watch(chip->ss_pin, &ss_watch);
|
||||||
|
|
||||||
|
const pin_watch_config_t rst_watch = {
|
||||||
|
.user_data = chip,
|
||||||
|
.edge = BOTH,
|
||||||
|
.pin_change = on_rst_changed,
|
||||||
|
};
|
||||||
|
pin_watch(chip->rst_pin, &rst_watch);
|
||||||
|
|
||||||
|
const i2c_config_t i2c_config = {
|
||||||
|
.user_data = chip,
|
||||||
|
.address = 0x24,
|
||||||
|
.scl = pin_init("SCL", INPUT_PULLUP),
|
||||||
|
.sda = pin_init("SDA", INPUT_PULLUP),
|
||||||
|
.connect = on_i2c_connect,
|
||||||
|
.read = on_i2c_read,
|
||||||
|
.write = on_i2c_write,
|
||||||
|
.disconnect = on_i2c_disconnect,
|
||||||
|
};
|
||||||
|
chip->i2c = i2c_init(&i2c_config);
|
||||||
|
|
||||||
|
chip->ready = true;
|
||||||
|
load_ack_frame();
|
||||||
|
update_irq();
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
#ifndef WOKWI_API_H
|
||||||
|
#define WOKWI_API_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef int32_t pin_t;
|
||||||
|
#define NO_PIN ((pin_t)-1)
|
||||||
|
|
||||||
|
enum pin_mode {
|
||||||
|
INPUT = 0,
|
||||||
|
OUTPUT = 1,
|
||||||
|
INPUT_PULLUP = 2,
|
||||||
|
INPUT_PULLDOWN = 3,
|
||||||
|
ANALOG = 4,
|
||||||
|
OUTPUT_LOW = 16,
|
||||||
|
OUTPUT_HIGH = 17,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum edge {
|
||||||
|
RISING = 1,
|
||||||
|
FALLING = 2,
|
||||||
|
BOTH = 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *user_data;
|
||||||
|
uint32_t edge;
|
||||||
|
void (*pin_change)(void *user_data, pin_t pin, uint32_t value);
|
||||||
|
} pin_watch_config_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *user_data;
|
||||||
|
pin_t sck;
|
||||||
|
pin_t mosi;
|
||||||
|
pin_t miso;
|
||||||
|
uint32_t mode;
|
||||||
|
void (*done)(void *user_data, uint8_t *buffer, uint32_t count);
|
||||||
|
uint32_t reserved[8];
|
||||||
|
} spi_config_t;
|
||||||
|
|
||||||
|
typedef uint32_t spi_dev_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *user_data;
|
||||||
|
uint32_t address;
|
||||||
|
pin_t scl;
|
||||||
|
pin_t sda;
|
||||||
|
bool (*connect)(void *user_data, uint32_t address, bool read);
|
||||||
|
uint8_t (*read)(void *user_data);
|
||||||
|
bool (*write)(void *user_data, uint8_t data);
|
||||||
|
void (*disconnect)(void *user_data);
|
||||||
|
uint32_t reserved[8];
|
||||||
|
} i2c_config_t;
|
||||||
|
|
||||||
|
typedef uint32_t i2c_dev_t;
|
||||||
|
|
||||||
|
typedef uint32_t buffer_t;
|
||||||
|
|
||||||
|
extern __attribute__((export_name("chipInit"))) void chip_init(void);
|
||||||
|
extern __attribute__((import_name("pinInit"))) pin_t pin_init(const char *name, uint32_t mode);
|
||||||
|
extern __attribute__((import_name("pinWatch"))) bool pin_watch(pin_t pin, const pin_watch_config_t *config);
|
||||||
|
extern __attribute__((import_name("pinWatchStop"))) void pin_watch_stop(pin_t pin);
|
||||||
|
extern __attribute__((import_name("pinMode"))) void pin_mode(pin_t pin, uint32_t value);
|
||||||
|
extern __attribute__((import_name("pinRead"))) uint32_t pin_read(pin_t pin);
|
||||||
|
extern __attribute__((import_name("pinWrite"))) void pin_write(pin_t pin, uint32_t value);
|
||||||
|
extern __attribute__((import_name("spiInit"))) spi_dev_t spi_init(const spi_config_t *config);
|
||||||
|
extern __attribute__((import_name("spiStart"))) void spi_start(const spi_dev_t spi, uint8_t *buffer, uint32_t count);
|
||||||
|
extern __attribute__((import_name("spiStop"))) void spi_stop(const spi_dev_t spi);
|
||||||
|
extern __attribute__((import_name("i2cInit"))) i2c_dev_t i2c_init(const i2c_config_t *config);
|
||||||
|
extern __attribute__((import_name("framebufferInit"))) buffer_t framebuffer_init(uint32_t *pixel_width, uint32_t *pixel_height);
|
||||||
|
extern __attribute__((import_name("bufferWrite"))) void buffer_write(buffer_t buffer, uint32_t offset, void *data, uint32_t data_len);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
SOURCES = src/main.c
|
||||||
|
TARGET = dist/chip.wasm
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
|
|
||||||
|
all: $(TARGET) dist/chip.json
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf dist
|
||||||
|
|
||||||
|
dist:
|
||||||
|
mkdir -p dist
|
||||||
|
|
||||||
|
$(TARGET): dist $(SOURCES) wokwi-api.h
|
||||||
|
podman run --rm -u root -v "$(CURDIR)":/src:Z -w /src docker.io/wokwi/builder-clang-wasm:latest \
|
||||||
|
clang --target=wasm32-unknown-wasi --sysroot /opt/wasi-libc -nostartfiles -Wl,--import-memory -Wl,--export-table -Wl,--no-entry -Werror -o $(TARGET) $(SOURCES)
|
||||||
|
|
||||||
|
dist/chip.json: dist chip.json
|
||||||
|
cp chip.json dist
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "Secondary Display",
|
||||||
|
"author": "GitHub Copilot",
|
||||||
|
"display": { "width": 84, "height": 48 },
|
||||||
|
"pins": ["VCC", "LIGHT", "GND", "CLK", "DIN", "CE", "DC", "RST"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "Secondary Display",
|
||||||
|
"author": "GitHub Copilot",
|
||||||
|
"display": { "width": 84, "height": 48 },
|
||||||
|
"pins": ["VCC", "LIGHT", "GND", "CLK", "DIN", "CE", "DC", "RST"]
|
||||||
|
}
|
||||||
BIN
Binary file not shown.
@@ -0,0 +1,171 @@
|
|||||||
|
#include "../wokwi-api.h"
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
buffer_t framebuffer;
|
||||||
|
uint32_t width;
|
||||||
|
uint32_t height;
|
||||||
|
spi_dev_t spi;
|
||||||
|
pin_t ce_pin;
|
||||||
|
pin_t dc_pin;
|
||||||
|
pin_t rst_pin;
|
||||||
|
uint8_t spi_buffer[64];
|
||||||
|
uint8_t x;
|
||||||
|
uint8_t y;
|
||||||
|
bool extended_commands;
|
||||||
|
} secondary_display_state_t;
|
||||||
|
|
||||||
|
static secondary_display_state_t *chip;
|
||||||
|
|
||||||
|
static void set_pixel(uint8_t x, uint8_t y, bool on) {
|
||||||
|
if (x >= chip->width || y >= chip->height) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t pixel[4] = {0, 0, 0, 255};
|
||||||
|
if (on) {
|
||||||
|
pixel[0] = 0x20;
|
||||||
|
pixel[1] = 0x90;
|
||||||
|
pixel[2] = 0x20;
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer_write(chip->framebuffer, ((uint32_t)y * chip->width + x) * 4, pixel, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void clear_display(void) {
|
||||||
|
for (uint32_t y = 0; y < chip->height; ++y) {
|
||||||
|
for (uint32_t x = 0; x < chip->width; ++x) {
|
||||||
|
set_pixel((uint8_t)x, (uint8_t)y, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chip->x = 0;
|
||||||
|
chip->y = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_data(uint8_t data) {
|
||||||
|
for (uint8_t bit = 0; bit < 8; ++bit) {
|
||||||
|
set_pixel(chip->x, (uint8_t)(chip->y * 8 + bit), (data >> bit) & 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
++chip->x;
|
||||||
|
if (chip->x >= chip->width) {
|
||||||
|
chip->x = 0;
|
||||||
|
++chip->y;
|
||||||
|
if (chip->y >= (chip->height / 8)) {
|
||||||
|
chip->y = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void write_command(uint8_t command) {
|
||||||
|
if (command == 0x20) {
|
||||||
|
chip->extended_commands = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command == 0x21) {
|
||||||
|
chip->extended_commands = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command == 0x0C) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command == 0x08) {
|
||||||
|
clear_display();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command == 0x80 || (command & 0x80) == 0x80) {
|
||||||
|
chip->x = (uint8_t)(command & 0x7F);
|
||||||
|
if (chip->x >= chip->width) {
|
||||||
|
chip->x = 0;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command == 0x40 || (command & 0x40) == 0x40) {
|
||||||
|
chip->y = (uint8_t)(command & 0x07);
|
||||||
|
if (chip->y >= (chip->height / 8)) {
|
||||||
|
chip->y = 0;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (command == 0x01) {
|
||||||
|
clear_display();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void chip_spi_done(void *user_data, uint8_t *buffer, uint32_t count) {
|
||||||
|
(void)user_data;
|
||||||
|
for (uint32_t i = 0; i < count; ++i) {
|
||||||
|
if (pin_read(chip->dc_pin) == 0) {
|
||||||
|
write_command(buffer[i]);
|
||||||
|
} else {
|
||||||
|
write_data(buffer[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pin_read(chip->ce_pin) == 0) {
|
||||||
|
spi_start(chip->spi, chip->spi_buffer, sizeof(chip->spi_buffer));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void chip_ce_changed(void *user_data, pin_t pin, uint32_t value) {
|
||||||
|
(void)user_data;
|
||||||
|
(void)pin;
|
||||||
|
|
||||||
|
if (value == 0) {
|
||||||
|
spi_start(chip->spi, chip->spi_buffer, sizeof(chip->spi_buffer));
|
||||||
|
} else {
|
||||||
|
spi_stop(chip->spi);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void chip_rst_changed(void *user_data, pin_t pin, uint32_t value) {
|
||||||
|
(void)user_data;
|
||||||
|
(void)pin;
|
||||||
|
|
||||||
|
if (value == 0) {
|
||||||
|
clear_display();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void chip_init(void) {
|
||||||
|
chip = (secondary_display_state_t *)malloc(sizeof(secondary_display_state_t));
|
||||||
|
chip->framebuffer = framebuffer_init(&chip->width, &chip->height);
|
||||||
|
chip->ce_pin = pin_init("CE", INPUT);
|
||||||
|
chip->dc_pin = pin_init("DC", INPUT);
|
||||||
|
chip->rst_pin = pin_init("RST", INPUT_PULLUP);
|
||||||
|
|
||||||
|
const spi_config_t spi_config = {
|
||||||
|
.user_data = chip,
|
||||||
|
.sck = pin_init("CLK", INPUT),
|
||||||
|
.mosi = pin_init("DIN", INPUT),
|
||||||
|
.miso = NO_PIN,
|
||||||
|
.mode = 0,
|
||||||
|
.done = chip_spi_done,
|
||||||
|
};
|
||||||
|
chip->spi = spi_init(&spi_config);
|
||||||
|
|
||||||
|
const pin_watch_config_t ce_watch = {
|
||||||
|
.user_data = chip,
|
||||||
|
.edge = BOTH,
|
||||||
|
.pin_change = chip_ce_changed,
|
||||||
|
};
|
||||||
|
pin_watch(chip->ce_pin, &ce_watch);
|
||||||
|
|
||||||
|
const pin_watch_config_t rst_watch = {
|
||||||
|
.user_data = chip,
|
||||||
|
.edge = BOTH,
|
||||||
|
.pin_change = chip_rst_changed,
|
||||||
|
};
|
||||||
|
pin_watch(chip->rst_pin, &rst_watch);
|
||||||
|
|
||||||
|
clear_display();
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
#ifndef WOKWI_API_H
|
||||||
|
#define WOKWI_API_H
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef int32_t pin_t;
|
||||||
|
#define NO_PIN ((pin_t)-1)
|
||||||
|
|
||||||
|
enum pin_mode {
|
||||||
|
INPUT = 0,
|
||||||
|
OUTPUT = 1,
|
||||||
|
INPUT_PULLUP = 2,
|
||||||
|
INPUT_PULLDOWN = 3,
|
||||||
|
ANALOG = 4,
|
||||||
|
OUTPUT_LOW = 16,
|
||||||
|
OUTPUT_HIGH = 17,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum edge {
|
||||||
|
RISING = 1,
|
||||||
|
FALLING = 2,
|
||||||
|
BOTH = 3,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *user_data;
|
||||||
|
uint32_t edge;
|
||||||
|
void (*pin_change)(void *user_data, pin_t pin, uint32_t value);
|
||||||
|
} pin_watch_config_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *user_data;
|
||||||
|
pin_t sck;
|
||||||
|
pin_t mosi;
|
||||||
|
pin_t miso;
|
||||||
|
uint32_t mode;
|
||||||
|
void (*done)(void *user_data, uint8_t *buffer, uint32_t count);
|
||||||
|
uint32_t reserved[8];
|
||||||
|
} spi_config_t;
|
||||||
|
|
||||||
|
typedef uint32_t spi_dev_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *user_data;
|
||||||
|
uint32_t address;
|
||||||
|
pin_t scl;
|
||||||
|
pin_t sda;
|
||||||
|
bool (*connect)(void *user_data, uint32_t address, bool read);
|
||||||
|
uint8_t (*read)(void *user_data);
|
||||||
|
bool (*write)(void *user_data, uint8_t data);
|
||||||
|
void (*disconnect)(void *user_data);
|
||||||
|
uint32_t reserved[8];
|
||||||
|
} i2c_config_t;
|
||||||
|
|
||||||
|
typedef uint32_t i2c_dev_t;
|
||||||
|
|
||||||
|
typedef uint32_t buffer_t;
|
||||||
|
|
||||||
|
extern __attribute__((export_name("chipInit"))) void chip_init(void);
|
||||||
|
extern __attribute__((import_name("pinInit"))) pin_t pin_init(const char *name, uint32_t mode);
|
||||||
|
extern __attribute__((import_name("pinWatch"))) bool pin_watch(pin_t pin, const pin_watch_config_t *config);
|
||||||
|
extern __attribute__((import_name("pinWatchStop"))) void pin_watch_stop(pin_t pin);
|
||||||
|
extern __attribute__((import_name("pinMode"))) void pin_mode(pin_t pin, uint32_t value);
|
||||||
|
extern __attribute__((import_name("pinRead"))) uint32_t pin_read(pin_t pin);
|
||||||
|
extern __attribute__((import_name("pinWrite"))) void pin_write(pin_t pin, uint32_t value);
|
||||||
|
extern __attribute__((import_name("spiInit"))) spi_dev_t spi_init(const spi_config_t *config);
|
||||||
|
extern __attribute__((import_name("spiStart"))) void spi_start(const spi_dev_t spi, uint8_t *buffer, uint32_t count);
|
||||||
|
extern __attribute__((import_name("spiStop"))) void spi_stop(const spi_dev_t spi);
|
||||||
|
extern __attribute__((import_name("i2cInit"))) i2c_dev_t i2c_init(const i2c_config_t *config);
|
||||||
|
extern __attribute__((import_name("framebufferInit"))) buffer_t framebuffer_init(uint32_t *pixel_width, uint32_t *pixel_height);
|
||||||
|
extern __attribute__((import_name("bufferWrite"))) void buffer_write(buffer_t buffer, uint32_t offset, void *data, uint32_t data_len);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,103 @@
|
|||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"author": "CreatureDex Builder",
|
||||||
|
"editor": "wokwi",
|
||||||
|
"parts": [
|
||||||
|
{
|
||||||
|
"type": "board-esp32-s3-devkitc-1",
|
||||||
|
"id": "esp",
|
||||||
|
"top": -144.18,
|
||||||
|
"left": -168.23,
|
||||||
|
"attrs": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "wokwi-lcd1602",
|
||||||
|
"id": "lcd1",
|
||||||
|
"top": -310.4,
|
||||||
|
"left": -253.6,
|
||||||
|
"attrs": { "pins": "i2c" }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "chip-main-display",
|
||||||
|
"id": "lcd2",
|
||||||
|
"top": -162.4,
|
||||||
|
"left": 105.3,
|
||||||
|
"attrs": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "chip-secondary-display",
|
||||||
|
"id": "oled_spi_1",
|
||||||
|
"top": 210,
|
||||||
|
"left": -291.78,
|
||||||
|
"attrs": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "chip-secondary-display",
|
||||||
|
"id": "oled_spi_2",
|
||||||
|
"top": 210,
|
||||||
|
"left": -109.38,
|
||||||
|
"attrs": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "chip-secondary-display",
|
||||||
|
"id": "oled_spi_3",
|
||||||
|
"top": 210,
|
||||||
|
"left": 73.02,
|
||||||
|
"attrs": {}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "chip-nfc-reader",
|
||||||
|
"id": "nfc1",
|
||||||
|
"top": 390,
|
||||||
|
"left": -24,
|
||||||
|
"attrs": {},
|
||||||
|
"hide": true
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"connections": [
|
||||||
|
[ "esp:TX", "$serialMonitor:RX", "", [] ],
|
||||||
|
[ "esp:RX", "$serialMonitor:TX", "", [] ],
|
||||||
|
|
||||||
|
[ "esp:3V3", "lcd1:VCC", "", [] ],
|
||||||
|
[ "esp:GND", "lcd1:GND", "", [] ],
|
||||||
|
[ "esp:8", "lcd1:SDA", "", [] ],
|
||||||
|
[ "esp:9", "lcd1:SCL", "", [] ],
|
||||||
|
|
||||||
|
[ "esp:3V3", "lcd2:VCC", "", [] ],
|
||||||
|
[ "esp:3V3", "lcd2:LED", "", [] ],
|
||||||
|
[ "esp:GND", "lcd2:GND", "", [] ],
|
||||||
|
[ "esp:12", "lcd2:SCK", "", [] ],
|
||||||
|
[ "esp:11", "lcd2:MOSI", "", [] ],
|
||||||
|
[ "esp:10", "lcd2:CS", "", [] ],
|
||||||
|
[ "esp:7", "lcd2:D/C", "", [] ],
|
||||||
|
[ "esp:5", "lcd2:RESET", "", [] ],
|
||||||
|
|
||||||
|
[ "esp:3V3", "oled_spi_1:VCC", "", [] ],
|
||||||
|
[ "esp:3V3", "oled_spi_1:LIGHT", "", [] ],
|
||||||
|
[ "esp:GND", "oled_spi_1:GND", "", [] ],
|
||||||
|
[ "esp:12", "oled_spi_1:CLK", "", [] ],
|
||||||
|
[ "esp:11", "oled_spi_1:DIN", "", [] ],
|
||||||
|
[ "esp:14", "oled_spi_1:CE", "", [] ],
|
||||||
|
[ "esp:6", "oled_spi_1:DC", "", [] ],
|
||||||
|
[ "esp:5", "oled_spi_1:RST", "", [] ],
|
||||||
|
|
||||||
|
[ "esp:3V3", "oled_spi_2:VCC", "", [] ],
|
||||||
|
[ "esp:3V3", "oled_spi_2:LIGHT", "", [] ],
|
||||||
|
[ "esp:GND", "oled_spi_2:GND", "", [] ],
|
||||||
|
[ "esp:12", "oled_spi_2:CLK", "", [] ],
|
||||||
|
[ "esp:11", "oled_spi_2:DIN", "", [] ],
|
||||||
|
[ "esp:15", "oled_spi_2:CE", "", [] ],
|
||||||
|
[ "esp:6", "oled_spi_2:DC", "", [] ],
|
||||||
|
[ "esp:5", "oled_spi_2:RST", "", [] ],
|
||||||
|
|
||||||
|
[ "esp:3V3", "oled_spi_3:VCC", "", [] ],
|
||||||
|
[ "esp:3V3", "oled_spi_3:LIGHT", "", [] ],
|
||||||
|
[ "esp:GND", "oled_spi_3:GND", "", [] ],
|
||||||
|
[ "esp:12", "oled_spi_3:CLK", "", [] ],
|
||||||
|
[ "esp:11", "oled_spi_3:DIN", "", [] ],
|
||||||
|
[ "esp:16", "oled_spi_3:CE", "", [] ],
|
||||||
|
[ "esp:6", "oled_spi_3:DC", "", [] ],
|
||||||
|
[ "esp:5", "oled_spi_3:RST", "", [] ]
|
||||||
|
],
|
||||||
|
"dependencies": {}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
[wokwi]
|
||||||
|
version = 1
|
||||||
|
firmware = "../build/firmware.bin"
|
||||||
|
elf = "../build/firmware.elf"
|
||||||
|
|
||||||
|
[[chip]]
|
||||||
|
name = "main-display"
|
||||||
|
binary = "chips/main-display/dist/chip.wasm"
|
||||||
|
|
||||||
|
[[chip]]
|
||||||
|
name = "secondary-display"
|
||||||
|
binary = "chips/secondary-display/dist/chip.wasm"
|
||||||
|
|
||||||
|
[[chip]]
|
||||||
|
name = "nfc-reader"
|
||||||
|
binary = "chips/nfc-reader/dist/chip.wasm"
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
[wokwi]
|
|
||||||
version = 1
|
|
||||||
gdbServerPort = 3333
|
|
||||||
elf = "target/xtensa-esp32-none-elf/debug/creaturedex"
|
|
||||||
firmware = "target/xtensa-esp32-none-elf/debug/creaturedex"
|
|
||||||
Reference in New Issue
Block a user