kopia lustrzana https://github.com/GuyCarver/MicroPython
Update library files.
rodzic
cf2e72261c
commit
742fe88d57
45
lib/OLED.py
45
lib/OLED.py
|
@ -1,4 +1,5 @@
|
|||
#driver for the diymall 9.6 oled display.
|
||||
#128x32 pixel support with help from adafruit libraries.
|
||||
|
||||
import pyb
|
||||
|
||||
|
@ -22,12 +23,14 @@ _SETPRECHARGE = const(0xD9)
|
|||
|
||||
_SETMULTIPLEX = const(0xA8)
|
||||
|
||||
_SETLOWCOLUMN = const(0x00)
|
||||
_SETHIGHCOLUMN = const(0x10)
|
||||
#_SETLOWCOLUMN = const(0x00)
|
||||
#_SETHIGHCOLUMN = const(0x10)
|
||||
|
||||
_SETSTARTLINE = const(0x40)
|
||||
|
||||
_MEMORYMODE = const(0x20)
|
||||
_COLUMNADDR = const(0x21)
|
||||
_PAGEADDR = const(0x22)
|
||||
|
||||
_COMSCANINC = const(0xC0)
|
||||
_COMSCANDEC = const(0xC8)
|
||||
|
@ -70,25 +73,26 @@ _VERTICAL_AND_LEFT_HORIZONTAL_SCROLL = const(0x2A)
|
|||
class oled(object) :
|
||||
"""diyMall OLED 9.6 128x64 pixel display driver."""
|
||||
|
||||
def __init__( self, aLoc ) :
|
||||
def __init__( self, aLoc, aHeight = 64 ) :
|
||||
"""aLoc I2C pin location is either 1 for 'X' or 2 for 'Y'."""
|
||||
self._size = (128, 64)
|
||||
self._size = (128, aHeight)
|
||||
self._rotation = 0
|
||||
self._inverted = False
|
||||
self._on = False
|
||||
self.i2c = pyb.I2C(aLoc, pyb.I2C.MASTER, baudrate = 200000)
|
||||
self.bytes = self.size[0] * self.size[1] // 8
|
||||
self.pages = aHeight // 8
|
||||
self.bytes = self.size[0] * self.pages
|
||||
self.buffer = bytearray(self.bytes + 1)
|
||||
self.buffer[0] = 0x40 #data write start command at very start of buffer.
|
||||
|
||||
self.data = bytearray(2)
|
||||
self.data[0] = 0
|
||||
self.data[0] = 0 #0 = Command mode.
|
||||
|
||||
self.command = _DISPLAYOFF
|
||||
self.command = _SETDISPLAYCLOCKDIV
|
||||
self.command = 0x80 #suggested ratio.
|
||||
self.command = _SETMULTIPLEX
|
||||
self.command = 0x3F
|
||||
self.command = aHeight - 1
|
||||
self.command = _SETDISPLAYOFFSET
|
||||
self.command = 0x0
|
||||
self.command = _SETSTARTLINE #| 0x0
|
||||
|
@ -99,9 +103,8 @@ class oled(object) :
|
|||
self.command = _SEGREMAP + 0x01
|
||||
self.command = _COMSCANDEC
|
||||
self.command = _SETCOMPINS
|
||||
self.command = 0x12
|
||||
self.command = _SETCONTRAST
|
||||
self.command = 0xCF
|
||||
self.command = 0x12 if aHeight == 64 else 0x02
|
||||
self.dim = 0xCF
|
||||
self.command = _SETPRECHARGE
|
||||
self.command = 0xF1
|
||||
self.command = _SETVCOMDETECT
|
||||
|
@ -156,6 +159,16 @@ class oled(object) :
|
|||
self._inverted = aTF
|
||||
self.command = _INVERTDISPLAY if aTF else _NORMALDISPLAY
|
||||
|
||||
@property
|
||||
def dim( self ):
|
||||
return self._dim
|
||||
|
||||
@dim.setter
|
||||
def dim( self, aValue ):
|
||||
self._dim = aValue
|
||||
self.command = _SETCONTRAST
|
||||
self.command = self._dim
|
||||
|
||||
@micropython.native
|
||||
def fill( self, aValue ) :
|
||||
for x in range(1, self.bytes + 1):
|
||||
|
@ -316,7 +329,7 @@ class oled(object) :
|
|||
def doscrollDiag( self, start, stop, aDir ) :
|
||||
self.command = _SET_VERTICAL_SCROLL_AREA
|
||||
self.command = 0x00
|
||||
self.command = self.size()[1]
|
||||
self.command = self.size[1]
|
||||
self.command = aDir
|
||||
self.command = 0x00
|
||||
self.command = start
|
||||
|
@ -335,9 +348,13 @@ class oled(object) :
|
|||
self.command = _DEACTIVATE_SCROLL
|
||||
|
||||
def display( self ) :
|
||||
self.command = _SETLOWCOLUMN #| 0x00
|
||||
self.command = _SETHIGHCOLUMN #| 0x00
|
||||
self.command = _SETSTARTLINE #| 0x00
|
||||
self.command = _COLUMNADDR
|
||||
self.command = 0
|
||||
self.command = self.size[0] - 1
|
||||
self.command = _PAGEADDR
|
||||
self.command = 0
|
||||
self.command = self.pages - 1
|
||||
|
||||
#buffer starts with 0x40 in 1st byte which is the command to start the buffer write.
|
||||
self.write(self.buffer)
|
||||
|
||||
|
|
|
@ -1,33 +1,30 @@
|
|||
|
||||
#Driver for the bts7960 43A high power motor controller.
|
||||
|
||||
from pwm import pwm
|
||||
from pyb import Pin
|
||||
from machine import Pin, PWM
|
||||
from utime import sleep_ms
|
||||
|
||||
class motor( ):
|
||||
"""Control a motor connected to the bts7960 motor controller."""
|
||||
|
||||
def __init__( self, aOnOff, aForward, aBackward ) :
|
||||
"""aOnOff is the pin to control free movement of the motor.
|
||||
aForward and aBackward are tuples indicating pin and timer channel # for PWM pins.
|
||||
aForward = (pin name, timer channel #)
|
||||
aBackward = (pin name, timer channel #)
|
||||
Need to make sure the given timer channel # is associated with the pin
|
||||
or an exception will be raised.
|
||||
#Examples:
|
||||
m1 = motor('Y1', ('Y2', 8), ('Y3', 10))
|
||||
m2 = motor('X1', ('X2', 5), ('X3', 5)) """
|
||||
self._onoff = Pin(aOnOff, Pin.OUT_PP)
|
||||
self._forward = pwm(*aForward)
|
||||
self._backward = pwm(*aBackward)
|
||||
def __init__( self, aForward, aBackward, aFreq = 100 ) :
|
||||
"""aForward = tuple (On Pin #, PWM Pin #)
|
||||
aBackward = tuple (On Pin #, PWM Pin #)
|
||||
aFreq = max frequency.
|
||||
#Example:
|
||||
m1 = motor((19, 22), (21, 23))
|
||||
"""
|
||||
self._onf = Pin(aForward[0], Pin.OUT)
|
||||
self._forward = PWM(Pin(aForward[1], Pin.OUT))
|
||||
self._onb = Pin(aBackward[0], Pin.OUT)
|
||||
self._backward = PWM(Pin(aBackward[1], Pin.OUT))
|
||||
self._maxfreq = aFreq
|
||||
self._speed = 0
|
||||
|
||||
self.seton( self, aOn ) :
|
||||
@staticmethod
|
||||
def seton( aPin, aOn ) :
|
||||
'''Set on/off (free wheeling) state of motor.'''
|
||||
if aOn :
|
||||
self._onoff.high()
|
||||
else:
|
||||
self._onoff.low()
|
||||
aPin.value(1 if aOn else 0)
|
||||
|
||||
@property
|
||||
def speed( self ) : return self._speed
|
||||
|
@ -36,29 +33,31 @@ class motor( ):
|
|||
def speed( self, aValue ) :
|
||||
'''Set velocity and direction of motor with -100 <= aValue <= 100.'''
|
||||
self._speed = aValue
|
||||
pos = True
|
||||
|
||||
on = False
|
||||
f = 0
|
||||
b = 0
|
||||
if aValue == 0 :
|
||||
motor.seton(self._onb, False)
|
||||
motor.seton(self._onf, False)
|
||||
return
|
||||
elif aValue < 0 :
|
||||
aValue = -aValue
|
||||
pos = False
|
||||
|
||||
if aValue < 0 :
|
||||
on = True
|
||||
f = 0
|
||||
b = min(100, -aValue)
|
||||
else:
|
||||
on = True
|
||||
f = min(100, aValue)
|
||||
b = 0
|
||||
f = self.p2hz(min(100, aValue))
|
||||
motor.seton(self._onf, pos)
|
||||
motor.seton(self._onb, not pos)
|
||||
self._forward.freq(f)
|
||||
self._backward.freq(f)
|
||||
|
||||
self.seton(on)
|
||||
self._forward.pulse_width_percent = f
|
||||
self._backward.pulse_width_percent = b
|
||||
def p2hz( self, aPerc ) :
|
||||
return int((self._maxfreq * aPerc) // 100)
|
||||
|
||||
def brake( self ) :
|
||||
""" Brake the motor by sending power both directions, then shut it all down. """
|
||||
self._forward.pulse_width_percent = 100
|
||||
self._backward.pulse_width_percent = 100
|
||||
self.seton(True)
|
||||
self._forward.freq(self.p2hz(100))
|
||||
self._backward.freq(self.p2hz(100))
|
||||
motor.seton(self._onf, True)
|
||||
motor.seton(self._onb, True)
|
||||
sleep_ms(500)
|
||||
self.speed = 0
|
||||
|
||||
|
|
|
@ -0,0 +1,251 @@
|
|||
|
||||
from machine import UART, Pin
|
||||
from time import sleep_ms
|
||||
|
||||
# HC05 Bluetooth board ----------------------------------------
|
||||
#See docs at:
|
||||
# https://www.itead.cc/wiki/Serial_Port_Bluetooth_Module_(Master/Slave)_:_HC-05
|
||||
|
||||
#COMMANDS:
|
||||
# AT - Does nothing but get an ok.
|
||||
# All of the following commands start with AT+ and end with \r\n
|
||||
# VERSION?
|
||||
# NAME= : This is the name that will show up in windows.
|
||||
# NAME? :
|
||||
# ADDR? : see default address
|
||||
# UART? : See baudrate
|
||||
# UART= : Set baudrate, stop, parity
|
||||
# ROLE? : See role of bt module(1=master/0=slave)
|
||||
# ROLE= : Set role of bt module(1=master/0=slave/2=slave loop)
|
||||
# RESET : Reset and exit AT mode
|
||||
# ORGL : Restore factory settings
|
||||
# PSWD? : see default password
|
||||
# PSWD= : set default password
|
||||
# RMAAD : Remove all paired devices.
|
||||
# CMODE? : Get the CMODE.
|
||||
# CMODE= : 0 = fixed addr, 1 = connect any, 2 = slave loop.
|
||||
# BIND= : Param is fixed address (Default 00:00:00:00:00:00). Send data separated by ,
|
||||
# BIND? : Return data is separated by :
|
||||
# INQM=<P1>,<P2>,<P3> : P1 = 0:standard, 1:rssi, P2 = Max # of devices. P3 = Timeout (1-48: 1-61s)
|
||||
# INQ : Start inquiring state.
|
||||
# INQC : Cancel inquiring state.
|
||||
# PAIR=<addr> : Match device. Don't know what the difference between PAIR and LINK are.
|
||||
# LINK=<addr> : Connect device.
|
||||
# CLASS= : Device class.
|
||||
# POLAR=<P1>,<P2> : 1, 0 for example.
|
||||
# STATE? : Get Initialized, Ready, Pairable, Inquiring, Connectin, Connected, Disconnected, Unknown
|
||||
# MRAD? : Get address of most recently used authenticated device.
|
||||
# ADCN? : Get authenticated device count.
|
||||
# DISC : Disconnect
|
||||
|
||||
#HC05 notes:
|
||||
# My HC05 has an AT button on it. I do not use that button at all.
|
||||
# I've found that connecting at baud 38400 is the best way to ensure communication in both AT and DATA mode.
|
||||
# To send most AT commands the 'EN' pin must be pulled high.
|
||||
# When the LED is flashing quickly, all AT commands work at all baud rates.
|
||||
# But on reset command, the LED will blink slowly. In this state AT commands will only work on baud 38400.
|
||||
|
||||
#Master connect sequence:
|
||||
# h.at = 1
|
||||
# h.clearpaired()
|
||||
# h.role(1)
|
||||
# h.reset()
|
||||
# h.cmode(1)
|
||||
# h.inqm(0,5,5)
|
||||
# h.init()
|
||||
# h.inq()
|
||||
|
||||
class hc05(object):
|
||||
'''hc05 Bluetooth serial device driver. This is simply a light UART wrapper
|
||||
with addition AT command methods to customize the device.'''
|
||||
|
||||
_OK = 'OK\r\n'
|
||||
_COMMA = ','
|
||||
_EQUAL = '='
|
||||
_AT = 'AT+'
|
||||
|
||||
#Commands that are both ? and =.
|
||||
_NAME = 'NAME'
|
||||
_PSWD = 'PSWD'
|
||||
_UART = 'UART'
|
||||
_ROLE = 'ROLE'
|
||||
_CMODE = 'CMODE'
|
||||
|
||||
def __init__( self, atpin, uart, aBaud = 38400, aParity = None, aStop = 1 ):
|
||||
''' uart = uart #0-2, baudrate must match what is set on the hc05 or 38400 (at-mode).
|
||||
It is suggested to set the baud rate to 38400, then at mode will always work with the current baud rate.
|
||||
atpin is the pin connected to the hc05 "en" pin. This is used to enter at mode.'''
|
||||
self._uart = UART(uart, aBaud, parity = aParity, stop = aStop)
|
||||
self._at = Pin(atpin, Pin.OUT, Pin.PULL_UP)
|
||||
|
||||
def __del__( self ) : self._uart.deinit()
|
||||
|
||||
def any( self ) : return self._uart.any()
|
||||
|
||||
def write( self, astring ) : return self._uart.write(astring)
|
||||
def writechar( self, achar ) : self._uart.writechar(achar)
|
||||
|
||||
def read( self, num = None ) : return self._uart.read(num)
|
||||
def readline( self ) :
|
||||
res = self._uart.readline()
|
||||
return res.decode('utf-8') if res else ''
|
||||
|
||||
def readchar( self ) : return self._uart.readchar()
|
||||
def readinto( self, buf, count = None ) : return self._uart.readinto(buf, count)
|
||||
|
||||
def clear( self ) :
|
||||
while self.any() :
|
||||
self._uart.readline()
|
||||
|
||||
def _test( self ) :
|
||||
self.clear()
|
||||
self._uart.write('AT\r\n')
|
||||
for i in range(1, 100) :
|
||||
sleep_ms(10)
|
||||
if self.any():
|
||||
return self.readline() == hc05._OK
|
||||
return False
|
||||
|
||||
@property
|
||||
def at( self ) :
|
||||
return self._at()
|
||||
|
||||
@at.setter
|
||||
def at( self, aArg ) :
|
||||
'''Put into at Mode.'''
|
||||
self._at(aArg)
|
||||
|
||||
def _write( self, aValue ) :
|
||||
return self._uart.write(aValue)
|
||||
|
||||
def _getreply( self ) :
|
||||
res = ''
|
||||
for i in range(1, 100) :
|
||||
if self.any() :
|
||||
res = self.readline()
|
||||
break
|
||||
sleep_ms(10)
|
||||
return res
|
||||
|
||||
def _cmd( self, *aCommands ) :
|
||||
'''Send AT command, wait a bit then return True if OK'''
|
||||
self.clear()
|
||||
self._write(hc05._AT)
|
||||
for c in aCommands:
|
||||
self._write(c)
|
||||
self._write('\r\n')
|
||||
return self._getreply()
|
||||
|
||||
def _qcmd( self, *aCommands ) :
|
||||
'''Send AT command, wait a bit then return result string'''
|
||||
self.clear()
|
||||
self._write(hc05._AT)
|
||||
for c in aCommands:
|
||||
self._write(c)
|
||||
self._write('?\r\n')
|
||||
bres = self._getreply()
|
||||
self.readline() #Read the 'OK\r\n' line.
|
||||
return bres
|
||||
|
||||
def qaddr( self ) :
|
||||
return self._qcmd('ADDR')
|
||||
|
||||
def qname( self ) :
|
||||
'''Get the name (AT mode only)'''
|
||||
return self._qcmd(hc05._NAME)
|
||||
|
||||
def name( self, aName ) :
|
||||
'''Set the name to show up on the connecting device'''
|
||||
return self._cmd(hc05._NAME, hc05._EQUAL, aName)
|
||||
|
||||
def qpswd( self ) :
|
||||
'''Get the password'''
|
||||
return self._qcmd(hc05._PSWD)
|
||||
|
||||
def pswd( self, aPin ) :
|
||||
'''Set the password'''
|
||||
return self._cmd(hc05._PSWD, hc05._EQUAL, str(aPin))
|
||||
|
||||
def qversion( self ) :
|
||||
'''Get version #'''
|
||||
return self._qcmd('VERSION')
|
||||
|
||||
def quart( self ) :
|
||||
'''Get uart baud, stop, parity'''
|
||||
return self._qcmd(hc05._UART)
|
||||
|
||||
def uart( self, aBaud, aStop, aParity ) :
|
||||
'''Set uart baud, stop, parity'''
|
||||
return self._cmd(hc05._UART, hc05._EQUAL, str(aBaud), hc05._COMMA, str(aStop), hc05._COMMA, str(aParity))
|
||||
|
||||
def qrole( self ) :
|
||||
'''Get role, 0 = slave, 1 = master'''
|
||||
return self._qcmd(hc05._ROLE)
|
||||
|
||||
def role( self, aRole ) :
|
||||
'''Set role, 0 = slave, 1 = master'''
|
||||
return self._cmd(hc05._ROLE, hc05._EQUAL, str(aRole))
|
||||
|
||||
def reset( self ) :
|
||||
'''Reset hc05'''
|
||||
return self._cmd('RESET')
|
||||
|
||||
def clearpaired( self ) :
|
||||
'''Clear all paired device'''
|
||||
return self._cmd('RMAAD')
|
||||
|
||||
def qcmode( self ) :
|
||||
'''Get connect mode: 0 = fixed address, 1 = any, 2 = slave loop'''
|
||||
return self._qcmd(hc05._CMODE)
|
||||
|
||||
def cmode( self, aMode ) :
|
||||
'''Set connect mode: 0 = fixed address, 1 = any, 2 = slave loop'''
|
||||
return self._cmd(hc05._CMODE, hc05._EQUAL, str(aMode))
|
||||
|
||||
def inqm( self, aProtocol, aDevices, aTimeout ) :
|
||||
'''Set inquiry mode: aProtocol = 0/standard, 1/rssi, aDevices = Max # devices, aTimeout = 1-48'''
|
||||
return self._cmd('INQM=', str(aProtocol), hc05._COMMA, str(aDevices), hc05._COMMA, str(aTimeout))
|
||||
|
||||
def init( self ) :
|
||||
'''Initialize hc05 with given settings.'''
|
||||
return self._cmd('INIT')
|
||||
|
||||
def inq( self ) :
|
||||
'''Enter inquiry state'''
|
||||
return self._cmd('INQ')
|
||||
|
||||
def inqcancel( self ) :
|
||||
'''Cancel inquiry state'''
|
||||
return self._cmd('INQC')
|
||||
|
||||
def setclass( self, aClass ) :
|
||||
'''Set class #'''
|
||||
return self._cmd('CLASS=', str(aClass))
|
||||
|
||||
def polar( self, aArg ) :
|
||||
return self._cmd('POLAR=', aArg)
|
||||
|
||||
def qstate( self ) :
|
||||
'''Get current state'''
|
||||
return self._qcmd('STATE')
|
||||
|
||||
def qdevicecount( self ) :
|
||||
'''Get authenticated defice count'''
|
||||
return self._qcmd('ADCN')
|
||||
|
||||
def qmrad( self ) :
|
||||
'''Get most recently used device address'''
|
||||
return self._qcmd('MRAD')
|
||||
|
||||
def pair( self, aDevice, aTimeout ) :
|
||||
'''Pair with given device'''
|
||||
return self._cmd('PAIR=', aDevice, str(aTimeout))
|
||||
|
||||
def link( self, aAddress ) :
|
||||
'''Link with given device'''
|
||||
return self._cmd('LINK=', aAddress)
|
||||
|
||||
def disconnect( self ) :
|
||||
'''Disconnect from device'''
|
||||
return self._cmd('DISC')
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
|
||||
from pyb import UART, repl_uart, udelay
|
||||
|
||||
# HC06 Bluetooth board ----------------------------------------
|
||||
|
||||
# This opens connection with Bluetooth module connected to Y1, Y2 (UART 6)
|
||||
# Then it sets the repl output to this UART.
|
||||
#COMMANDS AT - does nothing but get an ok.
|
||||
# The posible baudrates are:
|
||||
# AT+BAUD1-------1200
|
||||
# AT+BAUD2-------2400
|
||||
# AT+BAUD3-------4800
|
||||
# AT+BAUD4-------9600 - Default for hc-06
|
||||
# AT+BAUD5------19200
|
||||
# AT+BAUD6------38400
|
||||
# AT+BAUD7------57600
|
||||
# AT+BAUD8-----115200
|
||||
# AT+BAUD9-----230400
|
||||
# AT+BAUDA-----460800
|
||||
# AT+BAUDB-----921600
|
||||
# AT+BAUDC----1382400
|
||||
# AT+VERSION
|
||||
# AT+NAMEnewname This is the name that will show up in windows.
|
||||
# AT+PIN???? set 4 digit pairing pin.
|
||||
|
||||
class hc06(object):
|
||||
"""HC06 (JY-MCU) Bluetooth serial device driver. This is simply a light UART wrapper
|
||||
with addition AT command methods to customize the device."""
|
||||
|
||||
def __init__( self, uart, baudrate ):
|
||||
""" uart = uart #1-6, baudrate must match what is set on the JY-MCU.
|
||||
Needs to be a #1-C. """
|
||||
self._uart = UART(uart, baudrate)
|
||||
|
||||
def __del__( self ) : self._uart.deinit()
|
||||
|
||||
def any( self ) : return self._uart.any()
|
||||
|
||||
def write( self, astring ) : return self._uart.write(astring)
|
||||
def writechar( self, achar ) : self._uart.writechar(achar)
|
||||
|
||||
def read( self, num = None ) : return self._uart.read(num)
|
||||
def readline( self ) : return self._uart.readline()
|
||||
def readchar( self ) : return self._uart.readchar()
|
||||
def readall( self ) : return self._uart.readall()
|
||||
def readinto( self, buf, count = None ) : return self._uart.readinto(buf, count)
|
||||
|
||||
def _cmd( self, cmd ) :
|
||||
""" Send AT command, wait a bit then return result string. """
|
||||
self._uart.write("AT+" + cmd)
|
||||
udelay(500)
|
||||
return self.readline()
|
||||
|
||||
def baudrate( self, rate ) :
|
||||
""" Set the baud rate. Needs to be #1-C. """
|
||||
return self._cmd("BAUD" + str(rate))
|
||||
|
||||
def name( self, name ) :
|
||||
""" Set the name to show up on the connecting device. """
|
||||
return self._cmd("NAME" + name)
|
||||
|
||||
def pin( self, pin ) :
|
||||
""" Set the given 4 digit numeric pin. """
|
||||
return self._cmd("PIN" + str(pin))
|
||||
|
||||
def version( self ) : return self._cmd("VERSION")
|
||||
|
||||
def setrepl( self ) : repl_uart(self._uart)
|
||||
|
Ładowanie…
Reference in New Issue