Skip to content

Instantly share code, notes, and snippets.

@greed9
Created August 28, 2022 23:45
Show Gist options
  • Save greed9/3a06a5286089ca8cc39850475f66e9d7 to your computer and use it in GitHub Desktop.
Save greed9/3a06a5286089ca8cc39850475f66e9d7 to your computer and use it in GitHub Desktop.
Programs to map values for ping times into color values and meter setting
#!/bin/bash
# pings specified host once, greps out latency
# usage:
# generate_pings.sh <host> <ping_time_limit>
host=$1
ping_scale_limit=$2
ping_time=$(ping -c1 $host | grep from | cut -d '=' -f 4 | cut -d " " -f 1)
printf "%3.0f 0 %3.0F 0 255\n" $ping_time $ping_scale_limit
# map values for ping times into color values and meter setting
# usage
# echo x in_min in_max out_min out_max | awk -f this program
# outputs the scaled x value and r, g, b values
BEGIN{
# RGB color table -- roughly a spectrum
colors[1]="255 0 0"
colors[2]="255 128 0"
colors[3]="255 255 0"
colors[4]="128 255 0"
colors[5]="0 255 0"
colors[6]="0 255 128"
colors[7]="0 255 255"
colors[8]="0 128 255"
colors[9]="0 0 255"
colors[10]="127 0 255"
colors[11]="255 0 255"
colors[12]="255 0 127"
}
{
x = $1
x = constrain( x, $2, $3 )
result = map( x, $2, $3, $4, $5 )
if( result < 0 ) result = 0
color_index = int(map(result, 0, $5, 12, 1))
printf "%d %s\n", result, colors[color_index]
}
function constrain( in_x, min_x, max_x ) {
if( in_x < min_x ) in_x = min_x
if( in_x > max_x ) in_x = max_x
return in_x
}
# implement Arduino's map function in awk
# based on https://www.arduino.cc/reference/en/language/functions/math/map/
#long map(long x, long in_min, long in_max, long out_min, long out_max) {
# return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
#}
function map(x, in_min, in_max, out_min, out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
}
#!/bin/bash
# setup for use with cron (1 minute intervals)
. ~jerry/.profile
/home/jerry/generate_pings.sh yahoo.com 500 | awk -f /home/jerry/map.awk | /home/jerry/rgb_lights.sh
#!/bin/bash
# usage:
# rgb_lights.sh <meter> <red> <green> <blue>
# where value is between 0 and 255
read meter red green blue
# pins for rgb led use with pigs command
meter_pin=23
red_pin=17
green_pin=27
blue_pin=22
#echo $meter_pin $meter
pigs p $meter_pin $meter
pigs p $red_pin $red
pigs p $green_pin $green
pigs p $blue_pin $blue
printf '%(%Y-%m-%d %H:%M:%S)T' -1
printf ",%d\n" $meter
@greed9
Copy link
Author

greed9 commented Aug 28, 2022

The pigs command comes from the pipgpio library:.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment