diff --git a/Lib/ST7735/__init__.py b/Lib/ST7735/__init__.py index 1361759..c6f535b 100644 --- a/Lib/ST7735/__init__.py +++ b/Lib/ST7735/__init__.py @@ -74,26 +74,26 @@ def TFTColor( aR, aG, aB ) : This assumes rgb 565 layout and will be incorrect for bgr.''' 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) class TFT(object) : """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 - def makecolor(aR, aG, aB): + def color(aR, aG, aB): '''Create a 565 rgb TFTColor value''' 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.colorData = bytearray(2) 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 ): return self._size diff --git a/SonarDisplay.py b/SonarDisplay.py index 8650c2a..3812be0 100644 --- a/SonarDisplay.py +++ b/SonarDisplay.py @@ -2,7 +2,6 @@ import ultrasonic import pyb -from ST7735 import NAVY, CYAN, TFTColor import terminalfont ZeroPoint = (0, 0) @@ -28,10 +27,10 @@ def round( aValue ) : '''Round float value to 2 decimal places''' return (aValue - (aValue % 0.01)) -def getrgb( aDistance ) : - '''Get an interpolated TFTColor based on distance. +def getrgb( aDisplay, aDistance ) : + '''Get an interpolated color based on distance. Uses the COLORS list.''' - clr = NAVY + clr = aDisplay.NAVY def interp(l, v0, v1): return int(v0 * (1.0 - l) + (v1 * l)) @@ -46,7 +45,7 @@ def getrgb( aDistance ) : r = interp(l, r0, r1) g = interp(l, g0, g1) b = interp(l, b0, b1) - clr = TFTColor(r,g,b) + clr = aDisplay.color(r,g,b) break return clr diff --git a/bombs.py b/bombs.py index 0c59be8..0a2b27d 100644 --- a/bombs.py +++ b/bombs.py @@ -1,13 +1,10 @@ import pyb import time -from ST7735 import CYAN, RED, GREEN, YELLOW, TFTColor #todo: Pick a ransom spot, random radius, random color #todo: Animate the spot -colorA = [CYAN, RED, GREEN, YELLOW] - class bomb(object): """Animate a circle on the screen.""" def __init__(self, aPos, aRadius, aColor, aSpeed): @@ -28,7 +25,7 @@ class bomb(object): self.color = 0 self.curradius = 1.0 - aDisplay.fillcircle(self.pos, rad, color) + aDisplay.fillcircle(self.pos, int(rad), color) return self.state != 2 @@ -40,19 +37,20 @@ class bomber(object): """Control a bunch of bombs.""" def __init__(self, aDisplay): self.display = aDisplay + self.ds = self.display.size() self.numbombs = 4 self.bombs = [] self.sw = pyb.Switch() def addbomb( self ) : - x = int(randval(self.display.size[0])) - y = int(randval(self.display.size[1])) + x = int(randval(self.ds[0])) + y = int(randval(self.ds[1])) rad = randval(20) + 5 r = pyb.rng() & 0xFF g = pyb.rng() & 0xFF b = pyb.rng() & 0xFF 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)) def run( self ) : diff --git a/boot.py b/boot.py index 0bee9b0..998e077 100644 --- a/boot.py +++ b/boot.py @@ -2,7 +2,6 @@ # can run arbitrary Python, but best to keep it minimal import pyb -from ST7735 import * #pyb.main('main.py') # main script to run after this one #pyb.usb_mode('CDC+MSC') # act as a serial and a storage device diff --git a/level.py b/level.py index c582160..62038b9 100644 --- a/level.py +++ b/level.py @@ -1,7 +1,6 @@ #Show level bubble run by the accelerometer import pyb -from ST7735 import RED, CYAN import terminalfont ZeroPoint = (0, 0) @@ -14,7 +13,7 @@ class Bubble(object): self.pos = aCenter self.oldpos = self.pos self.speed = aSpeed - self.radius = aRadius + self.radius = int(aRadius) self.color = aColor self.accel = pyb.Accel() @@ -22,15 +21,15 @@ class Bubble(object): xtilt, ytilt, _ = self.accel.filtered_xyz() # xtilt = self.accel.x() # 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 - ys = (aDisplay.size[1] / 2) / 60.0 - - self.oldpos = self.pos + self.oldpos = (int(self.pos[0]), int(self.pos[1])) self.pos = (int(self.center[0] + xtilt * xs), int(self.center[1] - ytilt * ys)) s = "x: " + str(xtilt) + " y: " + str(ytilt) - aDisplay.fillrect(ZeroPoint, (aDisplay.size[0], 10), 0) - aDisplay.text(ZeroPoint, s, CYAN, terminalfont.terminalfont) + aDisplay.fillrect(ZeroPoint, (ds[0], 8), 0) + aDisplay.text(ZeroPoint, s, aDisplay.CYAN, terminalfont.terminalfont) # aTime *= self.speed # self.pos.x += xtilt * aTime # self.pos.y -= ytilt * aTime @@ -45,8 +44,9 @@ class Bubble(object): def _clamp( self, aDisplay ) : l = self.radius t = l - r = aDisplay.size[0] - l - b = aDisplay.size[1] - l + ds = aDisplay.size() + r = ds[0] - l + b = ds[1] - l self.pos = (max(l, min(self.pos[0], r)), max(t, min(self.pos[1], b))) class Level(object): @@ -54,10 +54,10 @@ class Level(object): controlled by the accelerometer.""" def __init__(self, aDisplay): self.display = aDisplay - cx, cy = aDisplay.size + cx, cy = aDisplay.size() cx /= 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 ) : self.display.fill(0) diff --git a/main.py b/main.py index b28b1a2..efca0e5 100644 --- a/main.py +++ b/main.py @@ -5,27 +5,45 @@ # Balance.main() -from basicfont import * -from seriffont import * +# from seriffont import * +# from sysfont import * from terminalfont import * -from bombs import bomber -from level import Level +# from SonarDisplay import SonarDisplay -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 ): s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=_+[]{}l;'<>?,./!@#$%^&*():" - t.drawstring(Point(0, 0), s, aColor, basicfont) - t.drawstring(Point(0, 40), s, aColor, seriffont) - t.drawstring(Point(0, 80), s, aColor, terminalfont) +# t.text(Point(0, 0), s, aColor, basicfont) +# t.text(Point(0, 40), s, aColor, seriffont) + t.text((0, 40), s, aColor, terminalfont) # tst(BLUE) def s(aRot, aColor): - t.setrotation(aRot) + t.rotation(aRot) tst(aColor) +# from bombs import bomber +# t.rotation(2) # b = bomber(t) -t.setrotation(2) +# b.run() + +from level import Level l = Level(t) l.run() + +# sd = SonarDisplay(t, "X3", "X4") +# sd.run()