Skip to content

Instantly share code, notes, and snippets.

@bcr
bcr / Ntp.cs
Created June 7, 2023 19:14
C# NTP Code
using System.Net;
using System.Net.Sockets;
// https://stackoverflow.com/questions/1193955/how-to-query-an-ntp-server-using-c
static DateTime GetNetworkTime()
{
const string ntpServer = "pool.ntp.org";
var ntpData = new byte[48];
ntpData[0] = 0x1B; //LeapIndicator = 0 (no warning), VersionNum = 3 (IPv4 only), Mode = 3 (Client Mode)
@bcr
bcr / pin.py
Created February 3, 2023 18:03
PIN encoding
#!/usr/bin/env python3
pin = input("Please enter a five digit numeric PIN > ")
integer_pin = int(pin)
print(f"{integer_pin:021_b}")
@bcr
bcr / status.py
Created January 10, 2023 07:38
Simple status bar for Python
class StatusBar:
def __init__(self, max_value, max_width):
self.max_value = max_value
self.max_width = max_width
self.update(0)
def update(self, new_value):
number_markers = new_value * (self.max_width - 2) // self.max_value
marker_string = "#" * number_markers
print("\r[" + marker_string + (" " * (self.max_width - len(marker_string) - 2)) + "]", end='')
@bcr
bcr / turning.js
Created September 6, 2017 00:59
Circuit Playground MakeCode turning test
let current_x = 0
let turning_right = 0
let turning_left = 0
let threshold_changed = 0
let threshold_increment = 0
let turning_threshold = 0
input.buttonA.onEvent(ButtonEvent.Click, function () {
turning_threshold += threshold_increment
turning_threshold = Math.constrain(turning_threshold, 0, 1023)
threshold_changed = 1
@bcr
bcr / displaytest.py
Created June 10, 2017 01:58
Minimal CircuitPython display code
import board
import busio
import digitalio
import ili9341
spi = busio.SPI(clock=board.SCK,MOSI=board.MOSI,MISO=board.MISO)
spi.configure(baudrate=23000000)
cs = digitalio.DigitalInOut(board.D9)
dc = digitalio.DigitalInOut(board.D10)
display = ili9341.ILI9341(spi, cs=cs, dc=dc)
display.fill(ili9341.color565(255, 0, 0))
@bcr
bcr / ocr.py
Created June 18, 2016 06:31
OpenCV fake OCR example
#https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_image_display/py_image_display.html#display-image
#http://docs.opencv.org/3.0-beta/modules/imgproc/doc/miscellaneous_transformations.html
#http://docs.opencv.org/2.4/doc/tutorials/imgproc/threshold/threshold.html
import cv2
# Original image for annotation
img = cv2.imread('CSH-drink-4k.jpg')
# Grayscale image
@bcr
bcr / keybase.md
Created March 27, 2014 03:14
Keybase proof for bcr

Keybase proof

I hereby claim:

  • I am bcr on github.
  • I am blake (https://keybase.io/blake) on keybase.
  • I have a public key whose fingerprint is EAF8 5937 D351 EF6D 4780 A3A8 BD9A A265 8023 E9CD

To claim this, I am signing this object:

@bcr
bcr / gist:717647
Created November 27, 2010 06:44
Get a stream on a URL or a file
private static Stream GetFileOrUrlStream(string filenameOrUrl)
{
// This function should accept the following formats:
//
// http://example.com/downloads/mybitstream.bit
// mybitstream.bin
// ..\..\mybitstream.bin
// C:\dir\mybitstream.bin
// If there is no colon, then it must be a file reference. Make it
// mcs -reference:nunit.framework ReadableRegex.cs && nunit-console ReadableRegex.exe
using NUnit.Framework;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
[TestFixture]
public class ReadableRegex
{
#include <iostream>
class A
{
public:
A() : m_member(42)
{
std::cout << "A::A" << std::endl;
}