Skip to content

Instantly share code, notes, and snippets.

View barafael's full-sized avatar
💭
fighting the red and yellow squiggles

Rafael Bachmann barafael

💭
fighting the red and yellow squiggles
View GitHub Profile
@barafael
barafael / build.rs
Last active September 22, 2020 07:55
Make an stm32 binary combining Rust and C code
fn main() {
println!("cargo:rustc-link-search=.");
println!("cargo:rustc-link-lib=static=toggle");
}
@barafael
barafael / typestate_pattern.md
Created July 26, 2020 21:23
Short piece about the typestate pattern in Rust

"Easy to Use Correctly and Hard to Use Incorrectly" - The Typestate Pattern

How do you model a program so that it reflects real-world state, and that undesired or impossible states are unreachable?

Especially in safety-focused systems programming related to communication, interfaces, or I/O, the typestate pattern helps to ensure correctness and safety. Encoding state in type signatures exposes only valid transitions and operations. This reduces the need of runtime errors and even checks and leads to very pleasant APIs.

The Typestate Pattern in Rust

The Rust language has some unique features which enable downright beautiful typestate patterns. One idiomatic way to implement the state machine pattern is a struct with a generic parameter for each state. Such a StateMachine implements the operations of ``, and the initial state has a 'new' implementation. Transitions are realized by implementing `std::convert::From` (automagically implementing `std::convert::Into`). Due to the type sign

@barafael
barafael / Cargo.toml
Created May 11, 2019 14:31
Sad blinky example in Rust
[dependencies]
cortex-m = "0.5.8"
cortex-m-rt = "0.6.7"
cortex-m-semihosting = "0.3.2"
panic-halt = "0.2.0"
stm32f4xx-hal = { version = "0.5.0", features = ["stm32f407"]}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int matr;
char *name;
unsigned long isiks;
} student;
extern "C" {
fn random() -> u64;
fn time(n: u32) -> u32;
fn srand(n: u32);
}
fn main() {
unsafe {srand(time(0)) };
println!("{}", unsafe{random()});
}
@barafael
barafael / floating_point.c
Created January 13, 2019 13:51
Floating point type punning in C
#include <math.h>
#include <stdint.h>
#include <stdio.h>
/*
* Type punning - allowed in C, not in C++ (but works on gcc)
*/
typedef union {
float content;
set shell=/bin/bash\ --login
set shell=bash
let g:python_host_prog = '/usr/bin/python3'
let g:python2_host_prog = '/usr/bin/python2'
let g:python3_host_prog = '/usr/bin/python3'
" '~' in path seems to not work?
call remote#host#RegisterPlugin('python3', '/home/ra/.local/share/nvim/plugged/neomake-platformio/rplugin/python/neomake-platformio.py', [
\ {'sync': v:false, 'name': 'SetupPlatformioEnvironment', 'type': 'function', 'opts': {}},
const float LOWER_LIMIT = 655.36f * 40; // 40% duty cycle of 2.5ms signal is 1 ms
const float UPPER_LIMIT = 655.36f * 80; // 80% duty cycle of 2.5ms signal is 2 ms
const float RANGE = UPPER_LIMIT - LOWER_LIMIT;
#define OUT_PIN 6
void write(float throttle) {
uint16_t value = LOWER_LIMIT + RANGE * throttle;
analogWrite(OUT_PIN, value);
}
#include "Receiver.h"
Receiver receiver(8, 9, 10, 11);
uint16_t channels[4] = { 0 };
void setup() {
Serial.begin(9600);
}
@barafael
barafael / error_handling.h
Last active November 2, 2017 20:04
WIP 1D PID controller for teensy 3.2
typedef enum {
DMP_INIT_MEM_LOAD_FAILED,
DMP_CONF_UPDATES_FAILED,
DMP_ERROR_UNKNOWN
} error_type;
static const uint16_t BLINK_PERIOD = 500;
void blink_pattern(char pattern[]) {
while(1) {