Skip to content

Instantly share code, notes, and snippets.

View narfdotpl's full-sized avatar

Maciej Konieczny aka narf narfdotpl

View GitHub Profile
@narfdotpl
narfdotpl / hs.lua
Created April 8, 2024 18:20
Hammerspoon: switch between light and dark mode based on the ambient light sensor
hs.execute("open shortcuts://run-shortcut?name=" .. (hs.brightness.ambient() > 80 and "light" or "dark"))
//
// Coordinates.swift
// Scrapyard
//
// Created by Maciej Konieczny aka narf
// https://vis.social/@narf/111654813643291716
//
import Foundation
@narfdotpl
narfdotpl / README.md
Created July 4, 2017 17:06
Quick note about ESP32 servo test

In this video I used TCP sockets. I have since switched to UDP sockets with no buffering. Results are similarly smooth but servos are more responsive.

I use the following setup:

  • SparkFun ESP32 Thing
  • MG92B servos
  • ESP32 is an access point (creates a wifi network) and a UDP socket server
  • gamepad communicates with the phone over bluetooth
  • phone joins the wifi network and sends data to the UDP socket
  • I need only one-way communication
@narfdotpl
narfdotpl / gist:d6d297d4d5f4db0aaa79
Last active August 29, 2015 14:24
Xcode 7β3 crash

Update: You can fix Xcode 7β3 "open project crash" by opening Xcode 6, going to preferences, "Accounts" tab, and removing repositories on the left.


Xcode 7β3 crashes when I open a project or try to create one...

Application Specific Information:
ProductBuildVersion: 7A152u
ASSERTION FAILURE in /Library/Caches/com.apple.xbs/Sources/IDEFrameworks/IDEFrameworks-8163.8/IDEFoundation/SourceControl/Model/IDESourceControlTree.m:82
protocol Fooable {
var foo: String { get }
}
extension Fooable {
var foo: String {
return "foo"
}
}
@narfdotpl
narfdotpl / gist:8f7f841569d5208f5180
Created February 14, 2015 14:52
Postfix `?` operator in Swift

Postfix ? operator in Swift

As Chris Eidhof pointed out, Swift 1.1 (available in Xcode 6.1) had postfix ? operator, which could be used to implement flatMap on optionals:

// Swift 1.1 (Xcode 6.1)

extension Optional {
    func flatMap<U>(f: T -> U?) -> U? {
 return map(f)?
@narfdotpl
narfdotpl / gist:3f52f720d3c9f311528a
Created February 9, 2015 19:52
if-let-with-multiple-optionals-is-lazy.swift
// Of course, `if let` with multiple optionals is lazy.
//
// Tested on 2015-02-09 in Xcode 6.3 Beta 1, Swift 1.2.
func foo() -> Int? {
println("foo")
return 0
}
@narfdotpl
narfdotpl / gist:78860a9ce5dbc3e19633
Created January 23, 2015 07:05
Swift job interview idea: implement Dictionary.map
def f(s):
return '%.3f' % (int(s, 16) / 255.0)
def rgb(s):
print '[UIColor colorWithRed:%s green:%s blue:%s alpha:1],' % \
tuple(f(s.lstrip('#')[i : i + 2]) for i in range(0, 6, 2))
rgb('ffffff') # [UIColor colorWithRed:1.000 green:1.000 blue:1.000 alpha:1],
rgb('ff0000') # [UIColor colorWithRed:1.000 green:0.000 blue:0.000 alpha:1],
@narfdotpl
narfdotpl / gist:2a919667bc5f98a1c666
Created June 20, 2014 10:05
Swift: get substring
let email = "hello@narf.pl"
let domain = email[find(email, "@")!.succ()..email.endIndex] // WTF
println(domain)