Skip to content

Instantly share code, notes, and snippets.

View pixelspark's full-sized avatar

Tommy van der Vorst pixelspark

View GitHub Profile
@pixelspark
pixelspark / _readme.md
Created March 25, 2020 14:53 — forked from stecman/_readme.md
Brother P-Touch PT-P300BT bluetooth driver python

Controlling the Brother P-Touch Cube label maker from a computer

The Brother PTP300BT label maker is intended to be controlled using the official Brother P-Touch Design & Print iOS/Android app. The app has arbitrary limits on what you can print (1 text object and up to 3 preset icons), so I thought it would be a fun challenge to reverse engineer the protocol to print whatever I wanted.

Python code at the bottom if you want to skip the fine details.

Process

Intitially I had a quick peek at the Android APK to see if there was any useful information inside. The code that handles the communication with the printer in Print&Design turned out to be a native library, but the app clearly prepares a bitmap image and passes it to this native library for printing. Bitmaps are definitely something we can work with.

[Macaron] 2019-10-30 10:08:26: Started GET /pulls for IPADDRESS
[Macaron] 2019-10-30 10:08:27: Completed GET /pulls 200 OK in 238.380521ms
[Macaron] 2019-10-30 10:08:27: Started GET /manifest.json for IPADDRESS
[Macaron] 2019-10-30 10:08:27: Completed GET /manifest.json 200 OK in 11.685308ms
Connection closed by 10.255.0.5 port 56762 [preauth]
[Macaron] 2019-10-30 10:08:29: Started GET /serviceworker.js for IPADDRESS
[Macaron] 2019-10-30 10:08:29: Completed GET /serviceworker.js 200 OK in 16.178945ms
[Macaron] 2019-10-30 10:08:34: Started GET /ORGNAME/REPONAME/issues/1 for IPADDRESS
[Macaron] 2019-10-30 10:08:34: Completed GET /ORGNAME/REPONAME/issues/1 302 Found in 95.282559ms
[Macaron] 2019-10-30 10:08:34: Started GET /ORGNAME/REPONAME/pulls/1 for IPADDRESS
public extension Sequence {
typealias Element = Iterator.Element
func foo() {
let x = self.makeIterator()
}
}
/** Throttle wraps a block with throttling logic, guarantueeing that the block will never be called (by enquueing
asynchronously on `queue`) more than once each `interval` seconds. If the wrapper callback is called more than once
in an interval, it will use the most recent call's parameters when eventually calling the wrapped block (after `interval`
has elapsed since the last call to the wrapped function) - i.e. calls are not queued and may get 'lost' by being superseded
by a newer call. */
public func throttle<P>(interval: TimeInterval, queue: DispatchQueue, _ block: ((P) -> ())) -> ((P) -> ()) {
var lastExecutionTime: TimeInterval? = nil
var scheduledExecutionParameters: P? = nil
let mutex = Mutex()
public func assertMainThread(_ file: StaticString = #file, line: UInt = #line) {
assert(Thread.isMainThread, "Code at \(file):\(line) must run on main thread!")
}
public func asyncMain(_ block: () -> ()) {
DispatchQueue.main.async(execute: block)
}
/** A pthread-based recursive mutex lock. */
public class Mutex {
private var mutex: pthread_mutex_t = pthread_mutex_t()
public init() {
var attr: pthread_mutexattr_t = pthread_mutexattr_t()
pthread_mutexattr_init(&attr)
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)
let err = pthread_mutex_init(&self.mutex, &attr)
public func trace(_ message: String, file: StaticString = #file, line: UInt = #line) {
#if DEBUG
DispatchQueue.main.async {
print(message)
}
#endif
}
/** Wrap a block so that it can be called only once. Calling the returned block twice results in a fatal error. After
the first call but before returning the result from the wrapped block, the wrapped block is dereferenced. */
public func once<P, R>(_ block: ((P) -> (R))) -> ((P) -> (R)) {
#if DEBUG
var blockReference: ((P) -> (R))? = block
let mutex = Mutex()
return {(p: P) -> (R) in
let block = mutex.locked { () -> ((P) -> (R)) in
assert(blockReference != nil, "callback called twice!")

Keybase proof

I hereby claim:

  • I am pixelspark on github.
  • I am tommyv (https://keybase.io/tommyv) on keybase.
  • I have a public key whose fingerprint is 1E34 4B8C 5441 6D99 2278 A524 8D35 BAC2 D110 A721

To claim this, I am signing this object:

@pixelspark
pixelspark / Fallible.swift
Created June 3, 2015 18:02
Swift Fallible type
/**
FIXME: Box exists to prevent the "Unimplemented IR generation feature non-fixed multi-payload enum layout" error. */
final class Box<T> {
let value: T
init(_ value: T) {
self.value = value
}
}