lower case everything.

master
Guy Carver 2018-02-18 09:11:30 -05:00
rodzic 49b4ae9d5e
commit 9a9b43cbd7
16 zmienionych plików z 339 dodań i 117 usunięć

Wyświetl plik

@ -27,7 +27,7 @@ from pyb import UART, udelay
#AT+IPD - Receive data
#AT+CSYSWDTENABLE or DISABLE - enable/disable restart on error watchdog.
class WIFI:
class wifi:
"""docstring for wifi"""
def __init__(self, uart, baudrate = 115200):

Wyświetl plik

@ -1,7 +1,9 @@
#driver for GY-521 Accelerometer
#driver for gy-521 Accelerometer
#Translated by Guy Carver from the MPU6050 sample code.
#todo: use const().
import pyb
ADDRESS_LOW = 0x68 #address pin low (GND), default for InvenSense evaluation board
@ -357,8 +359,8 @@ DMP_MEMORY_BANKS = 8
DMP_MEMORY_BANK_SIZE = 256
DMP_MEMORY_CHUNK_SIZE = 16
class Accel(object) :
"""GY-521 Accelerometer."""
class accel(object) :
"""gy-521 Accelerometer."""
@staticmethod
def color( aR, aG, aB ) :

Wyświetl plik

@ -1,7 +1,7 @@
from pyb import Pin, ADC
class IRDistance(object):
class irdistance(object):
""" Driver for Sharp Gp2y0a IR distance sensor. The distance
range is around 3 to 40 inches. """

Wyświetl plik

@ -23,7 +23,7 @@ from pyb import UART, repl_uart, udelay
# AT+NAMEnewname This is the name that will show up in windows.
# AT+PIN???? set 4 digit pairing pin.
class JYMCU(object):
class jymcu(object):
"""JY-MCU Bluetooth serial device driver. This is simply a light UART wrapper
with addition AT command methods to customize the device."""

Wyświetl plik

@ -1,9 +1,9 @@
#Driver for the L298N Dual HBridge motor controller.
#Driver for the l298n Dual HBridge motor controller.
from PWM import PWM
from pyb import Pin, delay
class Motor( ):
class motor( ):
"""Control a motor connected to the L298N Dual motor controller."""
def __init__( self, forward, backward, speed ) :

Wyświetl plik

@ -1,7 +1,4 @@
#driver for Sainsmart 1.8" TFT display ST7735
#Translated by Guy Carver from the ST7735 sample code.
#NOTE: This current code will set the pixel at 0,0 but the scrolling will not scroll it. Don't know if it's software causing it or not.
#driver for the diymall 9.6 oled display.
import pyb
@ -70,7 +67,7 @@ _VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = const(0x2A)
# 406 40E
# 407 40F
class OLED(object) :
class oled(object) :
"""diyMall OLED 9.6 128x64 pixel display driver."""
def __init__( self, aLoc ) :

Wyświetl plik

@ -2,7 +2,7 @@
import pyb
class PIR(object):
class pir(object):
"""Passive Infrared Motion Sensor driver. Supports on/off through an output pin
trigger reading as well as an interrupt callback on trigger high/low changes."""

Wyświetl plik

@ -2,7 +2,7 @@
from pyb import Timer, Pin
class PWM(object):
class pwm(object):
"""docstring for PWMM"""
#Dict pin name: timer #, channel #.
@ -44,14 +44,14 @@ class PWM(object):
if v[0] == timernum:
return v
except Exception as e :
raise PWM.PWMException("Pin {} cannot be used for PWM".format(pinname))
raise pwm.PWMException("Pin {} cannot be used for PWM".format(pinname))
raise PWM.PWMException("Pin {} cannot use timer {}".format(pinname, timernum))
raise pwm.PWMException("Pin {} cannot use timer {}".format(pinname, timernum))
def __init__( self, p, timernum, afreq = 100 ) :
isname = type(p) == str
pinname = p if isname else p.name()
timernum, channel = PWM.timerandchannel(pinname, timernum)
timernum, channel = pwm.timerandchannel(pinname, timernum)
if isname:
p = Pin(pinname)

Wyświetl plik

@ -2,7 +2,7 @@
from pyb import Pin
class Relay(object):
class relay(object):
"""Control a relay board with an output pin. Set on to True to drive the relay pin low
which turns the relay on."""

Wyświetl plik

@ -3,7 +3,7 @@ from pyb import Pin, Timer, udelay
# WARNING: Do not use PA4-X5 or PA5-X6 as the echo pin without a 1k resistor.
class SR04Distance(object):
class sr04distance(object):
""" """
maxinches = 20 #maximum range of SR04.

Wyświetl plik

@ -1,6 +1,8 @@
#driver for Sainsmart 1.8" TFT display ST7735
#driver for Sainsmart 1.8" tft display ST7735
#Translated by Guy Carver from the ST7735 sample code.
#todo: Use const()
import pyb
from math import sqrt
@ -33,8 +35,8 @@ def TFTColor( aR, aG, aB ) :
ScreenSize = (128, 160)
class TFT(object) :
"""Sainsmart TFT 7735 display driver."""
class tft(object) :
"""Sainsmart tft-7735 display driver."""
NOP = 0x0
SWRESET = 0x01
@ -121,12 +123,12 @@ class TFT(object) :
# @micropython.native
def on( self, aTF = True ) :
'''Turn display on or off.'''
self._writecommand(TFT.DISPON if aTF else TFT.DISPOFF)
self._writecommand(tft.DISPON if aTF else tft.DISPOFF)
# @micropython.native
def invertcolor( self, aBool ) :
'''Invert the color data IE: Black = White.'''
self._writecommand(TFT.INVON if aBool else TFT.INVOFF)
self._writecommand(tft.INVON if aBool else tft.INVOFF)
# @micropython.native
def rgb( self, aTF = True ) :
@ -386,35 +388,35 @@ class TFT(object) :
'''Set a single point for drawing a color to.'''
x = int(aPos[0])
y = int(aPos[1])
self._writecommand(TFT.CASET) #Column address set.
self._writecommand(tft.CASET) #Column address set.
self.windowLocData[0] = 0x00
self.windowLocData[1] = x
self.windowLocData[2] = 0x00
self.windowLocData[3] = x
self._writedata(self.windowLocData)
self._writecommand(TFT.RASET) #Row address set.
self._writecommand(tft.RASET) #Row address set.
self.windowLocData[1] = y
self.windowLocData[3] = y
self._writedata(self.windowLocData)
self._writecommand(TFT.RAMWR) #Write to RAM.
self._writecommand(tft.RAMWR) #Write to RAM.
# @micropython.native
def _setwindowloc( self, aPos0, aPos1 ) :
'''Set a rectangular area for drawing a color to.'''
self._writecommand(TFT.CASET) #Column address set.
self._writecommand(tft.CASET) #Column address set.
self.windowLocData[0] = 0x00
self.windowLocData[1] = int(aPos0[0])
self.windowLocData[2] = 0x00
self.windowLocData[3] = int(aPos1[0])
self._writedata(self.windowLocData)
self._writecommand(TFT.RASET) #Row address set.
self._writecommand(tft.RASET) #Row address set.
self.windowLocData[1] = int(aPos0[1])
self.windowLocData[3] = int(aPos1[1])
self._writedata(self.windowLocData)
self._writecommand(TFT.RAMWR) #Write to RAM.
self._writecommand(tft.RAMWR) #Write to RAM.
@micropython.native
def _writecommand( self, aCommand ) :
@ -443,7 +445,7 @@ class TFT(object) :
@micropython.native
def _setMADCTL( self ) :
'''Set screen rotation and RGB/BGR format.'''
self._writecommand(TFT.MADCTL)
self._writecommand(tft.MADCTL)
rgb = TFTRGB if self._rgb else TFTBGR
self._writedata(TFTRotations[self.rotate] | rgb)
@ -462,58 +464,58 @@ class TFT(object) :
'''Initialize blue tab version.'''
self._size = (ScreenSize[0] + 2, ScreenSize[1] + 1)
self._reset()
self._writecommand(TFT.SWRESET) #Software reset.
self._writecommand(tft.SWRESET) #Software reset.
pyb.delay(50)
self._writecommand(TFT.SLPOUT) #out of sleep mode.
self._writecommand(tft.SLPOUT) #out of sleep mode.
pyb.delay(500)
data1 = bytearray(1)
self._writecommand(TFT.COLMOD) #Set color mode.
self._writecommand(tft.COLMOD) #Set color mode.
data1[0] = 0x05 #16 bit color.
self._writedata(data1)
pyb.delay(10)
data3 = bytearray([0x00, 0x06, 0x03]) #fastest refresh, 6 lines front, 3 lines back.
self._writecommand(TFT.FRMCTR1) #Frame rate control.
self._writecommand(tft.FRMCTR1) #Frame rate control.
self._writedata(data3)
pyb.delay(10)
self._writecommand(TFT.MADCTL)
self._writecommand(tft.MADCTL)
data1[0] = 0x08 #row address/col address, bottom to top refresh
self._writedata(data1)
data2 = bytearray(2)
self._writecommand(TFT.DISSET5) #Display settings
self._writecommand(tft.DISSET5) #Display settings
data2[0] = 0x15 #1 clock cycle nonoverlap, 2 cycle gate rise, 3 cycle oscil, equalize
data2[1] = 0x02 #fix on VTL
self._writedata(data2)
self._writecommand(TFT.INVCTR) #Display inversion control
self._writecommand(tft.INVCTR) #Display inversion control
data1[0] = 0x00 #Line inversion.
self._writedata(data1)
self._writecommand(TFT.PWCTR1) #Power control
self._writecommand(tft.PWCTR1) #Power control
data2[0] = 0x02 #GVDD = 4.7V
data2[1] = 0x70 #1.0uA
self._writedata(data2)
pyb.delay(10)
self._writecommand(TFT.PWCTR2) #Power control
self._writecommand(tft.PWCTR2) #Power control
data1[0] = 0x05 #VGH = 14.7V, VGL = -7.35V
self._writedata(data1)
self._writecommand(TFT.PWCTR3) #Power control
self._writecommand(tft.PWCTR3) #Power control
data2[0] = 0x01 #Opamp current small
data2[1] = 0x02 #Boost frequency
self._writedata(data2)
self._writecommand(TFT.VMCTR1) #Power control
self._writecommand(tft.VMCTR1) #Power control
data2[0] = 0x3C #VCOMH = 4V
data2[1] = 0x38 #VCOML = -1.1V
self._writedata(data2)
pyb.delay(10)
self._writecommand(TFT.PWCTR6) #Power control
self._writecommand(tft.PWCTR6) #Power control
data2[0] = 0x11
data2[1] = 0x15
self._writedata(data2)
@ -523,36 +525,36 @@ class TFT(object) :
# 0x1b, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10])
dataGMCTRP = bytearray([0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29,
0x25, 0x2b, 0x39, 0x00, 0x01, 0x03, 0x10])
self._writecommand(TFT.GMCTRP1)
self._writecommand(tft.GMCTRP1)
self._writedata(dataGMCTRP)
# dataGMCTRN = bytearray([0x0f, 0x1b, 0x0f, 0x17, 0x33, 0x2c, 0x29, 0x2e, 0x30,
# 0x30, 0x39, 0x3f, 0x00, 0x07, 0x03, 0x10])
dataGMCTRN = bytearray([0x03, 0x1d, 0x07, 0x06, 0x2e, 0x2c, 0x29, 0x2d, 0x2e,
0x2e, 0x37, 0x3f, 0x00, 0x00, 0x02, 0x10])
self._writecommand(TFT.GMCTRN1)
self._writecommand(tft.GMCTRN1)
self._writedata(dataGMCTRN)
pyb.delay(10)
self._writecommand(TFT.CASET) #Column address set.
self._writecommand(tft.CASET) #Column address set.
self.windowLocData[0] = 0x00
self.windowLocData[1] = 2 #Start at column 2
self.windowLocData[2] = 0x00
self.windowLocData[3] = self._size[0] - 1
self._writedata(self.windowLocData)
self._writecommand(TFT.RASET) #Row address set.
self._writecommand(tft.RASET) #Row address set.
self.windowLocData[1] = 1 #Start at row 2.
self.windowLocData[3] = self._size[1] - 1
self._writedata(self.windowLocData)
self._writecommand(TFT.NORON) #Normal display on.
self._writecommand(tft.NORON) #Normal display on.
pyb.delay(10)
self._writecommand(TFT.RAMWR)
self._writecommand(tft.RAMWR)
pyb.delay(500)
self._writecommand(TFT.DISPON)
self._writecommand(tft.DISPON)
self.cs.high()
pyb.delay(500)
@ -560,94 +562,94 @@ class TFT(object) :
'''Initialize a red tab version.'''
self._reset()
self._writecommand(TFT.SWRESET) #Software reset.
self._writecommand(tft.SWRESET) #Software reset.
pyb.delay(150)
self._writecommand(TFT.SLPOUT) #out of sleep mode.
self._writecommand(tft.SLPOUT) #out of sleep mode.
pyb.delay(500)
data3 = bytearray([0x01, 0x2C, 0x2D]) #fastest refresh, 6 lines front, 3 lines back.
self._writecommand(TFT.FRMCTR1) #Frame rate control.
self._writecommand(tft.FRMCTR1) #Frame rate control.
self._writedata(data3)
self._writecommand(TFT.FRMCTR2) #Frame rate control.
self._writecommand(tft.FRMCTR2) #Frame rate control.
self._writedata(data3)
data6 = bytearray([0x01, 0x2c, 0x2d, 0x01, 0x2c, 0x2d])
self._writecommand(TFT.FRMCTR3) #Frame rate control.
self._writecommand(tft.FRMCTR3) #Frame rate control.
self._writedata(data6)
pyb.delay(10)
data1 = bytearray(1)
self._writecommand(TFT.INVCTR) #Display inversion control
self._writecommand(tft.INVCTR) #Display inversion control
data1[0] = 0x07 #Line inversion.
self._writedata(data1)
self._writecommand(TFT.PWCTR1) #Power control
self._writecommand(tft.PWCTR1) #Power control
data3[0] = 0xA2
data3[1] = 0x02
data3[2] = 0x84
self._writedata(data3)
self._writecommand(TFT.PWCTR2) #Power control
self._writecommand(tft.PWCTR2) #Power control
data1[0] = 0xC5 #VGH = 14.7V, VGL = -7.35V
self._writedata(data1)
data2 = bytearray(2)
self._writecommand(TFT.PWCTR3) #Power control
self._writecommand(tft.PWCTR3) #Power control
data2[0] = 0x0A #Opamp current small
data2[1] = 0x00 #Boost frequency
self._writedata(data2)
self._writecommand(TFT.PWCTR4) #Power control
self._writecommand(tft.PWCTR4) #Power control
data2[0] = 0x8A #Opamp current small
data2[1] = 0x2A #Boost frequency
self._writedata(data2)
self._writecommand(TFT.PWCTR5) #Power control
self._writecommand(tft.PWCTR5) #Power control
data2[0] = 0x8A #Opamp current small
data2[1] = 0xEE #Boost frequency
self._writedata(data2)
self._writecommand(TFT.VMCTR1) #Power control
self._writecommand(tft.VMCTR1) #Power control
data1[0] = 0x0E
self._writedata(data1)
self._writecommand(TFT.INVOFF)
self._writecommand(tft.INVOFF)
self._writecommand(TFT.MADCTL) #Power control
self._writecommand(tft.MADCTL) #Power control
data1[0] = 0xC8
self._writedata(data1)
self._writecommand(TFT.COLMOD)
self._writecommand(tft.COLMOD)
data1[0] = 0x05
self._writedata(data1)
self._writecommand(TFT.CASET) #Column address set.
self._writecommand(tft.CASET) #Column address set.
self.windowLocData[0] = 0x00
self.windowLocData[1] = 0x00
self.windowLocData[2] = 0x00
self.windowLocData[3] = self._size[0] - 1
self._writedata(self.windowLocData)
self._writecommand(TFT.RASET) #Row address set.
self._writecommand(tft.RASET) #Row address set.
self.windowLocData[3] = self._size[1] - 1
self._writedata(self.windowLocData)
dataGMCTRP = bytearray([0x0f, 0x1a, 0x0f, 0x18, 0x2f, 0x28, 0x20, 0x22, 0x1f,
0x1b, 0x23, 0x37, 0x00, 0x07, 0x02, 0x10])
self._writecommand(TFT.GMCTRP1)
self._writecommand(tft.GMCTRP1)
self._writedata(dataGMCTRP)
dataGMCTRN = bytearray([0x0f, 0x1b, 0x0f, 0x17, 0x33, 0x2c, 0x29, 0x2e, 0x30,
0x30, 0x39, 0x3f, 0x00, 0x07, 0x03, 0x10])
self._writecommand(TFT.GMCTRN1)
self._writecommand(tft.GMCTRN1)
self._writedata(dataGMCTRN)
pyb.delay(10)
self._writecommand(TFT.DISPON)
self._writecommand(tft.DISPON)
pyb.delay(100)
self._writecommand(TFT.NORON) #Normal display on.
self._writecommand(tft.NORON) #Normal display on.
pyb.delay(10)
self.cs.high()
@ -657,106 +659,106 @@ class TFT(object) :
'''Initialize a green tab version.'''
self._reset()
self._writecommand(TFT.SWRESET) #Software reset.
self._writecommand(tft.SWRESET) #Software reset.
pyb.delay(150)
self._writecommand(TFT.SLPOUT) #out of sleep mode.
self._writecommand(tft.SLPOUT) #out of sleep mode.
pyb.delay(255)
data3 = bytearray([0x01, 0x2C, 0x2D]) #fastest refresh, 6 lines front, 3 lines back.
self._writecommand(TFT.FRMCTR1) #Frame rate control.
self._writecommand(tft.FRMCTR1) #Frame rate control.
self._writedata(data3)
self._writecommand(TFT.FRMCTR2) #Frame rate control.
self._writecommand(tft.FRMCTR2) #Frame rate control.
self._writedata(data3)
data6 = bytearray([0x01, 0x2c, 0x2d, 0x01, 0x2c, 0x2d])
self._writecommand(TFT.FRMCTR3) #Frame rate control.
self._writecommand(tft.FRMCTR3) #Frame rate control.
self._writedata(data6)
pyb.delay(10)
self._writecommand(TFT.INVCTR) #Display inversion control
self._writecommand(tft.INVCTR) #Display inversion control
self._writedata(0x07)
self._writecommand(TFT.PWCTR1) #Power control
self._writecommand(tft.PWCTR1) #Power control
data3[0] = 0xA2
data3[1] = 0x02
data3[2] = 0x84
self._writedata(data3)
self._writecommand(TFT.PWCTR2) #Power control
self._writecommand(tft.PWCTR2) #Power control
self._writedata(0xC5)
data2 = bytearray(2)
self._writecommand(TFT.PWCTR3) #Power control
self._writecommand(tft.PWCTR3) #Power control
data2[0] = 0x0A #Opamp current small
data2[1] = 0x00 #Boost frequency
self._writedata(data2)
self._writecommand(TFT.PWCTR4) #Power control
self._writecommand(tft.PWCTR4) #Power control
data2[0] = 0x8A #Opamp current small
data2[1] = 0x2A #Boost frequency
self._writedata(data2)
self._writecommand(TFT.PWCTR5) #Power control
self._writecommand(tft.PWCTR5) #Power control
data2[0] = 0x8A #Opamp current small
data2[1] = 0xEE #Boost frequency
self._writedata(data2)
self._writecommand(TFT.VMCTR1) #Power control
self._writecommand(tft.VMCTR1) #Power control
self._writedata(0x0E)
self._writecommand(TFT.INVOFF)
self._writecommand(tft.INVOFF)
self._setMADCTL()
self._writecommand(TFT.COLMOD)
self._writecommand(tft.COLMOD)
self._writedata(0x05)
self._writecommand(TFT.CASET) #Column address set.
self._writecommand(tft.CASET) #Column address set.
self.windowLocData[0] = 0x00
self.windowLocData[1] = 0x01 #Start at row/column 1.
self.windowLocData[2] = 0x00
self.windowLocData[3] = self._size[0] - 1
self._writedata(self.windowLocData)
self._writecommand(TFT.RASET) #Row address set.
self._writecommand(tft.RASET) #Row address set.
self.windowLocData[3] = self._size[1] - 1
self._writedata(self.windowLocData)
dataGMCTRP = bytearray([0x02, 0x1c, 0x07, 0x12, 0x37, 0x32, 0x29, 0x2d, 0x29,
0x25, 0x2b, 0x39, 0x00, 0x01, 0x03, 0x10])
self._writecommand(TFT.GMCTRP1)
self._writecommand(tft.GMCTRP1)
self._writedata(dataGMCTRP)
dataGMCTRN = bytearray([0x03, 0x1d, 0x07, 0x06, 0x2e, 0x2c, 0x29, 0x2d, 0x2e,
0x2e, 0x37, 0x3f, 0x00, 0x00, 0x02, 0x10])
self._writecommand(TFT.GMCTRN1)
self._writecommand(tft.GMCTRN1)
self._writedata(dataGMCTRN)
self._writecommand(TFT.NORON) #Normal display on.
self._writecommand(tft.NORON) #Normal display on.
pyb.delay(10)
self._writecommand(TFT.DISPON)
self._writecommand(tft.DISPON)
pyb.delay(100)
self.cs.high()
def maker( ) :
t = TFT(1, "X1", "X2")
t = tft(1, "X1", "X2")
print("Initializing")
t.initr()
t.fill(0)
return t
def makeb( ) :
t = TFT(1, "X1", "X2")
t = tft(1, "X1", "X2")
print("Initializing")
t.initb()
t.fill(0)
return t
def makeg( ) :
t = TFT(1, "X1", "X2")
t = tft(1, "X1", "X2")
print("Initializing")
t.initg()
# t.fill(0)

83
lib/cjmcu.py 100644
Wyświetl plik

@ -0,0 +1,83 @@
#Driver for CJMCU-IR temperature sensor. Uses mlx90614 but adds serial interface to it.
from pyb import UART
#Format of data output from the device:
#length: 9
# Byte0 Header Flags 0x66
# Byte1 Header Flags 0x66
# Byte2 data output mode (0x01 continuous output; 0x02 query output, the default for continuous output mode)
# Byte3 Measured data length (counted by Byte)
# Byte4 Temperature 1 Upper 8 bits
# Byte5 Temperature 1 Lower 8 bits
# Byte6 Temperature 2 Upper 8 bits
# Byte7 Temperature 2 Lower 8 bits
# Byte8 data parity (all data accumulation, take the low 8-bit)
#
#Celcius Temperature calculation method:
#
#Temperature = Data High 8 bits << 8 | Lower 8 bits of data, the result is the actual temperature multiplied by 100.
#
#Command instructions:
#Lenght: 4
#Byte0 Header Flags 0x66
#Byte1 Header Flags 0x66
#Byte2 Sets the command:
# 0x01 Continuous output mode
# 0x02 Query output mode
# 0x11 Set the baud rate to 9600
# 0x12 Set the baud rate to 57600
# 0x13 Set the baud rate to 115200
#Byte3 End of frame flag 0x56
def c2f( aValue ):
'''Celcius to Farenheit conversion.'''
return (aValue * 9.0 / 5.0) + 32.0
class cjmcu(object) :
"""docstring for cjmcu"""
_CONTINUOUS = const(1)
_POLL = const(2)
_RATEBASE = 0x11
_BAUD9600 = const(0)
_BAUD19200 = const(1)
_BAUD38400 = const(2)
def __init__(self, aLoc):
super(cjmcu, self).__init__()
self._uart = UART(aLoc, 9600)
self._mode = _POLL
self._output = bytearray(4)
self._output[0] = 0x66
self._output[1] = 0x66
self._output[2] = self._mode
self._output[3] = 0x56
self._input = bytearray(9)
self.update()
def write( self ) :
self._uart.write(self._output)
def read( self ) :
self._uart.readinto(self._input)
def update( self ) :
self.write()
self.read()
def setbaud( self, aBaud ) :
self._output[2] = _BAUDBASE + aBaud
self.update()
self._output[2] = self._mode
self._uart.deinit()
self._uart.init(9600 << aBaud)
def temps( self ) :
v1 = (self._input[4] << 8) | self._input[5]
v2 = (self._input[6] << 8) | self._input[7]
return (v1 / 100.0, v2 / 100.0)

55
lib/joystick.py 100644
Wyświetl plik

@ -0,0 +1,55 @@
import pyb
class joystick(object) :
_x_center = 2084.0
_y_center = 2114.0
_pos_x = 4095.0 - _x_center
_pos_y = 4095.0 - _y_center
def __init__( self, aX, aY, aB ) :
self._jx = pyb.ADC(aX)
self._jy = pyb.ADC(aY)
#PULL_DOWN doesn't report button press, need to use pull up resistor.
self._js = pyb.Pin(aB, pyb.Pin.IN, pyb.Pin.PULL_UP)
self._x = 0.0
self._y = 0.0
self._button = False
self._index = 0
self._xA = [0, 0, 0]
self._yA = [0, 0, 0]
@property
def x( self ) :
'''Return value from -1.0 to 1.0.'''
return self._x
@property
def y( self ) :
'''Return value from -1.0 to 1.0.'''
return self._y
@property
def button( self ) :
'''return True or False.'''
return self._button
def update( self ) :
self._xA[self._index] = self._jx.read()
self._yA[self._index] = self._jy.read()
self._index += 1
if self._index >= 3 :
self._index = 0
rx = float(sum(self._xA)) / 3.0 - joystick._x_center
ry = float(sum(self._yA)) / 3.0 - joystick._y_center
dx = joystick._pos_x if rx >= 0 else joystick._x_center
dy = joystick._pos_y if ry >= 0 else joystick._y_center
self._x = rx / dx
self._y = ry / dy
#Button in pressed when value is 0.
self._button = not self._js.value()

62
lib/mlx90614.py 100644
Wyświetl plik

@ -0,0 +1,62 @@
'''IR temperature sensor using I2C interface.'''
import pyb
def c2f( aValue ):
'''Celcius to Farenheit conversion.'''
return (aValue * 9.0 / 5.0) + 32.0
class mlx(object):
''' '''
_ADDRESS = const(0x5A)
#RAM
# _RAWIR1 = const(0x04)
# _RAWIR2 = const(0x05)
_TA = const(0x06)
_TOBJ1 = const(0x07)
_TOBJ2 = const(0x08)
#EEPROM
# _TOMAX = const(0x20)
# _TOMIN = const(0x21)
# _PWMCTRL = const(0x22)
# _TARANGE = const(0x23)
# _EMISS = const(0x24)
# _CONFIG = const(0x25)
# _ADDR = const(0x0E)
# _ID1 = const(0x3C)
# _ID2 = const(0x3D)
# _ID3 = const(0x3E)
# _ID4 = const(0x3F)
def __init__(self, aLoc):
'''aLoc is either 'X', 1, 'Y' or 2.'''
super(mlx, self).__init__()
self.i2c = pyb.I2C(aLoc, pyb.I2C.MASTER)
self._w1 = bytearray(2)
def read( self, aLoc ) :
'''Read 16 bit value and return.'''
self.i2c.mem_read(self._w1, _ADDRESS, aLoc) #, addr_size = 16)
return (self._w1[0] << 8) | self._w1[1]
# def write( self, aVal, aLoc ) :
# """Write 16 bit value to given address. aVal may be an int buffer."""
# self.i2c.mem_write(aVal, _ADDRESS, aLoc, addr_size = 16)
def readtemp( self, aLoc ) :
''' '''
temp = self.read(aLoc)
return (temp * 0.02) - 273.15
def ambienttemp( self ) :
return self.readtemp(_TA)
def objecttemp( self ) :
return self.readtemp(_TOBJ1)
def object2temp( self ) :
return self.readtemp(_TOBJ2)

Wyświetl plik

@ -5,9 +5,9 @@ import pyb
from time import sleep_us
class pca9865(object):
"""16 servo contoller. Use index 0-15 for the servo #."""
'''16 servo contoller. Use index 0-15 for the servo #.'''
_ADDRESS = 0x40
_ADDRESS = const(0x40)
_MODE1 = const(0)
_PRESCALE = const(0xFE)
@ -22,40 +22,47 @@ class pca9865(object):
# _ALLLED_OFF_H = const(0xFD)
_DEFAULTFREQ = const(60)
_MINPULSE = const(150)
_MINPULSE = const(120)
_MAXPULSE = const(600)
_PULSERANGE = const(450)
def __init__(self, aLoc) :
"""aLoc I2C pin location is either 1, 'X', 2 or'Y'."""
'''aLoc I2C pin location is either 1, 'X', 2 or'Y'.'''
super(pca9865, self).__init__()
self.i2c = pyb.I2C(aLoc, pyb.I2C.MASTER)
# print(self.i2c)
self._buffer = bytearray(4)
self._b1 = bytearray(1)
sleep_us(50)
self.reset()
self.minmax(_MINPULSE, _MAXPULSE)
def minmax( self, aMin, aMax ) :
'''Set min/max and calculate range.'''
self._min = aMin
self._max = aMax
self._range = aMax - aMin
def read( self, aLoc ) :
"""Read 8 byte value and return in bytearray"""
return self.i2c.mem_read(1, self._ADDRESS, aLoc)
'''Read 8 bit value and return.'''
return self.i2c.mem_read(self._b1, _ADDRESS, aLoc)
return self._b1[0]
def write( self, aVal, aLoc ) :
"""Write 8 bit value to given address. aVal may be an int buffer."""
self.i2c.mem_write(aVal, self._ADDRESS, aLoc)
self.i2c.mem_write(aVal, _ADDRESS, aLoc)
def reset( self ):
"""Reset the controller and set default frequency."""
'''Reset the controller and set default frequency.'''
self.write(0, _MODE1)
self.setfreq(_DEFAULTFREQ)
def setfreq( self, aFreq ) :
"""Set frequency for all servos. A good value is 60hz (default)."""
'''Set frequency for all servos. A good value is 60hz (default).'''
aFreq *= 0.9 #Correct for overshoot in frequency setting.
prescalefloat = (6103.51562 / aFreq) - 1 #25000000 / 4096 / freq.
prescale = int(prescalefloat + 0.5)
data = self.read(_MODE1)
oldmode = data[0]
oldmode = self.read(_MODE1)
newmode = (oldmode & 0x7F) | 0x10
self.write(newmode, _MODE1)
self.write(prescale, _PRESCALE)
@ -64,10 +71,10 @@ class pca9865(object):
self.write(oldmode | 0xA1, _MODE1) #This sets the MODE1 register to turn on auto increment.
def setpwm( self, aServo, aOn, aOff ) :
"""aServo = 0-15.
'''aServo = 0-15.
aOn = 16 bit on value.
aOff = 16 bit off value.
"""
'''
if 0 <= aServo <= 15 :
#Data = on-low, on-high, off-low and off-high. That's 4 bytes each servo.
loc = _LED0_ON_L + (aServo * 4)
@ -81,10 +88,24 @@ class pca9865(object):
raise Exception('Servo index {} out of range.'.format(str(aServo)))
def off( self, aServo ) :
"""Turn off a servo."""
'''Turn off a servo.'''
self.setpwm(aServo, 0, 0)
def setangle( self, aServo, aAng ) :
"""Set the angle 0-100%"""
val = _MINPULSE + ((_PULSERANGE * aAng) // 100)
self.setpwm(aServo, 0, val)
def alloff( self ) :
'''Turn all servos off.'''
for x in range(0, 16):
self.off(x)
def set( self, aServo, aPerc ) :
'''Set the 0-100%. If < 0 turns servo off.'''
if aPerc < 0 :
self.off(aServo)
else:
val = self._min + ((self._range * aPerc) // 100)
self.setpwm(aServo, 0, val)
def setangle( self, aServo, aAngle ) :
'''Set angle -90 to +90. < -90 is off.'''
#((a + 90.0) * 100.0) / 180.0
perc = int((aAngle + 90.0) * 0.5556) #Convert angle +/- 90 to 0-100%
self.set(aServo, perc)

Wyświetl plik

@ -1,4 +1,4 @@
# MicroPython TM1637 quad 7-segment LED display driver
# MicroPython tm1637 quad 7-segment LED display driver
from machine import Pin
from time import sleep_us
@ -10,8 +10,8 @@ _CMD_SET3 = const(128) # 0x80 data control command set
# 0-9, a-f, blank, dash
_SEGMENTS = [63,6,91,79,102,109,125,7,127,111,119,124,57,94,121,113,0,64]
class TM1637(object):
"""Library for the quad 7-segment LED display modules based on the TM1637
class tm1637(object):
"""Library for the quad 7-segment LED display modules based on the tm1637
LED driver."""
def __init__(self, clk, dio, brightness=7):
self.clk = clk