Skip to content

Instantly share code, notes, and snippets.

View dcoles's full-sized avatar

David Coles dcoles

View GitHub Profile
@dcoles
dcoles / alarmtimer.py
Created March 20, 2012 07:30
POSIX Alarm Timers example using Python ctypes
#!/usr/bin/env python
# POSIX Alarm Timer example using Python ctypes
#
# Author: David Coles <coles.david@gmail.com>
# Date: 2012-03-19
#
# To the extent possible under law, the author(s) have dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.
@dcoles
dcoles / c99-cxx.c
Last active August 16, 2025 03:44
C99 features that don't work in C++
/**
* C99 features that don't work in C++
*
* Written in 2011 by David Coles https://dcoles.net/
*
* To the extent possible under law, the author(s) have dedicated all copyright and related and
* neighboring rights to this software to the public domain worldwide.
* This software is distributed without any warranty.
*/
@dcoles
dcoles / event_flag.rs
Last active February 4, 2025 07:06
C Handles in Rust
//! Furi Event Flag.
use core::cell::UnsafeCell;
use core::ops::Deref;
use core::ptr::NonNull;
use crate::furi::time::FuriDuration;
use crate::furi::Error;
use flipperzero_sys as sys;
use flipperzero_sys::furi::{Handle, Status};
@dcoles
dcoles / __init__.py
Last active October 21, 2024 17:07
SPA Plugin Loading in Python
# SPA bindings
import os
import functools
from spa._cffi import ffi
from spa.pod import PodObject
from spa.constants import SPA_TYPE_COMMAND__NODE
@dcoles
dcoles / main.rs
Created October 21, 2024 04:00
Flipper Zero serial reader (work in progress)
#![no_main]
#![no_std]
// Required for panic handler
extern crate flipperzero_rt;
use core::{ffi::{c_void, CStr}, ops::{Deref, DerefMut}, ptr};
use flipperzero::{furi::{self, stream_buffer::FuriStreamBuffer, thread::{self, ThreadId}, time::Duration}, info, log, println};
use flipperzero_rt::{entry, manifest};
@dcoles
dcoles / postcard-tlv.rs
Created October 5, 2024 05:50
A simple Type-Length-Value format inspired by `postcard-rpc`
//! A simple Type-Length-Value format inspired by `postcard-rpc`
//!
//! Author: David Coles <https://github.com/dcoles/>
//! SPDX-License-Identifier: MIT
use std::io::{Read, Write, Cursor};
use std::time;
use postcard::experimental::schema::Schema;
use postcard_rpc::hash::fnv1a64::hash_ty_path;
use serde::{Deserialize, Serialize};
@dcoles
dcoles / inline.py
Created November 14, 2012 09:13
Inline assembly in Python
import ctypes
import sys
import os
import errno
FUNC = ctypes.CFUNCTYPE(None)
PROT_NONE = 0
PROT_READ = 1
PROT_WRITE = 2
@dcoles
dcoles / 452874.py
Last active December 20, 2021 21:53
By only adding + - * into the string 452874 (in that order), how many way can you get to -18?
"""
By only adding + - * into the string 452874 (in that order),
how many way can you get to -18?
"""
N = "452874"
M = -18
OPERATIONS = [None, '+', '-', '*']
@dcoles
dcoles / draft.rs
Created December 1, 2021 19:51
Advent of Code 2021: Day 1 (Draft vs. Final)
fn main() {
let input = read_input_from_file("day01/input.txt").unwrap();
// Part 1
let mut count = 0;
let mut last = -1;
for &n in &input {
if n > last {
count += 1;
}
# Print the number of bytes unread in a fifo.
# David Coles <coles.david@gmail.com>
import argparse
import ctypes
import fcntl
import os
import sys
import termios