]> Repositories - Adafruit_Blinka-hackapet.git/blobdiff - src/adafruit_blinka/microcontroller/nova/pin.py
Merge pull request #216 from binhollc/master
[Adafruit_Blinka-hackapet.git] / src / adafruit_blinka / microcontroller / nova / pin.py
index 30dbc6688905cec2019a106b371964d6f3f95811..235e4ec749bd84cbf11b777864f37de15795510a 100644 (file)
@@ -1,80 +1,72 @@
 class Pin:
 class Pin:
-    """A basic Pin class for use with FT232H."""
+    """A basic Pin class for use with Binho Nova."""
 
 
-    IN = 0
-    OUT = 1
+    IN = 'DIN'
+    OUT = 'DOUT'
+    AIN = 'AIN'
+    AOUT = 'AOUT'
+    PWM = 'PWM'
     LOW = 0
     HIGH = 1
 
     LOW = 0
     HIGH = 1
 
-    ft232h_gpio = None
+    _nova = None
 
     def __init__(self, pin_id=None):
 
     def __init__(self, pin_id=None):
-        # setup GPIO controller if not done yet
-        # use one provided by I2C as default
-        if not Pin.ft232h_gpio:
-            from pyftdi.i2c import I2cController
-            i2c = I2cController()
-            i2c.configure("ftdi:///1")
-            Pin.ft232h_gpio = i2c.get_gpio()
+        if not Pin._nova:
+            from adafruit_blinka.microcontroller.nova import Connection
+            Pin._nova = Connection.getInstance()
         # check if pin is valid
         # check if pin is valid
-        if pin_id:
-            if Pin.ft232h_gpio.all_pins & 1 << pin_id == 0:
-                raise ValueError("Can not use pin {} as GPIO.".format(pin_id))
-        # ID is just bit position
+        if pin_id > 4:
+            raise ValueError("Invalid pin {}.".format(pin_id))
+
         self.id = pin_id
 
     def init(self, mode=IN, pull=None):
         self.id = pin_id
 
     def init(self, mode=IN, pull=None):
-        if not self.id:
+        if self.id is None:
             raise RuntimeError("Can not init a None type pin.")
             raise RuntimeError("Can not init a None type pin.")
-        # FT232H does't have configurable internal pulls?
+        # Nova does't have configurable internal pulls for 
         if pull:
             raise ValueError("Internal pull up/down not currently supported.")
         if pull:
             raise ValueError("Internal pull up/down not currently supported.")
-        pin_mask = Pin.ft232h_gpio.pins | 1 << self.id
-        current = Pin.ft232h_gpio.direction
-        if mode == self.OUT:
-            current |= 1 << self.id
-        else:
-            current &= ~(1 << self.id)
-        Pin.ft232h_gpio.set_direction(pin_mask, current)
+        Pin._nova.setIOpinMode(self.id, mode)
 
     def value(self, val=None):
 
     def value(self, val=None):
-        if not self.id:
+        if self.id is None:
             raise RuntimeError("Can not access a None type pin.")
             raise RuntimeError("Can not access a None type pin.")
-        current = Pin.ft232h_gpio.read(with_output=True)
         # read
         if val is None:
         # read
         if val is None:
-            return 1 if current & 1 << self.id != 0 else 0
+            return int(Pin._nova.getIOpinValue(self.id).split('VALUE ')[1])
         # write
         # write
-        elif val in (self.LOW, self.HIGH):
-            if val == self.HIGH:
-                current |= 1 << self.id
-            else:
-                current &= ~(1 << self.id)
-            # must mask out any input pins
-            Pin.ft232h_gpio.write(current & Pin.ft232h_gpio.direction)
-        # release the kraken
+        if val in (self.LOW, self.HIGH):
+            Pin._nova.setIOpinValue(self.id, val)
         else:
             raise RuntimeError("Invalid value for pin")
 
 # create pin instances for each pin
         else:
             raise RuntimeError("Invalid value for pin")
 
 # create pin instances for each pin
-# D0 to D3 are used by I2C/SPI
-D4 = Pin(4)
-D5 = Pin(5)
-D6 = Pin(6)
-D7 = Pin(7)
-C0 = Pin(8)
-C1 = Pin(9)
-C2 = Pin(10)
-C3 = Pin(11)
-C4 = Pin(12)
-C5 = Pin(13)
-C6 = Pin(14)
-C7 = Pin(15)
-# C8 and C9 are not GPIO
+IO0 = Pin(0)
+IO1 = Pin(1)
+IO2 = Pin(2)
+IO3 = Pin(3)
+IO4 = Pin(4)
+
+SCL = IO2
+SDA = IO0
+SCK = SCLK = IO3
+MOSI = IO4
+MISO = IO2
+SS0 = IO0
+SS1 = IO1
+
+PWM0 = IO0
+# No PWM support on IO1
+PWM2 = IO2
+PWM3 = IO3
+PWM4 = IO4
+
+# orderd as (channel, pin), id
+pwmOuts = (((1, 0), PWM0), ((1, 2), PWM2), ((1, 3), PWM3), ((1, 4), PWM4))
+
+UART1_TX = IO4
+UART1_RX = IO3
 
 
-# create None type pins for I2C and SPI since they are expected to be defined
-SCL = Pin()
-SDA = Pin()
-SCK = SCLK = Pin()
-MOSI = Pin()
-MISO = Pin()
+# ordered as uartId, txId, rxId
+uartPorts = ((0, UART1_TX, UART1_RX), )