OK, I managed to (b)ash together two skeletons for doing the flow measurement/totalizing
the first is a bit of a CPU-pig since it runs flat out looking for transitions
The second run uses the IOJuggler edge-trigger capability to measure/average pulse-widths/period and total
The only way I know to maintain "global" variables is just using simple /tmp files
perhaps this can be optimized by some clever techniques
The challenge now is to figure out how to put this data into the /tmp/regfile ...
The default methodology seems horrendously hyper-ugly
Using some random non-SI illogical ugly ASCII-ified date-stamp at its head ???!!!
flowpulse:
#!/bin/sh
#flowpulse returns a simple civilized [0,1] for digital input state - instead of that giant ubus mess
ubus call ioman.gpio.din2 status | grep value | sed s/[^01]//g
flow:
#!/bin/sh
#flow calculates averaged low/high pulsewidths and period and totalized flow from digital input
avg_on=100
avg_off=100
dur=0
total=0
tick=`./flowpulse`
end=`date +"%s"`
echo dur $dur avg_off $avg_off avg_on $avg_on total $total
ticklast=`./flowpulse`
start=`date +"%s"`
while [ 0 -lt 1 ]
do
#echo "tick"
#read tick
tick=`./flowpulse`
#end=`date +"%s"`
#echo $tick $ticklast
if [ "$tick" != "$ticklast" ]
then
if [ "$tick" == 1 ]
then
total=`echo "$(( $total + 1))"`
end=`date +"%s"`
dur=`echo "$(( ( $end - $start) * 10 ))"`
avg_off=`echo "$(( ( $avg_off * 9 + $dur ) / 10 ))"`
fi
if [ "$tick" == 0 ]
then
#total=`echo "$(( $total + 1))"`
end=`date +"%s"`
dur=`echo "$(( ( $end - $start) * 10 ))"`
avg_on=`echo "$(( ( $avg_on * 9 + $dur ) / 10 ))"`
fi
avg_period=`echo "$(( $avg_off + $avg_on ))"`
echo -e dur $dur avg_off $avg_off avg on $avg_on avg_period $avg_period total $total
fi
start=`echo $end`
ticklast=`echo $tick`
done
The above code works, but is pretty CPU intensive since Rust Never Sleeps while polling for DI transitions
I discovered that the IOJuggler can generate triggers on din2 input rise/fall transitions
So I surgically split and reworked the above code to get rid of the transition detection and use
/tmp/flow* files to store relevant calculated data...
I still want to explore more efficient mechanisms - perhaps concentrate stuff into fewer files - or maybe it's not worth the effort ?
flowrise:
#!/bin/sh
#flowrise flow pulse-off width averager and period and totalizer using /tmp/flow*
flowavg_on=`cat /tmp/flowavg_on`
flowavg_off=`cat /tmp/flowavg_off`
flowtotal=`cat /tmp/flowtotal`
start=`cat /tmp/flow_fall`
end=`date +"%s"`
echo $end > /tmp/flow_rise
flowtotal=`echo "$(( $flowtotal + 1))"`
dur=`echo "$(( ( $end - $start) * 10 ))"`
flowavg_off=`echo "$(( ( $flowavg_off * 9 + $dur ) / 10 ))"`
avg_period=`echo "$(( $flowavg_off + $flowavg_on ))"`
echo $avg_period>/tmp/flow_period
echo -e dur $dur avg_off $flowavg_off avg on $flowavg_on avg_period $avg_period total $flowtotal
flowfall:
#!/bin/sh
#flowrise flow pulse-on width averager and period using /tmp/flow*
flowavg_on=`cat /tmp/flowavg_on`
flowavg_off=`cat /tmp/flowavg_off`
flowtotal=`cat /tmp/flowtotal`
start=`cat /tmp/flow_rise`
end=`date +"%s"`
echo $end > /tmp/flow_fall
flowtotal=`echo "$(( $flowtotal + 1))"`
dur=`echo "$(( ( $end - $start) * 10 ))"`
flowavg_on=`echo "$(( ( $flowavg_on * 9 + $dur ) / 10 ))"`
avg_period=`echo "$(( $flowavg_off + $flowavg_on ))"`
echo $avg_period>/tmp/flow_period
echo -e dur $dur avg_off $flowavg_off avg on $flowavg_on avg_period $avg_period total $flowtotal