Skip to content

Instantly share code, notes, and snippets.

View kristianlm's full-sized avatar

Kristian Lein-Mathisen kristianlm

View GitHub Profile
@kristianlm
kristianlm / atmega328 low power sleep.c
Created May 26, 2021 16:32
My atmega328 and attiny85 avr-gcc snippet collection
// prescaler timings:
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void watchdog_init(uint8_t prescaler) {
MCUSR &= ~(1<<WDRF); // <-- reset wdt
WDTCSR |= (1<<WDCE) | (1<<WDE); // <-- start timed sequence
WDTCSR = (1<<WDCE) | (1<<WDIE) |
((prescaler>>3) & 1)<<WDP3 |
((prescaler>>2) & 1)<<WDP2 |
@kristianlm
kristianlm / x86-codegen-at-runtime.c
Created January 11, 2021 18:03
how to execute self-generated x86 machine code on Linux
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
int main(int argc, char *argv[]) {
unsigned int pagesize;
This file has been truncated, but you can view the full file.
KLUv/QSIxOILrBEVLmdpdC8AMDc1NTE3NTAAMTM2MTAxNDY3NjQAMDEwMDU3ACA1AHVzdGFyICAA
a2xtYnJhbmNoZXMvMDU0MjYyNTYxNjQ1aG9vazIwM2FwcGx5cGF0Y2gtbXNnLnNhbXBsZTczNjUz
IDAjIS9iaW4vc2gKIwojIEFuIGV4ICBzY3JpcHQgdG8gY2sgdGhlIGNvbW1pdCBsb2cgbWVzc2Fn
ZSB0YWtlbiBieSBmcm9tIGEtbWFpbC5UaGhvdWxkd2l0aCBub24temVybyB0dXMgYWZ0ZXIgaXNz
dWluZ3JvcHJpYXRlaWYgYW50c29wLiBsbG93ZWRlZGZpbG5hYmxlaXMsIHJtIiIuCgouIC1zaC1z
ZXR1cAo9IiQodi1wYXJzZSAtLSkiCnRlc3QgLXggIiQiICYmZWMkezErIiRAIn0KOgoxNjQ0NjIu
Q2J5ICIib25lIGFyZ3VtZW50LG9mdGhhdCBoYXNVbmJlYWRkIGEgU2lnbmVkLW9mZi1saW5Eb2kg
YmFkIGlkZWFnZW5lcmFsLCBidXByZWUtbW9yZSB0U09CPXZHSVRfQVVUSE9SX0lERU5UIHwgcy1u
ICdzL15cKC4qPlwpLiokLzogXDEvcCcpZyAtcXMgIl4kIjEiIHx8IGVjaD4+CmMgZHVwbGllcyIi
ID0oJ14nCgkgc29ydHVuaXEgLWNlICcvXlsgCV0qMS9kJyl7Cgk+JjIgRAkxCn0KZnNtb25pdG9y
@kristianlm
kristianlm / nurse-shift-schedule.scm
Created January 2, 2020 21:42
nurse shift shedule
;;(define cal (with-input-from-file "v2020.scm" read))
;;2020
(define cal
`(
;;w mtotfls
( 5 dfaadff)
(23 dfnnfff)
))
(import chicken.io
@kristianlm
kristianlm / gdigrab.c
Last active August 15, 2018 22:09
Screen Capture of your Windows10 machine as a http://<ip>:8088/image.png
#include <windows.h>
#include <stdio.h>
int screen_w() {
return GetSystemMetrics(SM_CXVIRTUALSCREEN)
- GetSystemMetrics(SM_XVIRTUALSCREEN);
}
int screen_h() {
(use minissh nrepl)
;; the default /dev/random causes hangs
(use tweetnacl) (current-entropy-port (open-input-file "/dev/urandom"))
;; the secret key would normally be kept safe
(define host-pk
"AAAAC3NzaC1lZDI1NTE5AAAAIIfd+rbtTF2hJJbnnbQxtp2UVrUWkQtnsT8CL9iLpZBZ")
(define host-sk
#${ba72291c15494ee02003b3c0bb0f8507a6a803850aa811d015b141a193e2447d
@kristianlm
kristianlm / generators.scm
Last active September 29, 2017 08:23
a sample generator API for Scheme, implemented using continuations
;;; generator function example, inspired by this post:
;;; http://matt.might.net/articles/programming-with-continuations--exceptions-backtracking-search-threads-generators-coroutines/
;;;
;;; Kristian Lein-Mathisen 2017
(import (scheme small))
; current-continuation : -> continuation
(define (current-continuation)
(call-with-current-continuation
// show an image in the terminal using ascii colors
// demonstrates raw pixel access
// (only works on certain image file types, though)
#include <stdio.h>
#include <FreeImage.h>
int meta(FIBITMAP *dib) {
FITAG *tag = NULL;
FIMETADATA *mdhandle = NULL;
mdhandle = FreeImage_FindFirstMetadata(FIMD_EXIF_MAIN, dib, &tag);
;; a nicer alist api
(define (aref alst key #!optional (missing (lambda () #f)) (= equal?))
(let loop ((alst alst))
(if (pair? alst)
(if (= (caar alst) key) (cdar alst) (loop (cdr alst)))
(if (procedure? missing) (missing) missing))))
(define (adel alst key #!optional (= equal?))
(alist-delete key alst =))