Skip to content

Instantly share code, notes, and snippets.

@n8allan
n8allan / LimitedWorkers.cs
Created February 13, 2024 03:05
Processes items in a list asynchronously up to n items at a time as an async enumerable.
public static class LimitedWorkers
{
public static async IAsyncEnumerable<TO> EnumerateAsync<TI, TO>(this IEnumerable<TI> items, Func<TI, TO> taskFunc, int maxConcurrency, Action<TI, Exception>? handleError)
{
var semaphore = new SemaphoreSlim(maxConcurrency);
var tasks = new List<Task<TO>>();
var taskItems = new Dictionary<Task<TO>, TI>();
foreach (var item in items)
{
@n8allan
n8allan / ExponentialFilter.cpp
Created September 14, 2023 21:01
Exponential filter in C++
int getSmooth6() {
// ASSUMPTION: history has at least 6 entries
auto a = (getOffset(0) + getOffset(-1)) / 2;
auto b = (getOffset(0) + getOffset(-2)) / 2;
auto c = (getOffset(0) + getOffset(-3)) / 2;
auto d = (getOffset(-1) + getOffset(-2)) / 2;
auto e = (getOffset(-1) + getOffset(-3)) / 2;
auto f = (getOffset(-2) + getOffset(-3)) / 2;
return (((((a + b * 2) / 3 + c * 2) / 3 + d * 2) / 3 + e * 2) / 3 + f * 2) / 3;
}
@n8allan
n8allan / libpsresample.cpp
Created March 15, 2023 17:17
Interface for the Raspberry Pi Broadcom ISP (/dev/video12) via V4L2 M2M
#include <stdio.h>
#include <stdlib.h>
#include <memory>
#include <cstring>
#include <vector>
#include <string.h>
#include <assert.h>
#include <getopt.h> /* getopt_long() */
#include <fcntl.h> /* low-level i/o */
@n8allan
n8allan / Concentric.cs
Created January 5, 2023 22:45
Concentric circle generation with no gaps
public static class Concentric
{
/// <summary> Enumerates the rings of a circle starting from 1 radius up to the given radius. </summary>
public static IEnumerable<Point> EnumerateConcentric(int centerX, int centerY, int radius)
{
yield return new Point(centerX, centerY);
for (var r = 1; r <= radius; ++r)
foreach (var point in EnumerateRadius(centerX, centerY, r))
yield return point;
}
@n8allan
n8allan / bash.cs
Created September 14, 2022 18:50
C# async Bash function
public static Task<string> Bash(string cmd, int[] acceptableCodes = null)
{
var source = new TaskCompletionSource<string>();
var escapedArgs = cmd.Replace("\"", "\\\"");
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "bash",
Arguments = $"-c \"{escapedArgs}\"",
@n8allan
n8allan / rpi4_compute.c
Last active February 21, 2022 17:27
Towards headless (no display) OpenGL ES Compute Shader in Raspberry Pi CM 4
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <gbm.h>
#include <EGL/egl.h>
#include <GLES3/gl31.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>