py driver more in line with c driver.

master
GuyCarver 2014-11-13 08:21:41 -05:00
rodzic 25b79d23cf
commit 2d57ca5ead
6 zmienionych plików z 63 dodań i 61 usunięć

Wyświetl plik

@ -74,26 +74,26 @@ def TFTColor( aR, aG, aB ) :
This assumes rgb 565 layout and will be incorrect for bgr.''' This assumes rgb 565 layout and will be incorrect for bgr.'''
return ((aR & 0xF8) << 8) | ((aG & 0xFC) << 3) | (aB >> 3) return ((aR & 0xF8) << 8) | ((aG & 0xFC) << 3) | (aB >> 3)
BLACK = 0
RED = TFTColor(0xFF, 0x00, 0x00)
MAROON = TFTColor(0x80, 0x00, 0x00)
GREEN = TFTColor(0x00, 0xFF, 0x00)
FOREST = TFTColor(0x00, 0x80, 0x80)
BLUE = TFTColor(0x00, 0x00, 0xFF)
NAVY = TFTColor(0x00, 0x00, 0x80)
CYAN = TFTColor(0x00, 0xFF, 0xFF)
YELLOW = TFTColor(0xFF, 0xFF, 0x00)
PURPLE = TFTColor(0xFF, 0x00, 0xFF)
WHITE = TFTColor(0xFF, 0xFF, 0xFF)
GRAY = TFTColor(0x80, 0x80, 0x80)
ScreenSize = (128, 160) ScreenSize = (128, 160)
class TFT(object) : class TFT(object) :
"""Sainsmart TFT 7735 display driver.""" """Sainsmart TFT 7735 display driver."""
BLACK = 0
RED = TFTColor(0xFF, 0x00, 0x00)
MAROON = TFTColor(0x80, 0x00, 0x00)
GREEN = TFTColor(0x00, 0xFF, 0x00)
FOREST = TFTColor(0x00, 0x80, 0x80)
BLUE = TFTColor(0x00, 0x00, 0xFF)
NAVY = TFTColor(0x00, 0x00, 0x80)
CYAN = TFTColor(0x00, 0xFF, 0xFF)
YELLOW = TFTColor(0xFF, 0xFF, 0x00)
PURPLE = TFTColor(0xFF, 0x00, 0xFF)
WHITE = TFTColor(0xFF, 0xFF, 0xFF)
GRAY = TFTColor(0x80, 0x80, 0x80)
@staticmethod @staticmethod
def makecolor(aR, aG, aB): def color(aR, aG, aB):
'''Create a 565 rgb TFTColor value''' '''Create a 565 rgb TFTColor value'''
return TFTColor(aR, aG, aB) return TFTColor(aR, aG, aB)
@ -112,18 +112,6 @@ class TFT(object) :
self.spi = pyb.SPI(aLoc, pyb.SPI.MASTER, baudrate = rate, polarity = 1, phase = 0, crc=None) self.spi = pyb.SPI(aLoc, pyb.SPI.MASTER, baudrate = rate, polarity = 1, phase = 0, crc=None)
self.colorData = bytearray(2) self.colorData = bytearray(2)
self.windowLocData = bytearray(4) self.windowLocData = bytearray(4)
self.BLACK = BLACK
self.RED = RED
self.MAROON = MAROON
self.GREEN = GREEN
self.FOREST = FOREST
self.BLUE = BLUE
self.NAVY = NAVY
self.CYAN = CYAN
self.YELLOW = YELLOW
self.PURPLE = PURPLE
self.WHITE = WHITE
self.GRAY = GRAY
def size( self ): def size( self ):
return self._size return self._size

Wyświetl plik

@ -2,7 +2,6 @@
import ultrasonic import ultrasonic
import pyb import pyb
from ST7735 import NAVY, CYAN, TFTColor
import terminalfont import terminalfont
ZeroPoint = (0, 0) ZeroPoint = (0, 0)
@ -28,10 +27,10 @@ def round( aValue ) :
'''Round float value to 2 decimal places''' '''Round float value to 2 decimal places'''
return (aValue - (aValue % 0.01)) return (aValue - (aValue % 0.01))
def getrgb( aDistance ) : def getrgb( aDisplay, aDistance ) :
'''Get an interpolated TFTColor based on distance. '''Get an interpolated color based on distance.
Uses the COLORS list.''' Uses the COLORS list.'''
clr = NAVY clr = aDisplay.NAVY
def interp(l, v0, v1): def interp(l, v0, v1):
return int(v0 * (1.0 - l) + (v1 * l)) return int(v0 * (1.0 - l) + (v1 * l))
@ -46,7 +45,7 @@ def getrgb( aDistance ) :
r = interp(l, r0, r1) r = interp(l, r0, r1)
g = interp(l, g0, g1) g = interp(l, g0, g1)
b = interp(l, b0, b1) b = interp(l, b0, b1)
clr = TFTColor(r,g,b) clr = aDisplay.color(r,g,b)
break break
return clr return clr

Wyświetl plik

@ -1,13 +1,10 @@
import pyb import pyb
import time import time
from ST7735 import CYAN, RED, GREEN, YELLOW, TFTColor
#todo: Pick a ransom spot, random radius, random color #todo: Pick a ransom spot, random radius, random color
#todo: Animate the spot #todo: Animate the spot
colorA = [CYAN, RED, GREEN, YELLOW]
class bomb(object): class bomb(object):
"""Animate a circle on the screen.""" """Animate a circle on the screen."""
def __init__(self, aPos, aRadius, aColor, aSpeed): def __init__(self, aPos, aRadius, aColor, aSpeed):
@ -28,7 +25,7 @@ class bomb(object):
self.color = 0 self.color = 0
self.curradius = 1.0 self.curradius = 1.0
aDisplay.fillcircle(self.pos, rad, color) aDisplay.fillcircle(self.pos, int(rad), color)
return self.state != 2 return self.state != 2
@ -40,19 +37,20 @@ class bomber(object):
"""Control a bunch of bombs.""" """Control a bunch of bombs."""
def __init__(self, aDisplay): def __init__(self, aDisplay):
self.display = aDisplay self.display = aDisplay
self.ds = self.display.size()
self.numbombs = 4 self.numbombs = 4
self.bombs = [] self.bombs = []
self.sw = pyb.Switch() self.sw = pyb.Switch()
def addbomb( self ) : def addbomb( self ) :
x = int(randval(self.display.size[0])) x = int(randval(self.ds[0]))
y = int(randval(self.display.size[1])) y = int(randval(self.ds[1]))
rad = randval(20) + 5 rad = randval(20) + 5
r = pyb.rng() & 0xFF r = pyb.rng() & 0xFF
g = pyb.rng() & 0xFF g = pyb.rng() & 0xFF
b = pyb.rng() & 0xFF b = pyb.rng() & 0xFF
spd = randval(30.0) + 1.0 spd = randval(30.0) + 1.0
clr = TFTColor(r,g,b) #colorA[pyb.rng() & 0x03] clr = self.display.color(r,g,b)
self.bombs.insert(0, bomb((x, y), rad, clr, spd)) self.bombs.insert(0, bomb((x, y), rad, clr, spd))
def run( self ) : def run( self ) :

Wyświetl plik

@ -2,7 +2,6 @@
# can run arbitrary Python, but best to keep it minimal # can run arbitrary Python, but best to keep it minimal
import pyb import pyb
from ST7735 import *
#pyb.main('main.py') # main script to run after this one #pyb.main('main.py') # main script to run after this one
#pyb.usb_mode('CDC+MSC') # act as a serial and a storage device #pyb.usb_mode('CDC+MSC') # act as a serial and a storage device

Wyświetl plik

@ -1,7 +1,6 @@
#Show level bubble run by the accelerometer #Show level bubble run by the accelerometer
import pyb import pyb
from ST7735 import RED, CYAN
import terminalfont import terminalfont
ZeroPoint = (0, 0) ZeroPoint = (0, 0)
@ -14,7 +13,7 @@ class Bubble(object):
self.pos = aCenter self.pos = aCenter
self.oldpos = self.pos self.oldpos = self.pos
self.speed = aSpeed self.speed = aSpeed
self.radius = aRadius self.radius = int(aRadius)
self.color = aColor self.color = aColor
self.accel = pyb.Accel() self.accel = pyb.Accel()
@ -22,15 +21,15 @@ class Bubble(object):
xtilt, ytilt, _ = self.accel.filtered_xyz() xtilt, ytilt, _ = self.accel.filtered_xyz()
# xtilt = self.accel.x() # xtilt = self.accel.x()
# ytilt = self.accel.y() # ytilt = self.accel.y()
ds = aDisplay.size()
xs = (ds[0] / 2) / 70.0
ys = (ds[1] / 2) / 60.0
xs = (aDisplay.size[0] / 2) / 70.0 self.oldpos = (int(self.pos[0]), int(self.pos[1]))
ys = (aDisplay.size[1] / 2) / 60.0
self.oldpos = self.pos
self.pos = (int(self.center[0] + xtilt * xs), int(self.center[1] - ytilt * ys)) self.pos = (int(self.center[0] + xtilt * xs), int(self.center[1] - ytilt * ys))
s = "x: " + str(xtilt) + " y: " + str(ytilt) s = "x: " + str(xtilt) + " y: " + str(ytilt)
aDisplay.fillrect(ZeroPoint, (aDisplay.size[0], 10), 0) aDisplay.fillrect(ZeroPoint, (ds[0], 8), 0)
aDisplay.text(ZeroPoint, s, CYAN, terminalfont.terminalfont) aDisplay.text(ZeroPoint, s, aDisplay.CYAN, terminalfont.terminalfont)
# aTime *= self.speed # aTime *= self.speed
# self.pos.x += xtilt * aTime # self.pos.x += xtilt * aTime
# self.pos.y -= ytilt * aTime # self.pos.y -= ytilt * aTime
@ -45,8 +44,9 @@ class Bubble(object):
def _clamp( self, aDisplay ) : def _clamp( self, aDisplay ) :
l = self.radius l = self.radius
t = l t = l
r = aDisplay.size[0] - l ds = aDisplay.size()
b = aDisplay.size[1] - l r = ds[0] - l
b = ds[1] - l
self.pos = (max(l, min(self.pos[0], r)), max(t, min(self.pos[1], b))) self.pos = (max(l, min(self.pos[0], r)), max(t, min(self.pos[1], b)))
class Level(object): class Level(object):
@ -54,10 +54,10 @@ class Level(object):
controlled by the accelerometer.""" controlled by the accelerometer."""
def __init__(self, aDisplay): def __init__(self, aDisplay):
self.display = aDisplay self.display = aDisplay
cx, cy = aDisplay.size cx, cy = aDisplay.size()
cx /= 2 cx /= 2
cy /= 2 cy /= 2
self.bubble = Bubble((cx, cy), 10.0, 5, RED) self.bubble = Bubble((cx, cy), 10.0, 5, aDisplay.RED)
def run( self ) : def run( self ) :
self.display.fill(0) self.display.fill(0)

38
main.py
Wyświetl plik

@ -5,27 +5,45 @@
# Balance.main() # Balance.main()
from basicfont import * # from seriffont import *
from seriffont import * # from sysfont import *
from terminalfont import * from terminalfont import *
from bombs import bomber # from SonarDisplay import SonarDisplay
from level import Level
t = makeg() pyt = 0
if pyt :
from ST7735 import makeg
t = makeg()
else:
t = pyb.TFT("x", "X1", "X2") #makegp()
t.initg()
t.fill(0)
import TFT
TFT.run(t)
def tst( aColor ): def tst( aColor ):
s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=_+[]{}l;'<>?,./!@#$%^&*():" s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=_+[]{}l;'<>?,./!@#$%^&*():"
t.drawstring(Point(0, 0), s, aColor, basicfont) # t.text(Point(0, 0), s, aColor, basicfont)
t.drawstring(Point(0, 40), s, aColor, seriffont) # t.text(Point(0, 40), s, aColor, seriffont)
t.drawstring(Point(0, 80), s, aColor, terminalfont) t.text((0, 40), s, aColor, terminalfont)
# tst(BLUE) # tst(BLUE)
def s(aRot, aColor): def s(aRot, aColor):
t.setrotation(aRot) t.rotation(aRot)
tst(aColor) tst(aColor)
# from bombs import bomber
# t.rotation(2)
# b = bomber(t) # b = bomber(t)
t.setrotation(2) # b.run()
from level import Level
l = Level(t) l = Level(t)
l.run() l.run()
# sd = SonarDisplay(t, "X3", "X4")
# sd.run()