Skip to content

Instantly share code, notes, and snippets.

View TorsteinOvstedal's full-sized avatar
🤓

Torstein Øvstedal TorsteinOvstedal

🤓
View GitHub Profile
@TorsteinOvstedal
TorsteinOvstedal / gist:5cabcaa6cc6fd86be9df00d524079e18
Created April 1, 2024 21:46
Dark Mode in Firefox's PDF reader (invert colors)
// Usage: Enter the JS snippet below into the web console (opened with Ctrl+Shift+K).
// Source: https://connect.mozilla.org/t5/ideas/dark-mode-for-embedded-pdf-viewer/idi-p/4932#link_30
window.PDFViewerApplication.pdfViewer.container.style.filter = "invert(1) grayscale(1)"
@TorsteinOvstedal
TorsteinOvstedal / main.py
Last active March 21, 2024 23:54
Syntax-directed translation using recursive-descent.
import io
import os
class SyntaxError(Exception): pass
class Parser:
"""
Translates infix expressions into postfix through a
syntax-directed, predictive "translator" (parser).
@TorsteinOvstedal
TorsteinOvstedal / main.py
Last active March 4, 2024 23:40
Example usage of a State Transition Table. Checks if the input string has the form of a valid unsigned integer.
"""
State transition table for scanning an unsigned int.
Source: p. 34, Engineering a Compiler, 3ed.
"""
S0 = 0
S1 = 1
S2 = 2
SE = 3
@TorsteinOvstedal
TorsteinOvstedal / pcm.py
Last active February 25, 2024 19:50
Playing PCM data in python using SDL2 with SDL_mixer.
"""
Purpose:
Play PCM data.
API overview:
sound_init Initialize sound system.
sound_close Close sound system.
sound_play Play PCM data.
Dependencies:
@TorsteinOvstedal
TorsteinOvstedal / bitstream.py
Last active February 25, 2024 15:59
Iterate through blocks of bits from a number using python generator.
def bitstream(x: int, length: int, block_size: int):
assert length > 0 and block_size > 0, "Unsupported parameters"
mask = int("1" * block_size, 2)
for i in range(length):
block = x & mask
x >>= block_size
yield block
@TorsteinOvstedal
TorsteinOvstedal / usart.h
Last active March 24, 2023 09:55
USART on the MCU ATmega328p.
#ifndef USART_H
#define USART_H
#include <avr/io.h>
/**
* Initialize the USART registers for async operation with 8N1 data frames.
*
* Ref: 7810D–AVR–01/15, p. 159 - 165
*/