pastebin - collaborative debugging tool
rovema.kpaste.net RSS


Simatic LED demo #001
Posted by Anonymous on Mon 9th Aug 2021 12:37
raw | new post

  1. #!/usr/bin/ksh93
  2.  
  3. #
  4. # Simatic LED demo #001
  5. #
  6. # Binary counter from hell - counts fron 0 to pow(2,number_of_leds),
  7. # and turns the LEDs on/off based on the binary value
  8. #
  9.  
  10.  
  11. compound c # ksh93 compound variable (sort of |struct|)
  12. compound -a c.ar
  13. integer c.num_leds
  14. typeset f # filename
  15. integer i
  16. typeset -i2 bin_counter # binary counter 0000...0001...0010...
  17.  
  18.  
  19. #
  20. # Enumerate all available LEDs
  21. # (the simatic-ipc-leds.ko driver will register it's LEDs
  22. # via the Linux LED driver, see
  23. # https://www.kernel.org/doc/html/latest/leds/leds-class.html how to
  24. # use it)
  25. #
  26. (( i=0 ))
  27. for f in $(find '/sys/devices/platform/simatic_ipc_leds/leds/' -name 'brightness') ; do
  28.         c.ar[i].filename="$f"
  29.         (( c.ar[i].index=i ))
  30.         # open file FD for later usage, this is more efficient than
  31.         # doing an |open(),write(),close()|-cycle for each time
  32.         # we flash an LED
  33.         redirect {c.ar[i].fd}<> "$f"
  34.  
  35.         (( i++ ))
  36. done
  37.  
  38. # count LEDs
  39. (( c.num_leds=${#c.ar[@]} ))
  40.  
  41.  
  42. #
  43. # print configuration
  44. #
  45. print -v c
  46.  
  47. # Bail out if there is no hardware
  48. if (( c.num_leds == 0 )) ; then
  49.         print -u2 -f $"%s: No leds available.\n" "$0"
  50.         exit 1
  51. fi
  52.  
  53.  
  54. #
  55. # main
  56. #
  57. typeset -R${c.num_leds} s # string with field width c.num_leds
  58.  
  59.  
  60. while true ; do
  61.         for (( bin_counter=0 ; bin_counter < pow(2,c.num_leds) ; bin_counter++ )) ; do
  62.                 # strip the '2#' prefix
  63.                 s="${bin_counter:2:c.num_leds}"
  64.                
  65.                 printf $"Value: |%s|\n" "$s"
  66.  
  67.                 for i in "${!c.ar[@]}" ; do
  68.                         print -u ${c.ar[i].fd} $(( ${s:$((c.ar[i].index)):1} ))
  69.                 done
  70.                 sleep 0.2
  71.         done
  72. done
  73. # EOF.

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at