Skip to content

Commit

Permalink
Add 256 color support.
Browse files Browse the repository at this point in the history
Add API function pew.palette([pal], [offset=0]) that takes a sequence of R, G, B, R, G, B... byte values for up to 256 colors (blue component is ignored here) and an optional offset to cyclically shift the palette. Passing no palette preserves the previous one and only adjusts the offset, which may be useful for some animations. Passing an empty palette or None sets a default palette (described at https://hackaday.io/project/167369-picopew/log/171107-256-colors). Changes take effect at the next pew.show().

The palette is preserved by pew.init(), much like pew.brightness(), so that a choice made by the user before starting a program stays in effect, unless the program explicitly sets its own palette. Program tint.py is provided to allow selection of some offsets in the default palette that match the color schemes of other PewPew devices.

Generating the default palette in code is a lot more compact that including it as a bytes literal. Zlib compression and Base64 encoding would be a little better still but I did not examine their memory and time requirements.
  • Loading branch information
cwalther committed Nov 18, 2019
1 parent 748473e commit ef48c49
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
29 changes: 26 additions & 3 deletions pew.py
Expand Up @@ -36,6 +36,28 @@ def brightness(level):
_i2c.writeto_mem(80, 0xfd, b'\x01') _i2c.writeto_mem(80, 0xfd, b'\x01')




def palette(pal=False, offset=0):
global _palette, _palettesize, _paletteoffset
if pal is not False:
if not pal:
pal = bytearray(768)
for i in range(256):
if (i & 0xc0) == 0:
b = (255, 121, 42, 7)[(i >> 4) & 3]
pal[3*i] = (b*(0, 0, 255, 160)[i & 3] + 127)//255
pal[3*i+1] = b*(i & 1)
elif (i & 0xc0) == 0x40:
b = (0, 15, 89, 255)[i & 3]
pal[3*i] = (b*(0, 64, 255, 255)[(i >> 4) & 3] + 127)//255
pal[3*i+1] = (b*(255, 255, 255, 0)[(i >> 4) & 3] + 127)//255
else:
pal[3*i] = (0, 1, 2, 4, 8, 15, 24, 35, 50, 68, 89, 114, 143, 176, 213, 255)[(i >> 3) & 15]
pal[3*i+1] = (0, 2, 10, 28, 60, 106, 171, 255)[i & 7]
_palette = pal
_palettesize = len(_palette)//3
_paletteoffset = offset % _palettesize


def show(pix): def show(pix):
global _buffer, _i2c global _buffer, _i2c


Expand All @@ -45,11 +67,11 @@ def show(pix):
for y in range(8): for y in range(8):
position = y*width position = y*width
for x in range(8): for x in range(8):
pixel = buffer[position] pixel = 3*((buffer[position] + _paletteoffset) % _palettesize)
position += 1 position += 1
_buffer[index] = 0xff if pixel & 1 else 0 _buffer[index] = _palette[pixel + 1]
index += 1 index += 1
_buffer[index] = 0xa0 if pixel & 2 else 0 _buffer[index] = _palette[pixel]
index += 1 index += 1
_i2c.writeto_mem(80, 0x00, _buffer) _i2c.writeto_mem(80, 0x00, _buffer)


Expand Down Expand Up @@ -246,4 +268,5 @@ def handler(p):
_i2c.writeto_mem(80, 0xfd, b'\x00') # go to LED control page _i2c.writeto_mem(80, 0xfd, b'\x00') # go to LED control page
_i2c.writeto_mem(80, 0x00, b'\xff'*16) # all 128 LEDs on _i2c.writeto_mem(80, 0x00, b'\xff'*16) # all 128 LEDs on


palette(None)
brightness(7) brightness(7)
44 changes: 44 additions & 0 deletions programs/tint.py
@@ -0,0 +1,44 @@
import pew

pew.init()
choice = {64: 1, 80: 2, 96: 3, 112: 4}.get(pew._paletteoffset, 0)
pew.palette(None)
screen = pew.Pix()

keyhistory = 0
def keyevents():
global keyhistory
keys = pew.keys()
events = keys & (~keyhistory | (keyhistory & (keyhistory >> 8) & (keyhistory >> 16) & (keyhistory >> 24)))
keyhistory = ((keyhistory & 0x3FFFFF) << 8) | keys
return events

def paint():
if choice == 0:
for i in range(3):
screen.box(i+1, 2*i+1, 1, 2, 2)
else:
for i in range(3):
screen.box(32+i+1, 2*i+1, 1, 2, 1)
for c in range(1, 5):
if choice == c:
for i in range(3):
screen.box(48+c*16+i+1, 2*i+1, 1+c, 2, 2)
else:
screen.box(48+c*16+1, 1, 1 + c + (0 if choice > c else 1), 6, 1)
pew.show(screen)

paint()
while pew.keys():
pew.tick(0.1)
while True:
k = keyevents()
if k & pew.K_UP:
choice = (choice - 1) % 5
if k & pew.K_DOWN:
choice = (choice + 1) % 5
if k & pew.K_O:
pew.palette(offset = (0, 64, 80, 96, 112)[choice])
break
paint()
pew.tick(0.06)

0 comments on commit ef48c49

Please sign in to comment.