Skip to content

Instantly share code, notes, and snippets.

View mstum's full-sized avatar

Michael Stum mstum

View GitHub Profile
ATX Power Controller
* ATX Breakout Board is Amazon ASIN B01MY7KK80
Basically:
* Connect PS_ON and GND from the PSU to a Relay's NO connectors. Leave the NC connector just floating.
* Connect the Trigger Pin of the Relay to Pin 9 (RELAY_PIN) of the Arduino
* Connect the Power Button of the case to Pin 12 (PWRBTN_PIN) of the Arduino, and GND
* Connect the Power LED of the case to Pin 10 (PSUPOWER_LED) of the Arduino, and GND, using an appropriate resistor (e.g., 330 Ohm)
* Connect a Piezo Speaker to Pin 11 (SPKR_PIN) of the Arduino, and GND
@mstum
mstum / AtomicFlag.cs
Created March 2, 2019 19:58
AtomicFlag
class Program
{
static void Main(string[] args)
{
var f = new AtomicFlag(true);
Console.WriteLine($"Value: {f.Value}");
var oldVal = f.CompareExchange(true, false);
Console.WriteLine($"Old Val: {oldVal}, Value: {f.Value}");
oldVal = f.CompareExchange(true, false);
Console.WriteLine($"Old Val: {oldVal}, Value: {f.Value}");
@mstum
mstum / tomcat
Created September 8, 2018 21:33
#!/bin/sh
### BEGIN INIT INFO
# Provides: tomcat
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start and stop Apache Tomcat
# Description: Enable Apache Tomcat service provided by daemon.
@mstum
mstum / Benchmark.txt
Last active April 17, 2023 15:02
Fully Managed alternative to StrCmpLogicalW
BenchmarkDotNet=v0.10.13, OS=Windows 10 Redstone 3 [1709, Fall Creators Update] (10.0.16299.309)
Intel Core i7-6700HQ CPU 2.60GHz (Skylake), 1 CPU, 8 logical cores and 4 physical cores
Frequency=2531253 Hz, Resolution=395.0613 ns, Timer=TSC
[Host] : .NET Framework 4.7 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.2633.0
Clr : .NET Framework 4.7 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.2633.0
Job=Clr Runtime=Clr
Method | Mean | Error | StdDev | Allocated |
---------------------------- |---------:|---------:|---------:|----------:|
@mstum
mstum / ToHex.cs
Created March 4, 2018 08:03
Efficient byte[] to string
// Taken from https://www.bouncycastle.org/
// Adapted to create and return a string rather than write to a Stream
// https://twitter.com/mstum/status/970207754207006720
private static readonly byte[] encodingTable =
{
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5', (byte)'6', (byte)'7',
(byte)'8', (byte)'9', (byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f'
};
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
/// <summary>
/// Wrapper for Two Dictionaries to allow lookup by Key or by Value.
/// Note that Value can't ever be null, as Dictionary will throw on a null Key.
/// </summary>
/// <typeparam name="TKey"></typeparam>
@mstum
mstum / LimitContentTypesFilterAttribute.cs
Created January 26, 2018 17:18
ASP.net Core Filter to limit allowed Content-Types per method.
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Stuff.Web
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class LimitContentTypesFilterAttribute : Attribute, IResourceFilter
public static class TreeDrawer
{
/// <summary>
/// Output a Tree to the console
/// </summary>
/// <remarks>
/// Example Output:
///
/// NodeName
/// ├─ NodeName
@mstum
mstum / gist:2fb41dfe16a9fd195b126d9b1fe75fd2
Last active July 10, 2017 02:08
Remotely manage Hyper-V when not domain Joined
Server:
* Enable-PSRemoting (use sconfig for that)
* Enable-WSManCredSSP -Role Server
Client:
* Run PowerShell as Admin
* Enable-PSRemoting
* Set-Item WSMan:\localhost\Client\TrustedHosts -Value "fqdn-of-hyper-v-host"
* -Concatenate can be used to add
* https://stackoverflow.com/questions/21548566/how-to-add-more-than-one-machine-to-the-trusted-hosts-list-using-winrm
@mstum
mstum / MyTestClass.tt
Created June 7, 2017 09:00
T4 Template to implement the Builder Pattern
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#
var nsName = "Testing";
var className = "MyTestClass";
var accessor = "public";