Skip to content

Instantly share code, notes, and snippets.

@PabloMansanet
PabloMansanet / gpio.rs
Created April 13, 2021 12:19
gpio.rs
use core::marker::PhantomData;
use efm32pac::{CMU, GPIO};
use crate::{
construct_gpio, efm32pac, gpio_modify, gpio_read, gpio_struct, gpio_write,
hal::gpio::{InputPin, OutputPin, TogglePin},
matrix, pin_aliases,
};
/// Pin mode typestates
import osproc
type
process_state = object
system_process: osproc.Process
proc process*(command: string): process_state =
result = process_state(
system_process: startProcess(
command: command

A common pattern we use at BF is dependency injection through C++ abstract classes. This makes testing easy, but I have been wondering for a while if we can do better in how we implement the underlying principles. The conversation about Rust and mutability has helped me verbalize why I think C++ is limited when it comes to solving this problem.

Why dynamic dispatch?

At BF, we solve the problem of dependency injection with dynamic dispatch, even when the particular injection can be resolved at compile time. Here's a silly (and not great for reasons I'll explain later) example of what we sometimes do:

  • GpioInterface.hpp
void check(char *password)
{
size_t password_length;
char character_test;
uint char_counter;
int accumulator;
int number_from_char;
accumulator = 0;
#include <stdio.h>
void innocentFunction(const char* userString)
{
char localBuffer[64];
sprintf(localBuffer, userString);
// [..]
}
coretemp-isa-0000
Adapter: ISA adapter
Package id 0: +39.0°C (high = +100.0°C, crit = +100.0°C)
Core 0: +38.0°C (high = +100.0°C, crit = +100.0°C)
Core 1: +38.0°C (high = +100.0°C, crit = +100.0°C)
Core 2: +38.0°C (high = +100.0°C, crit = +100.0°C)
Core 3: +38.0°C (high = +100.0°C, crit = +100.0°C)
acpitz-acpi-0
Adapter: ACPI interface
Device: Power Button
Kernel: /dev/input/event2
Group: 1
Seat: seat0, default
Capabilities: keyboard
Tap-to-click: n/a
Tap-and-drag: n/a
Tap drag lock: n/a
Left-handed: n/a
Nat.scrolling: n/a
[Nov29 21:07] Linux version 4.19.4-arch1-1-ARCH (builduser@heftig-22537) (gcc version 8.2.1 20180831 (GCC)) #1 SMP PREEMPT Fri Nov 23 09:06:58 UTC 2018
[ +0.000000] Command line: BOOT_IMAGE=(loop)/arch/boot/x86_64/vmlinuz module_blacklist=intel_lpss_pci img_dev=/dev/disk/by-uuid/df54812e-95b6-40ba-a398-e5d94d84d3fd img_loop=/boot/isos/halberd-2018.11.28-x86_64.iso earlymodules=loop
[ +0.000000] KERNEL supported cpus:
[ +0.000000] Intel GenuineIntel
[ +0.000000] AMD AuthenticAMD
[ +0.000000] Centaur CentaurHauls
[ +0.000000] x86/fpu: Supporting XSAVE feature 0x001: 'x87 floating point registers'
[ +0.000000] x86/fpu: Supporting XSAVE feature 0x002: 'SSE registers'
[ +0.000000] x86/fpu: Supporting XSAVE feature 0x004: 'AVX registers'
[ +0.000000] x86/fpu: Supporting XSAVE feature 0x008: 'MPX bounds registers'