Arduino: ESP32-S2 USB modes

I picked up some new ESP32-S2 boards to play around with. These newer boards come with native USB on board, instead of a separate USB to serial chip to handle communications. With this new chip, there are some new USB acronyms in the Arduino menus. I kept getting them confused so I looked them all up and am committing them here for future reference.

Mode Explanation
USB DFU Device Firmware Upgrade: Upload mode
USB CDC Communication and Data Control: UART/Serial mode
USB MSC Mass storage device class
USB HID Human Interface Device: Mouse/Keyboard emulation
Leave A Reply

Comparison table for ESP boards

Simplified table of ESP boards with components I care about.

ESP8266 ESP32 ESP32-S2 ESP32-S3 ESP32-C3
GPIO 17 34 43 45 22
CPU 1x @ 160Mhz 2x @ 240Mhz 1x @ 240Mhz 2x @ 240Mhz 1x @ 160Mhz
Architecture Tensilica Tensilica Tensilica Tensilica RISC-V
RAM 160K 520K 320K 512K 400K
Touch Pins 0 10 14 14 0
Bluetooth No 4.2 No 5.0 5.0
USB OTG No No Yes Yes No

More details and Espressif product comparison

Note: Assume about eight pins are used internally to connect flash and other peripherals.

Leave A Reply

Force SSH to ask for a password and skip keys

Normally I use SSH keys (with a password) to login to remote machines. Today I needed to force SSH to use a password to verify a change. Here is the command:

ssh -o PubkeyAuthentication=no user@server.domain.com

Leave A Reply

Books of 2023

List of books I read in 2023. Also see the list of 2022. The date indicated denotes the date I started reading the book.

2023-01-04 - The Twisted Ones by T. Kingfisher - 385 pages by
2023-01-29 - A Psalm For The Wild-built by Becky Chambers - 147 pages
2023-01-31 - 1984 by George Orwell - 298 pages
2023-02-08 - The House In The Cerulean Sea by T. J. Klune - 385 pages
2023-02-27 - A Court Of Thorns And Roses by Sarah J. Maas - 416 pages

Xanth

2023-01-10 - Isle Of View - Xanth #13 by Piers Anthony - 344 pages
2023-02-15 - Question Quest - Xanth #14 by Piers Anthony - 338 pages
2023-03-06 - The Color Of Her Panties - Xanth #15 by Piers Anthony - 342 pages

Harry Potter

2023-01-15 - Harry Potter And The Sorcerer's Stone by J. K. Rowling - 309 pages
2023-02-04 - Harry Potter And The Chamber Of Secrets by J. K. Rowling - 341 pages
2023-03-10 - Harry Potter And The Prisoner Of Azkaban by J. K. Rowling - 435 pages

2023-xx-xx - Harry Potter And The Goblet Of Fire by J. K. Rowling - 734 pages
2023-xx-xx - Harry Potter And The Order Of The Phoenix by J. K. Rowling - 870 pages
2023-xx-xx - Harry Potter And The Half-blood Prince by J. K. Rowling - 652 pages
2023-xx-xx - Harry Potter And The Deathly Hallows by J. K. Rowling - 759 pages

Dresden Files

2023-01-20 - Cold Days - Dresden Files #13 by Jim Butcher - 614 pages
2023-02-18 - Skin Game - Dresden Files #14 by Jim Butcher - 600 pages
2023-03-16 - Ghost Story - Dresden Files #15 by Jim Butcher - 578 pages

Leave A Reply

PHP: Find the second (or third) occurrence of a substr in a string

I needed to find the second occurrence of a substring inside of a larger string. PHP has strpos() which gets you the first occurrence, but nothing beyond that. I wrote a wrapper function around strpos() to let you specify the number you want to find. Returns false if nothing is found.

function strpos_num(string $haystack, string $needle, int $num) {
    $offset = 0;
    $length = strlen($needle);
    $pos    = null;

    for ($i = 0; $i < $num; $i++) {
        $pos = strpos($haystack, $needle, $offset);

        // Short circuit continued lookups if we don't find anything
        if ($pos === false) { return false; }

        $offset = $pos + $length;
    }

    return $pos;
}
Leave A Reply

Perl: Human size in color

I use human_size() a lot in Perl, and sometimes it's nice to have a colored version. Here is a quick colorized version:

sub human_size_c {
    my $size = shift();
    if (!$size) { return undef; }

    if    ($size >= (1024**5) * 0.98) { $size = sprintf("\e[38;5;167m%.1fP\e[0m", $size / 1024**5); }
    elsif ($size >= (1024**4) * 0.98) { $size = sprintf("\e[38;5;105m%.1fT\e[0m", $size / 1024**4); }
    elsif ($size >= (1024**3) * 0.98) { $size = sprintf("\e[38;5;45m%.1fG\e[0m" , $size / 1024**3); }
    elsif ($size >= (1024**2) * 0.98) { $size = sprintf("\e[38;5;47m%.1fM\e[0m" , $size / 1024**2); }
    elsif ($size >= 1024)             { $size = sprintf("\e[38;5;226m%.1fK\e[0m", $size / 1024);    }
    elsif ($size >= 0)                { $size = sprintf("\e[38;5;160m%dB\e[0m"  , $size);           }

    return $size;
}

See also: Original human_size()

Leave A Reply

Perl: Remove an item from array

If you want to remove an item from an array you can use a inverse grep filter like this:

my @x = qw(foo bar baz orange);
@x    = grep { !/orange/ } @x;

or

my @x = qw(foo bar baz orange);
@x    = grep { $_ ne 'orange' } @x;
Leave A Reply

Perl: Read from multiple files with one filehandle

I have a directory of data files I wanted to read line-by-line simply. You can loop through each file, open a filehandle, process the lines, close the filehandle, but that can be repetitive. Perl has a unique mechanism where it will iterate across all the files in the @ARGV array automatically. You can fake out the @ARGV array with your own list of files and then iterate accordingly:

local @ARGV = sort(glob("/tmp/data/*.txt"));

# Special ARGV filehandle reads all the files sequentially
while (my $line = readline(ARGV)) {
    print $line;
}
Leave A Reply

Perl: Creating a reference to a subroutine

Perl allows you to create a reference to subrouting and store it in a variable. This allows subroutines to be passed around to other functions. In Perl speak these are called coderefs. There are two ways to create them:

my $one = sub { print "Hello world!"; }
my $two = \&hello_world;

sub hello_world {
    print "Hello world!";
}

Calling a code reference is simple:

$coderef->(); # No params
$coderef->($param1, $param2);
Leave A Reply

Perl: Sort an array of IP addresses

I have a list of IP addresses that I want sorted in a human readable fashion. A simple sort() on a list of IPs will not work because the octets may be: one, two, or three digits long which confuses sort(). Here is a simple sorting function for a list of IP addresses:

my @ips    = qw(198.15.0.20 4.2.2.1 10.11.1.1 10.100.1.1 65.182.224.40);
my @sorted = sort by_ip_address @ips;
sub by_ip_address {
    return ip2long($a) <=> ip2long($b);
}

Note: You will need my ip2long() function for this to work.

Leave A Reply

Linux: Check if a process is running

You can list all the running processes on a Linux box with ps aux, but often you're looking for a specific process. This is pretty easily accomplished with grep:

ps aux | grep /usr/sbin/sshd

The problem with this is that you often pick up you own grep in the output:

$ ps aux | grep /usr/sbin/sshd
root         883  0.0  0.0  76640  7428 ?        Ss   Oct18   0:00 /usr/sbin/sshd -D -oCiphers...
bakers     11691  0.0  0.0  12148  1104 pts/0    S+   08:09   0:00 grep --color=auto /usr/sbin/sshd

The quick and dirty solution is to do some trickery with a regular expression and grep:

$ ps aux | grep -P '/usr/sbin/[s]shd'
root         883  0.0  0.0  76640  7428 ?        Ss   Oct18   0:00 /usr/sbin/sshd -D -oCiphers...

The square brackets tell grep to match a character class with only one character in it. This prevents grep from picking up itself, but still matches what you want.

Leave A Reply

Making ffmpeg truly quiet (non verbose)

ffmpeg is a great program, but can be rather verbose in it's output. This can be problematic if you're trying to use ffmpeg in a script. The best way to make ffmpeg truly silent in it's output is with the following options:

ffmpeg -hide_banner -loglevel error ...

This will hide the startup banner, and any encoding stats. Assuming your encode goes normally you shouldn't get any output at all.

Leave A Reply

Perl: Create a temporary file that's automatically removed on script termination

I need a random temporary file to put some data in while my script executes. The file should be removed automatically after the script completes. Enter File::Temp which handles all of this for you.

use File::Temp;

my ($fh, $filename) = File::Temp::tempfile(UNLINK => 1);

Alternately if you need a temporary directory that's automatically removed on completion you can use:

use File::Temp;

my $dir = File::Temp::tempdir(CLEANUP => 1);

Note: File::Temp is a core module, so you already have it.

Leave A Reply

CentOS Stream and Rocky Linux End of Life

CentOS and Rocky end of life:

Distro Active Support Security
Centos 7 Aug 6, 2020 Jun 30, 2024
Centos 8 Stream May 31, 2024 N/A
Centos 9 Stream May 31, 2027 N/A
Rocky Linux 8 May 31, 2024 May 31, 2029
Rocky Linux 9 May 31, 2025 May 31, 2032
Leave A Reply

Using Vim as a time machine

If you're editing a file in Vim you can rewind time to a previous version of the file with the earlier command. You can go backwards (or forwards) in the history of a file based on a given time measurement. This can be helpful if you mess up your file and just want to rollback to a previous version.

:earlier 5m

or

:later 5m

Reddit had some interesting discussion on what you can do with this feature.

Note: Alternately you can use :e! to reload the file from disk if you haven't saved since your mess up.

Leave A Reply