Skip to content

Instantly share code, notes, and snippets.

View funky-monkey's full-sized avatar

Sidney de Koning funky-monkey

View GitHub Profile
@PimCoumans
PimCoumans / CustomButton.swift
Last active July 8, 2022 09:16
UIButton subclass with per-state custom values like background and image colors, extendable with whatever value you want to update
class Button: UIButton {
private class Subview: UIView {
// Never allow isUserInteractionEnabled in any button subview
override var isUserInteractionEnabled: Bool {
get { false }
set { super.isUserInteractionEnabled = false }
}
}
import Foundation
@propertyWrapper
struct YMD {
var wrappedValue: Date?
}
extension YMD: Codable {
func encode(to encoder: Encoder) throws {
if let date = self.wrappedValue {
@gelin
gelin / print-badge
Last active November 12, 2024 06:44
Python script to print a badge to a thermal printer with TSPL language (by TSC)
#!/usr/bin/env python3
PRINTER = '/dev/usb/lp0' # the printer device
DOTS_MM = 8 # printer dots per mm, 8 == 203 dpi
WIDTH_MM = 100 # sticker width, mm
HEIGHT_MM = 35 # sticker height, mm
GAP_MM = 2 # sticker gap, mm
FONT = "0" # built-in vector font, scalable by X and Y
@onderweg
onderweg / inkbird.js
Last active December 7, 2023 09:50
Read temperature and humidity data from Inkbird ibs-TH1.
const DATA_HND = 0x002d;
const parseData = (data) => {
const rawTemp = data.readInt16LE(0);
const rawHum = data.readInt16LE(2);
return {
temperature: rawTemp / 100,
humidity: rawHum / 100
}
}
@hardillb
hardillb / Notes
Last active February 10, 2021 19:54
Notes on IKEA TRÅDFRI Smart Lighting
Basic MQTT bridge can be found here:
https://github.com/hardillb/TRADFRI2MQTT
Bridge adds a mDNS entry for a COAP sever:
Service Type: _coap._udp
Service Name: gw:b0-72-bf-25-bf-59
Domain Name: local
Interface: wlan0 IPv4
@andymatuschak
andymatuschak / States-v3.md
Last active November 14, 2024 18:33
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,

@pvzig
pvzig / main.swift
Last active November 14, 2019 13:35
Sample Slack bot implementation in Swift
import Foundation
import SlackKit
class RobotOrNotBot {
let verdicts: [String:Bool] = [
"Mr. Roboto" : false,
"Service Kiosks": false,
"Darth Vader": false,
"K-9": true,
@nakiostudio
nakiostudio / UpdateRealmObject.swift
Last active June 25, 2019 14:04
Make RealmSwift write and update an Object keeping the existing relationships
/**
Writes a set of objects in the database.
- parameter objects: Array of `Objects` to be stored on the database
- parameter configuration: Realm `Configuration` in which the write action will be performed
- parameter update: Enabled the custom *update* maintaining existing relationships
*/
static func write(objects : [Object], configuration: Realm.Configuration, update: Bool = false) {
if let realm = try? Realm(configuration: configuration) {
realm.beginWrite()
@jimrutherford
jimrutherford / CrashlyticsDestination.swift
Created December 10, 2015 21:00
SwiftyBeaver Crashlyitcs Log Destination
//
// CrashlyticsDestination.swift
//
// Created by Jim Rutherford on 2015-12-10.
//
import UIKit
import Crashlytics
public class CrashlyticsDestination: BaseDestination {
@davetrux
davetrux / hmac.swift
Last active November 8, 2016 19:06
HMAC algorithm for iOS
import Foundation
//You have to create a bridging header in your project containing:
// #import <CommonCrypto/CommonHMAC.h>
extension String {
func digestHMac256(key: String) -> String! {
let str = self.cStringUsingEncoding(NSUTF8StringEncoding)