giovedì 15 febbraio 2018

Libreria per GPIO raspberryPi

Abbiamo già visto precedentemente come lavorare con i GPIO della raspberry, ma ecco il codice di una libreria che rende tutto più facile:


on Pi_gpio_init nbr dir
   /* Pi_gpio_init RaspberryPi
   Syntax:
   Pi_gpio_init nbr, dir   # Command #
   Examples:
   Pi_gpio_init 2, "in"
   Pi_gpio_init 2, "out"
   Description:
   Initialize the gpio and set the direction of the operation
   nbr is one of the gpio pins: "2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,32"
   dir is "in" or "out"
   
   Source:
   Michael Doub
   Pi_gpio_init */
   /* Include
   RaspberryPi_var
   Pi_gpio_output
   */
   local cmd, direction_path
   if nbr is not among the items of good_pins then return "error: invalid pin nbr"
   if lower(dir) is not among the items of good_directions then return "error: invalid direction"
   if the platform is "Linux" then
      put ("/sys/class/gpio/gpio" & nbr & "/direction") into direction_path
      if there is not a file direction_path then
         put "echo" && nbr && "> /sys/class/gpio/export" into cmd
         get shell (cmd)
         if it is not empty then return "error:" & it
      end if
      if there is not a file direction_path then return "error:" && direction_path && "does not exist"
      open file direction_path for write
      write dir to file direction_path
      close file direction_path
   end if
   put dir into gpio[nbr][direction]
   return empty
end Pi_gpio_init

on Pi_gpio_input nbr
   /* Pi_gpio_input RaspberryPi
   Syntax:
   Pi_gpio_input nbr   # Command #
   Examples:
   Pi_gpio_input 2
   
   Description:
   Input from the gpio nbr
   nbr is one of the gpio pins: "2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,32"
   
   Source:
   Michael Doub
   Pi_gpio_input */
   /* Include
   RaspberryPi_var
   Pi_gpio_init
   */
   
   local value_path
   if nbr is not among the items of good_pins then return "error: invalid pin nbr"
   if the platform is "Linux" then
      put ("/sys/class/gpio/gpio" & nbr & "/value") into value_path
      if there is not a file value_path then return "error:" && value_path && "does not exist"
       open file value_path
       read from file value_path until EOF
       close file value_path
   end if
   return it
end Pi_gpio_input


on Pi_gpio_output nbr val
   /* Pi_gpio_output RaspberryPi
   Syntax:
   Pi_gpio_output nbr, val   # Command #
   Examples:
   Pi_gpio_output 2, 1
   Pi_gpio_output 2, 0
   Description:
   Output to the gpio nbr, with value of val
   nbr is one of the gpio pins: "2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,32"
   val is 1 or 0
   
   Source:
   Michael Doub
   Pi_gpio_output */
   /* Include
   RaspberryPi_var
   Pi_gpio_init
   */
   
   local value_path
   if nbr is not among the items of good_pins then return "error: invalid pin nbr"
   if val is not among the items good_values then return "error: invalid value"
   if the platform is "Linux" then
      put ("/sys/class/gpio/gpio" & nbr & "/value") into value_path
      if there is not a file value_path then return "error:" && value_path && "does not exist"
      open file value_path for write
      write val to file value_path
      close file value_path
   end if
   put val into gpio[nbr]["value"]
   return empty
end Pi_gpio_output

on Pi_to_Gert_Close
   /* Pi_to_Gert_Close RaspberryPi
   Syntax:
   Pi_to_Gert_Close    # Command #
   Examples:
   Pi_to_Gert_Close
   Description:
   This reoutine actually closes the serial port to the GertDuino.
   Source:
   Michael Doub
   Pi_to_Gert_Close */
   /* Include
   Pi_to_Gert_Variables
   */
   put false into _pi_okToRead
   wait for _pi_readrate with messages
   close file _pi_thePort
end Pi_to_Gert_Close


on Pi_to_Gert_Open pPortName, pReadrate, pBaud, pParity, pDatabits, pStopbits
   /* Pi_to_Gert_Open RaspberryPi
   Syntax:
   Pi_to_Gert_Open pPortName, [pReadrate], [pBaud], [pParity], [pDatabits], [pStopbits]    # Command #
   Examples:
   Pi_to_Gert_Open
   Description:
   Set the serial port configuration paramenters and opens the serial port to the GertDuino.
   
   Defaults:
   .    pPortName: "/dev/ttyAMA0"
   .    readrate: 10
   .    pBaud: 9600
   .    pParity: N
   .    pDatabits: 8
   .    pStopbits: 1
   
   Source:
   Michael Doub
   Pi_to_Gert_Open */
   /* Include
   Pi_to_Gert_Variables
   */
   
   if pPortName is empty then
      put "/dev/ttyAMA0" into _pi_theport
   else
      put pPortName into _pi_thePort
   end if
   if pReadrate is empty then put 10 into _pi_readrate
   if pBaud is empty then put 9600 into pBaud
   if pParity is empty then put "N" into pParity
   if pDatabits is empty then put 8 into pDatabits
   if pStopbits is empty then put 1 into pStopbits
   Put "BAUD=" & pBaud &&\
   "PARITY="& pParity &&\
   "DATA=" & pDatabits &&\
   "STOP=" & pDatabits into serial
   if the platform = "Linux" then
      set the serialControlString to serial
      open file _pi_thePort for binary update
   end if
end Pi_to_Gert_Open

on Pi_to_Gert_Start_Reading pCallbackname, pObj
   /* Pi_to_Gert_Start_Reading RaspberryPi
   Syntax:
   Pi_to_Gert_Start_Reading pCallbackname, pObj    # Command #
   Examples:
   Pi_to_Gert_Start_Reading "WeGotData", me
   Description:
   This routine starts the reading process between the Gertduino and
   the raspBerryPi. Since you must read the serial port until empty, this
   means that you must constantly have a read active on the port.   
      
      When data is actually recieved it is passed
      to pCallbackname of pObj thru the message path with a single parameter
      of the data base64encoded.
      
      Source:
      Michael Doub
      Pi_to_Gert_Start_Reading */
      /* Include
      Pi_to_Gert_Variables
      readPort_Gert
      */
      put true into _pi_okToRead
      put pCallbackname into _pi_callbackname
      put pObj into _pi_owner
      readPort_Gert
end Pi_to_Gert_Start_Reading

on Pi_to_Gert_Stop_Reading
   /* Pi_to_Gert_Stop_Reading RaspberryPi
   Syntax:
   Pi_to_Gert_Stop_Readingj    # Command #
   Examples:
   Pi_to_Gert_Stop_Reading
   Description:
   This routine stops the continious read process that is expecting data
   from the Gertdino serial port.
   Source:
   Michael Doub
   Pi_to_Gert_Stop_Reading */
   /* Include
   Pi_to_Gert_Variables
   */
   put false into _pi_okToRead
end Pi_to_Gert_Stop_Reading

on Pi_to_Gert_Write theData pDecode
   /* Pi_to_Gert_Write RaspberryPi
   Syntax:
   Pi_to_Gert_Write theData, [pDecode]    # Command #
   Examples:
   Pi_to_Gert_Write thedata
   Pi_to_Gert_Write thedata, true
   Description:
   Writes theData to the Gertduino serial port.   If pDecode is true then the data
   will be base64decoded
   Source:
   Michael Doub
   Pi_to_Gert_Write */
   /* Include
   Pi_to_Gert_Variables
   */
   if pDecode then
      write base64decode(theData) to file _pi_thePort
   else
      write theData to file _pi_thePort
   end if
end Pi_to_Gert_Write