Skip to content

Instantly share code, notes, and snippets.

@brandonros
brandonros / gist:f276b75099d363d8c74e00ec55892e91
Created February 21, 2019 06:05
Vanilla JavaScript equivalent of jQuery $('body').on('click', 'selector', ...)
const bindEvent = (eventNames, selector, handler) => {
eventNames.split(' ').forEach((eventName) => {
document.addEventListener(eventName, function (event) {
if (event.target.matches(selector + ', ' + selector + ' *')) {
handler.apply(event.target.closest(selector), arguments)
}
}, false)
})
}
@brandonros
brandonros / grpc+avro.js
Created March 27, 2019 12:49
gRPC + SSL with Avro encoding in node.js
const grpc = require('grpc')
const fs = require('fs')
const avro = require('avsc')
const hexdump = require('hexdump-nodejs')
const encodeMessage = (avroType, request) => {
let opts = {
codec: null,
writeHeader: true
}
@brandonros
brandonros / isotp-frames.js
Last active June 3, 2019 18:04
node.js implementation of chunking a CAN response into ISO-TP frames
const FIRST_FRAME_PCI_BYTE = 0x01
const CONSECUTIVE_FRAME_PCI_BYTE = 0x02
const FIRST_FRAME_DATA_LENGTH = 5
const CONSECUTIVE_FRAME_DATA_LENGTH = 7
const PADDING_BYTE = 0xAA
const buildFirstFrame = (responseSid, data) => {
const responseLength = data.length + 1 // add a byte for response SID
const firstFrameData = data.slice(0, FIRST_FRAME_DATA_LENGTH)
const firstFrameHeader = Buffer.from([
@brandonros
brandonros / stripe.js
Last active July 6, 2019 21:25
Easiest node.js Stripe API implementation I could come up with
const rp = require('request-promise-native')
const apiRequest = (uri, form) => {
return rp.post({
uri,
form,
json: true,
headers: {
authorization: `Basic ${Buffer.from(`${process.env.STRIPE_API_KEY}:`).toString('base64')}`
}
@brandonros
brandonros / gist:d992a1836671e4e43cad0943aeefca91
Created November 17, 2019 17:46
How to compile libusb in XCode for iPhoneOS 13
See comment/image/screenshot.
@brandonros
brandonros / lit-element.html
Last active February 18, 2024 21:15
lit-element example with unpkg (no build), async API fetch() call, loading state, and styling
<!doctype html>
<html>
<head>
<script src="https://unpkg.com/@webcomponents/webcomponentsjs@2.0.0/custom-elements-es5-adapter.js"></script>
<script src="https://unpkg.com/@webcomponents/webcomponentsjs@2.0.0/webcomponents-bundle.js"></script>
<script type="module">
import { LitElement, css, html } from 'https://unpkg.com/lit-element@2.2.1/lit-element.js?module'
class SimpleGreeting extends LitElement {
static get styles() {
@brandonros
brandonros / lit-element-spa.html
Last active November 22, 2019 02:42
Outline for single-page application (router + links + app container + routes) with lit-element
<!doctype html>
<html>
<head>
<script type="module">
import { LitElement, css, html } from 'https://unpkg.com/lit-element@latest/lit-element.js?module'
class RouterLink extends LitElement {
static get properties() {
return {
href: { type: String }
drivers/usb/serial/pl2303.c:993 [pl2303]pl2303_process_read_urb =_ "%s - tty_flag = %d\012"
drivers/usb/serial/pl2303.c:943 [pl2303]pl2303_read_int_callback =_ "%s - nonzero urb status received: %d\012"
drivers/usb/serial/pl2303.c:939 [pl2303]pl2303_read_int_callback =_ "%s - urb shutting down with status: %d\012"
drivers/usb/serial/pl2303.c:855 [pl2303]pl2303_set_break =_ "%s - turning break %s\012"
drivers/usb/serial/pl2303.c:803 [pl2303]pl2303_tiocmget =_ "%s - result = %x\012"
drivers/usb/serial/pl2303.c:644 [pl2303]pl2303_set_termios =_ "parity = none\012"
drivers/usb/serial/pl2303.c:639 [pl2303]pl2303_set_termios =_ "parity = even\012"
drivers/usb/serial/pl2303.c:636 [pl2303]pl2303_set_termios =_ "parity = space\012"
drivers/usb/serial/pl2303.c:631 [pl2303]pl2303_set_termios =_ "parity = odd\012"
drivers/usb/serial/pl2303.c:628 [pl2303]pl2303_set_termios =_ "parity = mark\012"
No. Time Source Destination Protocol Length Text item Info usb.bmRequestType usb.bRequest usb.wValue usb.wIndex usb.wLength usb.data_fragment usb.capdata
47 10.860227 host 2.1.0 USB 36 ✓ URB_CONTROL in 0xc0 1 0x8484 0 (0x0000) 1
49 10.860227 host 2.1.0 USB 36 ✓ URB_CONTROL out 0x40 1 0x0404 207 (0x00cf) 0
51 10.875827 host 2.1.0 USB 36 ✓ URB_CONTROL in 0xc0 1 0x8484 0 (0x0000) 1
53 10.875827 host 2.1.0 USB 36 ✓ URB_CONTROL in 0xc0 1 0x8383 0 (0x0000) 1
55 10.891427 host 2.1.0 USB 36 ✓ URB_CONTROL in 0xc0 1 0x8484 0 (0x0000) 1
57 10.891427 host 2.1.0 USB 36 ✓ URB_CONTROL out 0x40 1 0x0404 222 (0x00de) 0
59 10.907027 host 2.1.0 USB 36 ✓ URB_CONTROL in 0xc0 1 0x8484 0 (0x0000) 1
61 10.907027 host 2.1.0 USB 36 ✓ URB_CONTROL in 0xc0 1 0x8383 0 (0x0000) 1
69 10.908027 host 2.1.0 USB 36 ✓ URB_CONTROL in 0xc0 1 0x008c 0 (0x0000) 2
@brandonros
brandonros / index.js
Last active May 3, 2020 17:45
cp2012 Robotell USB-CAN Adapter userspace implementation
const usb = require('usb')
const util = require('util')
const assert = require('assert')
const USB_DIR_OUT = 0
const USB_TYPE_VENDOR = (0x02 << 5)
const USB_RECIP_DEVICE = 0x00
const _PACKET_ESC = 0xA5
const _PACKET_HEAD = 0xAA