PureBasic - форум

Информация о пользователе

Привет, Гость! Войдите или зарегистрируйтесь.


Вы здесь » PureBasic - форум » PureBasic для ARM » Raspberry Pi » Модуль GPIO для Raspberry PI


Модуль GPIO для Raspberry PI

Сообщений 1 страница 3 из 3

1

Модуль для работы с портами ввода / вывода компьютера Raspberry PI, который известен как малина.

Код:
; Библиотека для PureBasic предоставляющая доступ к портам ввода вывода GPIO компьютера Raspberry PI.
; Автор - Пётр.
; Версия 1.0.
; [url=https://purebasic.mybb.ru/viewtopic.php?id=885]Модуль GPIO для Raspberry PI[/url]

DeclareModule GPIO
  Enumeration
    #DirIN
    #DirOUT
  EndEnumeration
  
  Declare Export(Pin)
  Declare Unexport(Pin)
  Declare Direction(Pin, Dir)
  Declare ReadPin(Pin)
  Declare WritePin(Pin, Value)
EndDeclareModule

Module GPIO
  EnableExplicit
  
  Procedure Export(Pin)
    Protected Res = #False, id
    
    id = OpenFile(#PB_Any, "/sys/class/gpio/export")
    If id
      Res = Bool(WriteString(id, Str(Pin), #PB_Ascii))
      CloseFile(id)
    EndIf
    
    ProcedureReturn Res
  EndProcedure
  
  Procedure Unexport(Pin)
    Protected Res = #False, id
    
    id = OpenFile(#PB_Any, "/sys/class/gpio/unexport")
    If id
      Res = Bool(WriteString(id, Str(Pin), #PB_Ascii))
      CloseFile(id)
    EndIf
    
    ProcedureReturn Res
  EndProcedure
  
  Procedure Direction(Pin, Dir)
    Protected Res = #False, id
    
    id = OpenFile(#PB_Any, "/sys/class/gpio/gpio"+Pin+"/direction")
    If id
      If Dir = #DirIN
        Res = Bool(WriteString(id, "in", #PB_Ascii))
      ElseIf Dir = #DirOUT
        Res = Bool(WriteString(id, "out", #PB_Ascii))
      EndIf
      CloseFile(id)
    EndIf
    
    ProcedureReturn Res
  EndProcedure
  
  Procedure ReadPin(Pin)
    Protected Res = -1, id, x.l=0
    
    id = ReadFile(#PB_Any, "/sys/class/gpio/gpio"+Pin+"/value")
    If id
      If ReadData(id, @x, 3)>0
        Res = Val(PeekS(@x, -1, #PB_Ascii))
      EndIf
      CloseFile(id)
    EndIf
    
    ProcedureReturn Res
  EndProcedure
  
  Procedure WritePin(Pin, Value)
    Protected Res = #False, id
    
    id = OpenFile(#PB_Any, "/sys/class/gpio/gpio"+Pin+"/value")
    If id
      Res = Bool(WriteString(id, Str(Bool(Value)), #PB_Ascii))
      CloseFile(id)
    EndIf
    
    ProcedureReturn Res
  EndProcedure
EndModule

Пример использования.

Код:
XIncludeFile "GPIO.pbi"
UseModule GPIO

#PinIn  = 17 ; GPIO17
#PinOut = 4  ; GPIO4

Debug "Export PinIn " + Export(#PinIn)
Debug "Export PinOut "+ Export(#PinOut)

Delay(800) ; Без небольшой паузы, функция 'Direction' возвращает 0.

Debug "Direction PinIn "  + Direction(#PinIn, #DirIN)
Debug "Direction PinOut " + Direction(#PinOut, #DirOUT)

For i=1 To 20
  Debug "Write status " + WritePin(#PinOut, i&1)
  Debug "Read Value "   + ReadPin(#PinIn)
  Delay(500)
Next

Debug "Unexport PinIn " + Unexport(#PinIn)
Debug "Unexport PinOut "+ Unexport(#PinOut)

Схема подключения светодиода и кнопки к малине.

Свернутый текст

https://forumupload.ru/uploads/0009/ae/28/2/774365.jpg

Файлы http://pure-basic.narod.ru/forum_files/ … O_v1.0.zip

0

2

Пётр написал(а):

Схема подключения светодиода и кнопки к малине.

Картинки нет :(

0

3

Ссылка на картинку http://c.radikal.ru/c42/2112/20/40071a90fb4b.jpg

Картинка

https://forumupload.ru/uploads/0009/ae/28/2/774365.jpg

0


Вы здесь » PureBasic - форум » PureBasic для ARM » Raspberry Pi » Модуль GPIO для Raspberry PI