]> Repositories - Adafruit_Blinka-hackapet.git/commitdiff
digitalio works
authorladyada <limor@ladyada.net>
Mon, 18 Jun 2018 00:29:08 +0000 (20:29 -0400)
committerScott Shawcroft <scott@tannewt.org>
Wed, 27 Jun 2018 19:57:29 +0000 (12:57 -0700)
examples/piblinka.py
src/adafruit_blinka/microcontroller/raspi_23/pin.py
src/digitalio.py

index e385a6367b55b12a875a067eaa68b8ddb182f970..0b13109fd4334853ea02960e5ee34e873fcccad8 100644 (file)
@@ -1,15 +1,30 @@
 import sys
+import time
 sys.path.append('/home/pi/Adafruit_Micropython_Blinka/src')
 sys.path.append('/home/pi/Adafruit_Python_GPIO')
 
 #from Adafruit_GPIO import Platform
 #print("Platform = ", Platform.platform_detect(), Platform.pi_version())
 
-from adafruit_blinka.agnostic import board as agnostic_board
 print("hello blinka!")
+
+from adafruit_blinka.agnostic import board as agnostic_board
 print("Found system type: %s (sys.plaform %s implementation %s) " % (agnostic_board, sys.platform, sys.implementation.name))
 
 import board
 print("board contents: ", dir(board))
 
-#print(adafruit_blinka.adafruit_blinka.agnostic.microcontroller)
+import digitalio
+
+led = digitalio.DigitalInOut(board.D4)
+led.direction = digitalio.Direction.OUTPUT
+
+button = digitalio.DigitalInOut(board.D18)
+button.direction = digitalio.Direction.INPUT
+button.pull = digitalio.Pull.DOWN
+
+while True:
+    led.value = button.value
+    time.sleep(0.1)
+    #led.value = False
+    #time.sleep(0.5)
index 873e7cf9b483b6f3125b43ec3a28e5e9f592d632..8fecca98df99c2dea08097a16fd14b99c37ffaf2 100644 (file)
@@ -1,36 +1,86 @@
+import RPi.GPIO as GPIO
+GPIO.setmode(GPIO.BCM)
+
 # Pins dont exist in CPython so...lets make our own!
 class Pin:
-    def __init__(self, name, bcm_number):
-        self._name = name
-        self._number = bcm_number
-
-SDA = Pin("SDA/D2", 2)
-SCL = Pin("SCL/D3", 3)
-D2 = Pin("SDA/D2", 2)
-D3 = Pin("SCL/D3", 3)
-D4 = Pin("BCM 4", 4)
-D9 = Pin("MISO/D9", 9)
-D10 = Pin("MOSI/D10", 10)
-D11 = Pin("SCLK/D11", 11)
-MISO = Pin("MISO/D9", 9)
-MOSI = Pin("MOSI/D10", 10)
-SCLK = Pin("SCLK/D11", 11)
-D14 = Pin("TXD/D14", 14)
-D15 = Pin("RXD/D15", 15)
-TXD = Pin("TXD/D14", 14)
-RXD = Pin("RXD/D15", 15)
-D17 = Pin("BCM 17", 17)
-D18 = Pin("BCM 18", 18)
-D19 = Pin("BCM 19", 19)
-D20 = Pin("BCM 20", 20)
-MISO_2 = Pin("MISO_2/19", 19)
-MOSI_2 = Pin("MOSI_2/20", 20)
-SCLK_2 = Pin("SCLK_2/21", 21)
-D21 = Pin("BCM 21", 21)
-D22 = Pin("BCM 22", 22)
-D23 = Pin("BCM 23", 23)
-D24 = Pin("BCM 24", 24)
-D27 = Pin("BCM 27", 27)
+    IN = 0
+    OUT = 1
+    LOW = 0
+    HIGH = 1
+    PULL_NONE = 0
+    PULL_UP = 1
+    PULL_DOWN = 2
+    
+    id = None
+    _value = LOW
+    _mode = IN
+    
+    def __init__(self, bcm_number):
+        self.id = bcm_number
+
+    def init(self, mode=IN, pull=None):
+        if mode != None:
+            print("set %d to mode %d" % (self.id, mode))
+            if mode == self.IN:
+                self._mode = self.IN
+                GPIO.setup(self.id, GPIO.IN)
+            elif mode == self.OUT:
+                self._mode = self.OUT
+                GPIO.setup(self.id, GPIO.OUT)
+            else:
+                raise RuntimeError("Invalid mode for pin: %s" % self.id)
+        if pull != None:
+            print("set %d to pull %d" % (self.id, pull))
+            if self._mode != self.IN:
+                raise RuntimeError("Cannot set pull resistor on output")
+            if pull == self.PULL_UP:
+                GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_UP)
+            elif pull == self.PULL_DOWN:
+                GPIO.setup(self.id, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
+            else:
+                raise RuntimeError("Invalid pull for pin: %s" % self.id)       
+
+    def value(self, val=None):
+        if val != None:
+            print("set %d to value %d" %(self.id, val))
+            if val == self.LOW:
+                self._value = val
+                GPIO.output(self.id, val)
+            elif val == self.HIGH:
+                self._value = val
+                GPIO.output(self.id, val)
+            else:
+                raise RuntimeError("Invalid value for pin")
+        else:
+            return GPIO.input(self.id)
+
+SDA = Pin(2)
+SCL = Pin(3)
+D2 = Pin(2)
+D3 = Pin(3)
+D4 = Pin(4)
+D9 = Pin(9)
+D10 = Pin(10)
+D11 = Pin(11)
+MISO = Pin(9)
+MOSI = Pin(10)
+SCLK = Pin(11)
+D14 = Pin(14)
+D15 = Pin(15)
+TXD = Pin(14)
+RXD = Pin(15)
+D17 = Pin(17)
+D18 = Pin(18)
+D19 = Pin(19)
+D20 = Pin(20)
+MISO_2 = Pin(19)
+MOSI_2 = Pin(20)
+SCLK_2 = Pin(21)
+D21 = Pin(21)
+D22 = Pin(22)
+D23 = Pin(23)
+D24 = Pin(24)
+D27 = Pin(27)
 
 # ordered as spiId, sckId, mosiId, misoId
 spiPorts = ((1, SCLK, MOSI, MISO), (2, SCLK_2, MOSI_2, MISO_2))
index 8fb53854a81e7d27637b3ea137606e3759dd0294..84aeebdc9bd94c0b660f0563055fdcb79e2780f2 100755 (executable)
@@ -7,8 +7,11 @@ See `CircuitPython:digitalio` in CircuitPython for more details.
 * Author(s): cefn
 """
 
-from machine import Pin
 from adafruit_blinka.agnostic import board as boardId
+if boardId == "raspi_3" or boardId == "raspi_2":
+    from adafruit_blinka.microcontroller.raspi_23.pin import Pin
+else:
+    from machine import Pin
 from adafruit_blinka import Enum, ContextManaged
 
 class DriveMode(Enum):