Skip to content

Instantly share code, notes, and snippets.

View Jeff-Russ's full-sized avatar

Jeffrey Russ Jeff-Russ

View GitHub Profile
@Jeff-Russ
Jeff-Russ / arduinoVCC.md
Last active October 31, 2018 16:11
Accurate Arduino analogRead() with a Simple VCC Detection

Accurate Arduino analogRead() with a Simple VCC Detection

This is a function to determine your Arduino's supply voltage automatically in order to convert the results of analogRead into a much more accurate voltage. As you may know, analogRead() returns a value that's relative to the supply voltage to the Arduino, which can deviate. If you feed a known voltage to an analog pin, this function can read and use it to calculate what Arduino's supply voltage is:

double getArduinoVcc(int analog_pin, double expected_volt)
{

double received_frac = (double)analogRead(analog_pin) / 1023.0;

@Jeff-Russ
Jeff-Russ / wrap_to_range.h
Last active October 31, 2018 05:00
C/C++ function to wrap out-of-bounds numbers to a specific range (modulo with an offset)
/* wrap_to_range() is similar to modulo but, instead of wrapping back to zero, wraps
back to specified range closer to the top (what the modulo value would be).
It was created for musical keyboard to handle a note command that is too high to be
played in order to handle that event that so that the specified note is still played
just in a different octave (the highest octave possible).
For those where that useage doesn't ring true, imagine a clock function (in 24 hour
time) where if 24:01 is attempted to be set (an invalid time) it is adjusted to 23:01.
The same 23:01 would be returned if 25:01 or 26:01 are attempted.
The second arg is the max (allowed) value
@Jeff-Russ
Jeff-Russ / download_numbered_files.rb
Last active July 24, 2018 10:26 — forked from johnjohndoe/download-url-to-file.rb
Ruby script to download a number of files from individual URLs via HTTP/HTTPS/FTP specified in an external file.
#!/usr/bin/env ruby
#
# Ruby script to download many numbered of files
# from individual URLs via HTTP/HTTPS/FTP
# specified in an external file.
#
# Author: Jeff Russ
# Original Author: Tobias Preuss
# License: Creative Commons Attribution-ShareAlike 3.0 Unported
@Jeff-Russ
Jeff-Russ / phpDoc Usage Guide.md
Last active May 31, 2017 16:14
phpDocumentor Usage Guide

PHPDoc Usage Guide

Basic Info

@version [version] Provides the version number of a class or method
@author Author Name <author@email> The author of the current element
@copyright name date Documents copyright information
@package name of a package A group of related classes and functions
@subpackage sub-package under @package classes or functions
@todo Documents things that need to be done to the code at a later date

Handy Regex Patterns

Here are some handy regex patterns and portions of patterns. I seem to keep coming back to these so it made sense to document them for the sake of memory.

Numerics

Signed integers or floats. Skipping the zero before the decimal, for example .1, +.1 or -.1 , is valid.

@Jeff-Russ
Jeff-Russ / Sequence.md
Last active March 1, 2017 18:17
PHP: Sequential Array Class

Sequence and TSequence

TSequence is an array trait with numerically sortable keys. Keys can be negative or positive integers or floats. It does not matter whether they are actually numeric types or string and if they are strings they are allowed to contain any character so long as they start with something numeric. The non-numeric ending of the string key, if present, is called the 'tag'.

You can optionally protect to an already assigned element by automatically incrementing the numeric portion of the key you provided until a free slot is

@Jeff-Russ
Jeff-Russ / regex_capture_checker.php
Last active February 21, 2017 17:18
Regex Capture Checker (in PHP)
<?php
function regex_capture_checker($regex_pat, $strings) {
foreach ($strings as $k => $v) {
if (is_integer($k)) {
$string = $v;
$comp = false;
} else {
$string = $k;
$comp = $v;
}
@Jeff-Russ
Jeff-Russ / recursive_search.php
Last active February 18, 2017 05:09
PHP Recursive Array Search
<?php
function recursive_search($val, $arr, $mode=1, $strict=true)
{
$depth = ($depth=(int)$mode) ? $depth : 0;
if (strpos($mode, 'all')!==false) {
$r = function ($val, $a, $d, $s, $l=0, &$path=[], &$keys=[], &$i=0) use (&$r) {
foreach($a as $k=>$v) {
$path[] = $k;
if (!$s && $v==$val || $v===$val) $keys[$i++] = $path;
elseif ($l<$d && (is_array($v) /*|| method_exists($v,'getIterator')*/))
@Jeff-Russ
Jeff-Russ / PHP: array_insert.php
Last active February 10, 2017 21:52
PHP: array_insert() to insert at index, non-destructively
<?php
/**
* Insert a value at a specified integer index non-destructively with minimal
* impact on indexing. Indexes below the insert location are not changed and
* indexes above the insertion are shift up until the point where a free slot is
* found, above which indexing is unaltered.
*
* As for sorting, integer indexes will sorted up to this free slot but not above.
* Every element above this location, whether the key is an integer or string,
* is appended to the end of the array unsorted. If the 4th argument is true the
@Jeff-Russ
Jeff-Russ / Bash: Pretty Prompt.md
Last active February 3, 2017 21:55
Bash: Pretty Prompt

Bash: Pretty Prompt

place this in ~/.bashrc or ~/.bash_profile

_pretty_prompt() {
	# args: $maincolor_int, $dircolor_int, $gitcolor_int, $style_str 
	# color ints: green 32, yellow 33, cyan 36, white 37, reset(none) 00
	# style: '1;2;4;' means bold dim and underline. leave off for no style