Skip to content

Instantly share code, notes, and snippets.

@jamesgregson
jamesgregson / NodeMCU_JSON-RPC.ino
Last active November 1, 2023 15:34
JSON-RPC with NodeMCU and ArduinoJSON
#include <ArduinoJson.h>
#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
// change the IP address in the following line, should return "hello world" wrapped in JSON-RPC boilerplate
// curl -d '{"jsonrpc": "2.0", "method": "concatenate", "params": ["hello", "world"], "id": 5}' -H "Content-Type: application/json" -X POST http://192.168.0.24/api
ESP8266WebServer http_server(80);
@jamesgregson
jamesgregson / euler.py
Last active September 6, 2019 13:26
Euler angles to rotation matrices and vice versa
"""Euler-angle to matrix and matrix to Euler-angle utility routines
This module provides utility routines to convert between 4x4 homogeneous
transformation matrices and Euler angles (and vice versa). It supports
all six rotation orders ('xyz','xzy','yxz','yzx','zxy','zyx') for rotations
around body axes.
Two primary functions are provided:
- rotation( theta, order='xyz' ): given input rotation angles in radians
@jamesgregson
jamesgregson / fsm.cpp
Created September 13, 2019 15:41
Fast Sweeping Method in 2D and 3D using PyBind11
/*cppimport
<%
setup_pybind11(cfg)
%>
*/
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
namespace py = pybind11;
#include <cmath>
@jamesgregson
jamesgregson / kd_tree.h
Last active September 24, 2019 14:29
Simple KDTree (3D)
#ifndef __KD_TREE_HEADER_H
#define __KD_TREE_HEADER_H
#include <set>
#include <list>
#include <tuple>
#include <queue>
#include <vector>
#include <algorithm>
@jamesgregson
jamesgregson / camera-server.py
Created September 29, 2019 20:50
Python3 Stereo MJPG streaming with opencv
import cv2
import numpy as np
from http.server import BaseHTTPRequestHandler,HTTPServer
import io
import socket
import time
capture0 = None
capture1 = None
@jamesgregson
jamesgregson / server.py
Created October 16, 2019 04:01
Python3 http.server with basic authorization
import time
import base64
from http.server import BaseHTTPRequestHandler, HTTPServer
class MyHandler(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
@jamesgregson
jamesgregson / contract.py
Last active October 26, 2019 23:26
Simple contract implementation with decorator
import inspect
def contract( expects=[], ensures=[] ):
def func_wrapper( func ):
return Contract(function=func,preconditions=expects,postconditions=ensures )
return func_wrapper
class Contract:
class Args:
@jamesgregson
jamesgregson / arap.py
Last active March 17, 2023 22:10
Python ARAP mesh deformation
from typing import *
import time
import numpy as np
import scipy.sparse as sparse
import scipy.sparse.linalg as spla
def build_cotan_laplacian( points: np.ndarray, tris: np.ndarray ):
a,b,c = (tris[:,0],tris[:,1],tris[:,2])
A = np.take( points, a, axis=1 )
B = np.take( points, b, axis=1 )
@jamesgregson
jamesgregson / jsonrpc_example.py
Created January 4, 2020 19:05
(Over) Simplified JSONRPC in Python
'''Very simplified JSON-RPC server with endpoint registration
Try with:
curl -X POST -H "Content-type: application/json" -d '{"jsonrpc": "2.0", "id": 1, "method": "say_hello", "params": {"name": "James", "age": 11}}' http://127.0.0.1:8000
curl -X POST -H "Content-type: application/json" -d '{"jsonrpc": "2.0", "id": 1, "method": "say_hello", "params": {"name": "James"}}' http://127.0.0.1:8000
curl -X POST -H "Content-type: application/json" -d '{"jsonrpc": "2.0", "id": 1, "method": "say_hello", "params": ["James", 10]}' http://127.0.0.1:8000
curl -X POST -H "Content-type: application/json" -d '{"jsonrpc": "2.0", "id": 1, "method": "say_hello", "params": ["James"]}' http://127.0.0.1:8000
'''
@jamesgregson
jamesgregson / filter.py
Created July 10, 2020 03:11
Spatial, Fourier and matrix representations of 2D filtering operations
import unittest
import numpy as np
import scipy.sparse as sparse
class Filter2D:
def __init__( self, offy, offx, vals ):
'''2D filtering operation with spatial, fourier and matrix features
Args:
- offy (integer sequence): vertical offsets of filter coefficients