Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # TempScan v21.02.25 for Raspberry Pi by @ThisSteveGuy
- # Configurable temperature monitor with low pass filter
- # Pastebin can mess up bash files, so you may need to run:
- # sed -i 's/\r$//' filename
- # LPF = Low Pass Filter, less erratic and more realistic
- # Min = the lowest temperature found in the latest group of samples
- # Avg = the average temperature of the latest samples
- # Max = guess
- # You'll notice a lot of exact duplicates scrolling up the Min and Max columns.
- # This is because the pi's temperature sensor has a fairly low resolution.
- # The Low Pass Filter (LPF) tries to correct for this shortcoming.
- # The temps surrounding the gradient are for AutoColor mode,
- # they're really just there for testing right now.
- # ___ OPTIONS ___
- # Keep making temperature readings or quit after a single one:
- Continuous=true
- # Scroll the temperature readings up the window or have them stay on the bottom:
- Scroll=true
- # Display colors as bars (colored backgrounds) or just as colored numbers:
- Bars=true
- # An option in case your terminal cannot support color:
- Color=true
- # Places labels underneath the types of measurements:
- ShowLabels=true
- # How relatively dark does a bar have to be before changing the text to white:
- DarkTextFix=100
- # Automatically adjust the cold and hot color temps over time:
- AutoColor=false
- # For setting the min and max colors manually (if AutoColor=false)
- # Temperatures are in hundredths of a degree (4321 = 43.21 degrees C):
- ColdTemp=4000
- HotTemp=5500
- # Adjusts how aggressively the LPF column filters out erroneous readings:
- LowPassFilterStrength=100
- # SELECT COLOR PALLETE BY COMMENTING OUT ALL THE OTHERS WITH # MARKS
- # You must have only one of each (palleteR, palleteG, palleteB) uncommented
- # Colorblind-friendly palette:
- palleteR=( 49 69 116 171 224 255 254 253 244 215 165 255 )
- palleteG=( 54 117 173 217 243 255 224 174 109 48 0 255 )
- palleteB=( 149 180 209 233 248 191 144 97 67 39 38 255 )
- # Weather map color palette:
- #palleteR=( 77 179 0 7 72 247 255 161 251 )
- #palleteG=( 10 0 7 247 180 244 110 1 222 )
- #palleteB=( 163 181 255 240 32 4 0 1 226 )
- # Blue Cyan Green Yellow Red:
- #palleteR=( 0 0 128 255 128 )
- #palleteG=( 0 128 255 128 0 )
- #palleteB=( 128 255 128 0 0 )
- # Blue to Red then White Hot:
- #palleteR=( 0 36 72 108 144 180 255 )
- #palleteG=( 0 0 0 0 0 0 255 )
- #palleteB=( 180 144 108 72 36 0 255 )
- # TIME AND TEMPERATURE SAMPLE CONFIGURATION
- # Number of temperature samples taken within each sample period:
- Samples=3
- # Duration of each sample period (ms):
- SamplingTime=300
- # Wait time between sampling periods (ms):
- UpdateDelay=700
- # Here's what the timeline would look like when those are set to 3, 300, and 700:
- # Sample, wait 150ms, Sample, wait 150ms, Sample, wait 700ms
- # That's 3 Samples in a total SamplingTime of 300ms (150+150) and an UpdateDelay of 700ms.
- # Since 300ms + 700ms = 1000ms, it will take (approximately) 1 second to show each reading.
- # I honestly don't know the how many samples you can/should do within a set period of time.
- # I wouldn't go higher than 10 per second. Any more would be overkill and you don't want
- # the script itself to start affecting the temperature.
- # ___ END OF OPTIONS ___
- letsRock() {
- varInit
- fixWaiter
- if [ $Samples -eq 1 ] ; then
- waitFor=$(($SamplingTime/$Samples))
- else
- waitFor=$(($SamplingTime/($Samples-1)))
- if [ $UpdateDelay -ge $waitFor ] ; then
- UpdateDelay=$(($UpdateDelay-$waitFor))
- else
- waitFor=$(($SamplingTime/$Samples))
- fi
- fi
- colorScale
- while true
- do
- makeSamples
- output
- if [ $Continuous = false ]; then echo "" ; return ; fi
- waiter $UpdateDelay
- done
- }
- colorScale() {
- cSRange=$(($HotTemp-$ColdTemp))
- cSLeft=1
- cSMaking=true
- while [ $cSLeft -le 32 ] ;
- do
- cSColor=$((($ColdTemp+($cSRange/32)*$cSLeft) * $Big * 10))
- formatText cSColor
- cSMsg="${cSMsg}$formattedText"
- cSLeft=$[$cSLeft+1]
- done
- cSMsg="${cSMsg}\E[0m"
- cSMaking=false
- }
- varInit() {
- Big=999
- getTemp
- lpfRough=$tempRaw
- lpfSmpl=$lpfRough
- lpfOLD=$lpfSmpl
- lpfDelta=0
- smooth=100
- sF=$(($LowPassFilterStrength * $LowPassFilterStrength +1))
- topTemp=0
- botTemp=$((99999*$Big))
- colorResolution=10000
- labeled=false
- }
- getTemp() {
- tempRaw=$(cat /sys/class/thermal/thermal_zone0/temp)
- tempRaw=$(($tempRaw*$Big))
- }
- makeSamples() {
- avgSum=0
- maxSmpl=$((-99999*$Big))
- minSmpl=$((99999*$Big))
- for (( k=0; k < $Samples; k++ )); do
- waiter $waitFor
- getTemp
- avgSum=$(($avgSum + $tempRaw))
- if [ $k -eq $(($Samples-1)) ]; then avgSmpl=$(($avgSum/$Samples)); fi
- if [ $tempRaw -gt $maxSmpl ]; then maxSmpl=$tempRaw; fi
- if [ $tempRaw -lt $minSmpl ]; then minSmpl=$tempRaw; fi
- if [ $Samples -gt 1 ] ; then
- lpfRough=$(($lpfRough * ($Samples-1)/$Samples + $tempRaw * 1/$Samples))
- else
- lpfRough=$(($lpfRough * 1/5 + $tempRaw * 4/5))
- fi
- lpfDelta=$(($lpfDelta * ($Samples-1)/$Samples + ($tempRaw - $lpfOLD)/$Big * 1/$Samples))
- smooth=$(($lpfDelta * $lpfDelta / 100))
- if [ $smooth -gt $sF ] ; then smooth=$sF ; fi
- lpfSmpl=$(($lpfSmpl * ($sF-$smooth) / $sF + $lpfRough * $smooth / $sF))
- lpfOLD=$lpfSmpl
- done
- if [ $lpfSmpl -ge $topTemp ] ; then
- topTemp=$lpfSmpl
- formatText topTemp
- topMsg="$formattedText"
- else
- # topTemp=$(($topTemp*149/150+$avgSmpl*1/150))
- formatText topTemp
- topMsg="$formattedText"
- fi
- if [ $lpfSmpl -le $botTemp ] ; then
- botTemp=$lpfSmpl
- formatText botTemp
- botMsg="$formattedText"
- else
- # botTemp=$(($botTemp*149/150+$avgSmpl*1/150))
- formatText botTemp
- botMsg="$formattedText"
- fi
- if [[ $AutoColor = true ]] ; then
- ColdTemp=$((($botTemp / $Big +5) /10))
- HotTemp=$((($topTemp / $Big +5) /10))
- fi
- }
- makeRGB() {
- newColor=$((($1 - $ColdTemp) * $colorResolution / ($HotTemp - $ColdTemp +1 )))
- if [[ $newColor -lt 0 ]] ; then newColor=0 ; fi
- if [[ $newColor -gt $colorResolution ]] ; then newColor=$colorResolution ; fi
- pal=("${palleteR[@]}")
- makeColorComponent pal
- red=$colorComponent
- pal=("${palleteG[@]}")
- makeColorComponent pal
- grn=$colorComponent
- pal=("${palleteB[@]}")
- makeColorComponent pal
- blu=$colorComponent
- lum=$((( $red*299 + $grn*587 + $blu*114 ) /1000 ))
- }
- makeColorComponent() {
- palleteLength=("${#pal[@]}")
- if [ $palleteLength -gt 1 ] ; then
- section=$(( ($palleteLength-1) * $newColor / $colorResolution ))
- leftColor=("${pal[$section]}")
- rightColor=("${pal[$section+1]}")
- shadesPerSection=$(($colorResolution / ($palleteLength-1)))
- shade=$(( ($newColor - $shadesPerSection*$section) * 10000 / $shadesPerSection ))
- colorComponent=$(($shade * ($rightColor - $leftColor) / 10000 + $leftColor))
- else
- colorComponent=("${pal[0]}")
- fi
- }
- output() {
- labels=" \E[38;5;16;48;5;8m LPF \u2588 Min \u2588 Avg \u2588 Max \E[0m"
- formatText lpfSmpl
- lpf="$formattedText"
- formatText minSmpl
- min="$formattedText"
- formatText avgSmpl
- avg="$formattedText"
- formatText maxSmpl
- max="$formattedText"
- Messages="${botMsg}${cSMsg}${topMsg} \t"
- if [[ $Scroll = true ]] ;
- then
- echo -e " ${lpf} ${min} ${avg} ${max} "
- if [[ $ShowLabels = true ]] ; then echo -ne "${labels} $Messages\r"; fi
- else
- if [[ $labeled = false ]] ;
- then
- if [[ $ShowLabels = true ]] ; then echo -e "${labels} (Press Enter to scroll once)"; fi
- labeled=true
- fi
- echo -ne " ${lpf} ${min} ${avg} ${max} $Messages\r"
- fi
- }
- formatText() {
- fourDigits=$((($1 / $Big +5) /10))
- if [[ $Color = true ]] ; then
- makeRGB fourDigits
- if [[ $Bars = true ]] ; then
- if [ $lum -le $DarkTextFix ] ; then textBG="8;2;200;200;200;4" ; else textBG="8;2;0;0;0;4" ; fi
- fi
- tColor="\E[3${textBG}8;2;${red};${grn};${blu}m"
- else
- tColor=""
- fi
- if [[ $cSMaking = true ]] ; then
- if [[ $Bars = true ]] ; then
- formattedText="$tColor "
- else
- formattedText="$tColor|"
- fi
- else
- decimalize fourDigits
- formattedText="$tColor $properNumber \E[0m"
- fi
- }
- decimalize() {
- decimal=$(($1 - ($1 / 100)*100))
- if [ $decimal -lt 10 ] ; then zero="0" ; else zero="" ; fi
- properNumber="$(($fourDigits / 100)).${zero}${decimal}"
- }
- fixWaiter() {
- wTsum=0
- waitAdjust=0
- wTstart=$(date +%s%N)
- waiter 5
- wTfinish=$(date +%s%N)
- wTresult=$((($wTfinish - $wTstart)/1000000))
- waitAdjust=$(($wTsum+$wTresult-5))
- }
- waiter() {
- waitLeft=$(($1))
- while [ $waitLeft -ge 1000 ] ;
- do
- if [ $waitLeft -ge 60000 ]; then sleep 60; waitLeft=$(($waitLeft-60000))
- elif [ $waitLeft -ge 30000 ]; then sleep 30; waitLeft=$(($waitLeft-30000))
- elif [ $waitLeft -ge 10000 ]; then sleep 10; waitLeft=$(($waitLeft-10000))
- elif [ $waitLeft -ge 1000 ]; then sleep 1; waitLeft=$(($waitLeft-1000))
- fi
- done
- while [ $waitLeft -ge 5 -a $waitLeft -lt 1000 ] ;
- do
- if [ $waitLeft -ge 400 ]; then sleep .40; waitLeft=$(($waitLeft-400))
- elif [ $waitLeft -ge 200 ]; then sleep .20; waitLeft=$(($waitLeft-200))
- elif [ $waitLeft -ge 100 ]; then sleep .10; waitLeft=$(($waitLeft-100))
- elif [ $waitLeft -ge 40 ]; then sleep .040; waitLeft=$(($waitLeft-40))
- elif [ $waitLeft -ge 20 ]; then sleep .020; waitLeft=$(($waitLeft-20))
- elif [ $waitLeft -ge 10 ]; then sleep .010; waitLeft=$(($waitLeft-10))
- elif [ $waitLeft -ge 5 ]; then sleep .005; waitLeft=$(($waitLeft-5))
- fi
- waitLeft=$(($waitLeft-$waitAdjust))
- done
- }
- letsRock
Advertisement
Add Comment
Please, Sign In to add comment