Code:
# Arrows change distances among segments;
# SELECT saves settings to file;
# 5: load settings from file;
# 1-3: change distance among figures;
# 2-8, 4-6: change segments size;
from appuifw import *
from graphics import *
import camera
import e32
from key_codes import *
import appuifw
from camera import *
import miso # Needed for backlight refresh
PixThreshold = 66
TopLeftX = [ 30, 65, 65, 30, 20, 20, 30]
TopLeftY = [ 20, 35, 80,110, 80, 35, 65]
# the keys as usual
class Keyboard(object):
def __init__(self,onevent=lambda:None):
self._keyboard_state={}
self._downs={}
self._onevent=onevent
def handle_event(self,event):
if event['type'] == appuifw.EEventKeyDown:
cod=event['scancode']
if not self.is_down(cod):
self._downs[cod]=self._downs.get(cod,0)+1
self._keyboard_state[cod]=1
elif event['type'] == appuifw.EEventKeyUp:
self._keyboard_state[event['scancode']]=0
self._onevent()
def is_down(self,scancode):
return self._keyboard_state.get(scancode,0)
def pressed(self,scancode):
if self._downs.get(scancode,0):
self._downs[scancode]-=1
return True
return False
keyboard=Keyboard()
def InitVars():
global offset, threshold, XWidth, YWidth, XHeight, YHeight, num
offset = 70
num=[0,offset,2*offset,3*offset,4*offset,5*offset]
threshold=32000
XWidth = 25
XHeight = 10
YWidth = XHeight
YHeight = XWidth
def Reader():
global canvas, PixThreshold, XWidth, YWidth, XHeight, YHeight
canvas.blit(img, (0,0,150,150))
TotColorR = 0
TotColorG = 0
TotColorB = 0
x = 0
y = 0
n = 0
for n in range(2): # Loop through various figures
miso.reset_inactivity_time() # Keep light on
idx = 0
segm=[9,9,9,9,9,9,9] # Initialize segments to "8" (1=on=black, 2=off=transparent)
for idx in range(7): # Loop through segments.
#print "idx=",idx,":",
TotColorR = 0 # Per each segment calculate a separate sum.
TotColorG = 0
TotColorB = 0
# x=TopLeftX[idx]-10
# y=TopLeftY[idx]-10
# Examine a box inside the segment; values of each pixel are summed up into
# a single value,which is the compared to a threshold to determine if
# the segment is on (=black=under threshold) or off (=white=over threshold).
if (idx == 0) or (idx == 6) or (idx == 3): # Horizontal segments
Xstart = num[n]+TopLeftX[idx]
Xend = num[n]+TopLeftX[idx]+XWidth
Ystart = TopLeftY[idx]
Yend = TopLeftY[idx]+XHeight
else:
Xstart = num[n]+TopLeftX[idx]
Xend = num[n]+TopLeftX[idx]+YWidth
Ystart = TopLeftY[idx]
Yend = TopLeftY[idx]+YHeight
for x in range(Xstart, Xend):
for y in range(Ystart, Yend):
tempR, tempG, tempB = img.getpixel((x,y))[0]
TotColorR = TotColorR + tempR
TotColorG = TotColorG + tempG
TotColorB = TotColorB + tempB
if tempR+tempG+tempB > PixThreshold:
img.point((x,y),65000)
else:
img.point((x,y),10000)
#print TotColorR + TotColorG + TotColorB
if TotColorR+TotColorG+TotColorB > threshold:
segm[idx]=0
#img.point((x,y),65000)
else:
segm[idx]=1
#img.point((x,y),0)
print "result=",segm
#stringa = repr(segm[0])+repr(segm[1])
#img.text((10,10),stringa)
canvas.blit(img, (0,0,150,120))
def Overdraw():
global num, XWidth, YWidth, XHeight, YHeight
for n in range(2):
for idx in range(7):
for x in range(num[n]+TopLeftX[idx], num[n]+TopLeftX[idx]+XSize):
for y in range(TopLeftY[idx],TopLeftY[idx]+YSize):
img.point((x,y),0)
def Adjust():
global offset, XWidth, YWidth, XHeight, YHeight, num,PixThreshold
running=1
switch = 1
x=8
y=10
XINC = 1
XINC2 = 2
YINC = 1
YINC2 = 2
YINC3 =4
YINC4 = 4
OFFSETINC = 10
THRESHINC = 10
# create a loop to get stuff handled that needs to be done again and again
while running:
# activate the camera again taking in pictures again and again (till key 2 is pressed)
if switch == 1:
screen_picture = camera.take_photo(size = (160,120))
# draw the new picture on the canvas in the upper part -> numbers are coordinates of the location of the picture on the canvas
img.blit(screen_picture,target=(x,y,168,130),scale=1)
Reader()
#Overdraw()
# redraw the canvas
handle_redraw(())
e32.ao_yield()
# scan for left softkey to switch on the camera again
if keyboard.pressed(EScancodeRightArrow):
#TopLeftX[0] = TopLeftX[0]+ XINC
TopLeftX[1] = TopLeftX[1]+ XINC2
TopLeftX[2] = TopLeftX[2]+ XINC2
#TopLeftX[3] = TopLeftX[3]+ XINC
#TopLeftX[6] = TopLeftX[6]+ XINC
if keyboard.pressed(EScancodeLeftArrow):
#TopLeftX[0] = TopLeftX[0]- XINC
TopLeftX[1] = TopLeftX[1]- XINC2
TopLeftX[2] = TopLeftX[2]- XINC2
#TopLeftX[3] = TopLeftX[3]- XINC
#TopLeftX[6] = TopLeftX[6]- XINC
if keyboard.pressed(EScancodeUpArrow):
TopLeftY[1] = TopLeftY[1]- YINC
TopLeftY[5] = TopLeftY[5]- YINC
TopLeftY[6] = TopLeftY[6]- YINC2
TopLeftY[2] = TopLeftY[2]- YINC3
TopLeftY[4] = TopLeftY[4]- YINC3
TopLeftY[3] = TopLeftY[3]- YINC4
if keyboard.pressed(EScancodeDownArrow):
TopLeftY[1] = TopLeftY[1]+ YINC
TopLeftY[5] = TopLeftY[5]+ YINC
TopLeftY[6] = TopLeftY[6]+ YINC2
TopLeftY[2] = TopLeftY[2]+ YINC3
TopLeftY[4] = TopLeftY[4]+ YINC3
TopLeftY[3] = TopLeftY[3]+ YINC4
if keyboard.pressed(EScancode1):
offset = offset - OFFSETINC
num=[0,offset,2*offset,3*offset,4*offset,5*offset]
if keyboard.pressed(EScancode3):
offset = offset + OFFSETINC
num=[0,offset,2*offset,3*offset,4*offset,5*offset]
if keyboard.pressed(EScancode4):
YWidth = YWidth - 1
XWidth = XWidth - 2
if keyboard.pressed(EScancode6):
YWidth = YWidth + 1
XWidth = XWidth + 2
if keyboard.pressed(EScancode2):
YHeight = YHeight - 2
XHeight = XHeight - 1
if keyboard.pressed(EScancode8):
YHeight = YHeight + 2
XHeight = XHeight + 1
if keyboard.pressed(EScancode5): # Test capture
switch = 1
Reader()
if keyboard.pressed(EScancode7):
PixThreshold = PixThreshold - THRESHINC
if keyboard.pressed(EScancode9):
PixThreshold = PixThreshold + THRESHINC
# scann for left softkey to switch on the camera again
if keyboard.pressed(EScancodeLeftSoftkey):
switch = 1
# scan for the Select key (navigaiton key pressed)
# if pressed the go out of the previous loop of getting in pictures again and again
if keyboard.pressed(EScancodeSelect):
switch = 2
e32.ao_yield()
filename=u'c:\\LCD.ini'
out_file = open(filename,"w")
out_file.write("Offset=" + repr(offset) + "\n")
out_file.write("BoxWidth=" + repr(XSize) + "\n")
out_file.write("BoxHeight=" + repr(YSize) + "\n")
for i in range(7):
out_file.write("TopLeftX[" + repr(i) + "]=" + repr(TopLeftX[i])+"\n")
out_file.write("TopLeftY[" + repr(i) + "]=" + repr(TopLeftY[i])+"\n")
out_file.close()
e32.ao_yield()
if keyboard.pressed(EScancode5):
out_file.open(filename,"r")
out_file.readline()
out_file.close()
def quit():
global running
running=0
appuifw.app.set_exit()
# define the redraw function (redraws the screen)
def handle_redraw(rect):
canvas.blit(img)
appuifw.app.screen='normal'
# create an empty image
img=Image.new((176,208))
canvas=appuifw.Canvas(event_callback=keyboard.handle_event, redraw_callback=handle_redraw)
#appuifw.app.body=canvas
app.exit_key_handler=quit
screen_picture = camera.take_photo(size = (160,120))
InitVars()
Adjust()
NOTE: looks like a bug in forum engine prevents codes from containing "[code]" string!