Skip to content

Instantly share code, notes, and snippets.

View lionello's full-sized avatar
🇭🇰

Lio李歐 lionello

🇭🇰
View GitHub Profile
@lionello
lionello / imageset2mipmap.js
Last active August 22, 2017 08:04
Node.JS script to losslessly convert an iOS .imageset to an Android mipmap
#!/usr/bin/env node
const FS = require('fs')
const Path = require('path')
const FolderMap = {
"1x": "mipmap-mdpi",
"2x": "mipmap-xhdpi",
"3x": "mipmap-xxhdpi",
}
@lionello
lionello / mrz.d
Last active September 7, 2017 04:02
MRZ (Machine Readable Zone) checksum calculator
// Copyright Lionello Lunesu, placed in the public domain.
import std.algorithm;
import std.range;
ubyte charCode(dchar c) pure {
switch (c) {
case '<': return 0;
case 'A': .. case 'Z': return cast(ubyte)(c - 'A' + 10);
case '0': .. case '9': return cast(ubyte)(c - '0');
default: assert(0);
@lionello
lionello / nrf_ws2812.h
Last active September 16, 2017 01:08
Bit-banging a WS2812/WS2812B LED on Nordic nRF51 platform (nRF51422 or nRF51822)
#define NOP() __asm volatile ( "NOP" )
void ws2812_init(void)
{
NRF_GPIO->PIN_CNF[PIN_WS_DIN] = (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos)
| (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos)
| (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos)
| (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos)
| (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
}
@lionello
lionello / optimize_eagle.d
Created September 17, 2017 01:26
Optimize paths in Eagle PCB files for faster CAM processing of GERBER files
import std.xml;
import std.file;
import std.stdio;
import std.conv;
import std.math;
real EPSILON = 1e-2;
real distance_to_line(real fx, real fy, real tx, real ty, real px, real py) {
@lionello
lionello / androiduml.d
Last active October 20, 2017 06:27
Create PlantUML/graphviz dot activity diagram for an Android project
#!/usr/bin/env dmd -run
import std.xml;
import std.file;
import std.stdio;
Element getElementByTagName(Element parent, string name) {
foreach (child; parent.elements) {
if (child.tag.name == name) {
return child;
@lionello
lionello / 1password-to-pass.js
Created February 27, 2018 16:22
NodeJS script to import a 1password tab-delimited TXT file into 'pass'
#!/usr/bin/env node
const FS = require('fs')
const URL = require('url')
const Child_process = require('child_process')
const all = FS.readFileSync('pww.txt', {encoding:'utf8'})
const regex = /\n(?:"([^"]*)")?\t"([^"]*)"\t"([^"]*)"\t"([^"]*)"\t"([^"]*)"\t(?:"([^"]*)")?/mg
while (true) {
const match = regex.exec(all)
if (!match) break
@lionello
lionello / repro181.sol
Last active March 14, 2018 18:56
Repro for Geth 1.8.x `eth.call` regression
#!/usr/bin/env node
const Child_process = require('child_process')
const Web3 = require('web3')
const Assert = require('assert')
const SOL = `
contract BS {
function isValidSignature(
address signer,
bytes32 hash,
@lionello
lionello / rejects.js
Created July 5, 2018 23:29
Assertion helper for async/promise methods that should fail
Assert.rejects = async function rejects (blockOrPromise, error, message) {
return new Promise((resolve,reject) => {
if (typeof blockOrPromise === 'function') {
blockOrPromise = blockOrPromise()
}
blockOrPromise.then(x => {
reject(Error('Should fail, not return ' + x))
}).catch(async err => {
if (typeof error === 'function') {
err = (await error(err)) || err
@lionello
lionello / ethereum-test-template.js
Last active July 31, 2018 08:07
Small JS template for writing Ethereum/Solidity tests
/* Use this with an overmind Procfile like this:
geth: geth --dev --rpc console
jest: pnpx jest --watchAll
*/
const ChildProcess = require('child_process')
const Web3 = require('web3')
const Assert = require('assert')
function addressOf(contract) {
return contract.options.address
@lionello
lionello / main.go
Created August 20, 2018 06:20
Graceful shutdown of a Go HTTP server
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"