Skip to content

Instantly share code, notes, and snippets.

View DavidAntliff's full-sized avatar

David Antliff DavidAntliff

View GitHub Profile
@DavidAntliff
DavidAntliff / mqtt_cpp_demo2_disconnect.cpp
Created August 30, 2021 23:53
Support disconnection via subscription or timeout
/*
* Working integration of mqtt_cpp with producer thread.
* Having the worker thread started by the connack handler is recommended by the mqtt_cpp author.
* This program also demonstrates automatic disconnection by a timer, as well as handling disconnection by the broker.
* Additionally, a message can be sent to the 'quit' topic to initiate disconnection.
*
* E.g.
* $ mosquitto_pub -h localhost -t quit -m 1
*/
@DavidAntliff
DavidAntliff / mqtt_cpp_demo2.cpp
Created August 27, 2021 02:30
Move the creation of the worker thread to the connack handler
#include <iostream>
#include <thread>
#include <chrono>
#include <boost/asio.hpp>
#include <mqtt_client_cpp.hpp>
struct App {
boost::asio::io_context ioc {};
using client_t = decltype(MQTT_NS::make_sync_client(std::declval<boost::asio::io_context &>(), "", 0));
@DavidAntliff
DavidAntliff / mqtt_cpp_demo.cpp
Last active August 27, 2021 01:56
Wait for connection before attempting to publish
#include <iostream>
#include <thread>
#include <chrono>
#include <boost/asio.hpp>
#include <mqtt_client_cpp.hpp>
struct App {
boost::asio::io_context ioc {};
using client_t = decltype(MQTT_NS::make_sync_client(std::declval<boost::asio::io_context &>(), "", 0));
@DavidAntliff
DavidAntliff / disable-acpi-wakeup.py
Created August 13, 2020 23:45
Deactivate ACPI devices that may cause hibernation to fail
#!/usr/bin/env python
"""
Deactivate ACPI devices that may cause hibernation to fail
"""
from dataclasses import dataclass
import subprocess
@dataclass
@DavidAntliff
DavidAntliff / matplotlib-macos-notes.md
Last active August 15, 2019 02:41
Build Python 3.7.3 via pyenv on MacOS Mojave with matplotlib support

Errors about undefined symbols

Something something __Py_UnixMain _main python.o:

$ brew uninstall binutils

Xcode headers are not properly installed.

Error is:

@DavidAntliff
DavidAntliff / dice_roll.py
Created October 22, 2018 11:28
Simple simulation of rolling dice
#!/usr/bin/env python
# Python 3.x
import random
rolls = 5000 # the more results the better
scaling = 10 # scale the histogram to fit the screen
dice = (20,) # a single d20
#dice = (6, 6) # two d6s
@DavidAntliff
DavidAntliff / float_to_rgba.py
Created December 19, 2017 02:26
Encode 32bit float into three 8bit RGBA channels
#!/usr/bin/env python
# Store 32-bit floating point number within three 8bit channels of a 32-bit RGBA pixel.
# Only suitable for normalised input values in the range [0.0, 1.0).
#
# Refer: https://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
import numpy as np
def frac(x):
return x - np.floor(x)
@DavidAntliff
DavidAntliff / txt2csv.py
Created October 24, 2017 22:42
Convert xxd hexdump to CSV file for spreadsheet import
# Assumes in_txt was generated by `xxd -g1 in.bin > in.txt`
import csv
with open(in_txt) as fin, open(out_csv, 'w') as fout:
o=csv.writer(fout, quotechar='"', quoting=csv.QUOTE_ALL)
for line in fin:
row = line.rstrip('\n').split(sep=' ', maxsplit=17)
# remove one leading space from last item
row[-1] = row[-1][1:]
@DavidAntliff
DavidAntliff / git-rinse.sh
Created March 21, 2017 22:28
Git Rinse - revert a project back to a just-cloned state quickly
#!/bin/bash
# Note: this removes all uncommitted changes and untracked files!
git clean -xfd
git submodule foreach --recursive git clean -xfd
git reset --hard
git submodule foreach --recursive git reset --hard
git submodule update --init --recursive
@DavidAntliff
DavidAntliff / custom_logging.py
Last active March 21, 2017 22:29
Python: install custom logging handlers for stdout and stderr based on --debug/--verbose flags
import logging
import sys
def init_logging(name, log_level):
# install handlers for stdout (DEBUG, INFO) and stderr (WARNING, ERROR)
# http://stackoverflow.com/a/16066513/143397
logger = logging.getLogger(name)
logger.setLevel(log_level)