]>
vault307.fbx.one Git - PicoTamachibi.git/blob - icon.py
564bd2fc1d44538afbaeca391c42e90f91395900
2 from machine
import Pin
8 """ Models an icon and all the properties it requires """
18 def __init__(self
, filename
:None, width
=None, height
=None, x
=None, y
=None, name
=None):
19 """ Sets up the default values """
23 self
.__height
= height
30 if filename
is not None:
31 self
.__image
= self
.loadicons(filename
)
35 """ gets the icon image """
41 """ Sets the icon image """
47 """ Gets the X value """
54 """ Sets the X value """
67 def width(self
, value
):
68 """ Sets the icon width """
75 """ Returns height """
80 def height(self
, value
):
81 """ Gets the height """
94 def name(self
, value
):
95 """ Sets the icon name """
102 """ Gets the y value """
109 """ Sets the Y value """
115 def invert(self
)->bool:
116 """ Flips the bits in the image so white become black etc and returns the image """
117 print("Invert is", self
.__invert
)
121 def invert(self
, value
:bool):
122 """ Inverts the icon colour """
125 for x
in range(0,self
.width
):
126 for y
in range(0, self
.height
):
127 pxl
= image
.pixel(x
,y
)
134 self
.__invert
= value
135 # print("Invert is", self.__invert)
137 def loadicons(self
, file):
139 with open(file, 'rb') as f
:
140 f
.readline() # magic number
141 f
.readline() # creator comment
142 f
.readline() # dimensions
143 data
= bytearray(f
.read())
144 fbuf
= framebuf
.FrameBuffer(data
, self
.__width
,self
.__height
, framebuf
.MONO_HLSB
)
145 # print(self.__name, self.__width, self.__height)
149 """ Models the toolbar """
151 __framebuf
= framebuf
.FrameBuffer(bytearray(160*64*1), 160, 16, framebuf
.MONO_HLSB
)
153 __selected_item
= None
154 __selected_index
= -1 # -1 means no item selected
157 # print("building toolbar")
158 self
.__framebuf
= framebuf
.FrameBuffer(bytearray(160*64*8), 160, 16, framebuf
.MONO_HLSB
)
160 def additem(self
, icon
):
161 self
.__icon
_array
.append(icon
)
162 print("mehr icons...")
165 def remove(self
, icon
):
166 self
.__icon
_array
.remove(icon
)
169 return len(self
.__icon
_array
)
177 for icon in self.__icon_array:
180 if type(icon) == Icon:
181 self.__framebuf.blit(icon.image, x, 0)
182 if type(icon) == Animate:
183 self.__framebuf.blit(icon.__frames[icon.__current_frame].image, x, 0)
185 x += icon.width + self.spacer
191 """ returns the spacer value"""
195 def spacer(self
, value
):
196 """ Sets the spacer value"""
197 self
.__spacer
= value
199 def show(self
, oled
):
200 oled
.blit(self
.__framebuf
, 0,0)
203 def select(self
, index
, oled
):
204 """ Set the item in the index to inverted """
205 # for item in self.__icon_array:
206 # item.invert = False
207 self
.__icon
_array
[index
].invert
= True
208 self
.__selected
_index
= index
211 for loopcount
in range(len(self
.__icon
_array
)):
212 offset
= loopcount
- offsetvalue
213 zeiger
= (index
+ offset
) % len(self
.__icon
_array
)
215 if type(self
.__icon
_array
[zeiger
]) == Icon
:
216 self
.__framebuf
.blit(self
.__icon
_array
[zeiger
].image
, x
, 0)
217 if type(self
.__icon
_array
[zeiger
]) == Animate
:
218 self
.__framebuf
.blit(self
.__icon
_array
[zeiger
].__frames
[self
.__icon
_array
[zeiger
].__current
_frame
].image
, x
, 0)
220 x
+= self
.__icon
_array
[zeiger
].width
+ self
.spacer
223 def unselect(self
, index
, oled
):
224 self
.__icon
_array
[index
].invert
= False
225 self
.__selected
_index
= -1
229 def selected_item(self
):
230 """ Returns the name of the currently selected icon """
231 self
.__selected
_item
= self
.__icon
_array
[self
.__selected
_index
].name
232 return self
.__selected
_item
237 __speed
= "normal" # Other speeds are 'fast' and 'slow' - it just adds frames or skips frames
239 __done
= False # Has the animation completed
242 __animation_type
= "default"
251 """ other animations types:
261 def set(self
, value
:bool):
271 """ Returns the current speed """
275 def speed(self
, value
:str):
276 if value
in ['very slow','slow','normal','fast']:
278 if value
== 'very slow':
280 self
.__speed
_value
= 10
283 self
.__speed
_value
= 1
284 if value
== "normal":
286 self
.__speed
_value
= 0
288 print(value
, "is not a valid value, try 'fast','normal' or 'slow'")
291 def animation_type(self
):
292 return self
.__animation
_type
294 @animation_type.setter
295 def animation_type(self
, value
):
296 if value
in ['default','loop','bounce','reverse']:
297 self
.__animation
_type
= value
299 print(value
," is not a valid Animation type - it should be 'loop','bounce','reverse' or 'default'")
301 def __init__(self
, frames
=None, animation_type
:str=None,x
:int=None,y
:int=None, width
:int=None, height
:int=None, filename
=None):
302 """ setup the animation """
311 self
.__height
= height
312 self
.__current
_frame
= 0
313 if frames
is not None:
314 self
.__frames
= frames
316 self
.__loop
_count
= 1
317 if animation_type
is not None:
318 self
.animation_type
= animation_type
320 self
.__filename
= filename
324 """ Returns the current filename"""
325 return self
.__filename
328 def filename(self
, value
):
329 """ Sets the filename """
330 self
.__filename
= value
333 """ progress the current frame """
334 if self
.__speed
== 'normal':
335 self
.__current
_frame
+=1
337 if self
.__speed
in ['very slow','slow']:
341 self
.__current
_frame
+=1
342 self
.__pause
= self
.__speed
_value
344 if self
.__speed
== 'fast':
345 if self
.__current
_frame
< self
.frame_count
+2:
346 self
.__current
_frame
+=2
348 self
.__current
_frame
+=1
351 if self
.__speed
== 'normal':
352 self
.__current
_frame
-=1
354 if self
.__speed
in ['very slow','slow']:
358 self
.__current
_frame
-=1
359 self
.__pause
= self
.__speed
_value
361 if self
.__speed
== 'fast':
362 if self
.__current
_frame
< self
.frame_count
+2:
363 self
.__current
_frame
-=2
365 self
.__current
_frame
-=1
368 """ load the animation files """
370 # load the files from disk
371 if not self
.__cached
:
375 if (file.startswith(self
.__filename
)) and (file.endswith('.pbm')):
376 array
.append(Icon(filename
=file, width
=self
.__width
, height
=self
.__height
, x
=self
.__x
, y
=self
.__y
, name
=file))
377 self
.__frames
= array
381 """ free up memory """
384 self
.__cached
= False
387 def animate(self
, oled
):
388 """ Animates the frames based on the animation type and for the number of times specified """
390 cf
= self
.__current
_frame
# Current Frame number - used to index the frames array
391 frame
= self
.__frames
[cf
]
392 oled
.blit(frame
.image
, frame
.x
, frame
.y
)
394 if self
.__animation
_type
== "loop":
395 # Loop from the first frame to the last, for the number of cycles specificed, and then set done to True
398 if self
.__current
_frame
> self
.frame_count
:
399 self
.__current
_frame
= 0
400 self
.__loop
_count
-=1
401 if self
.__loop
_count
== 0:
404 if self
.__animation
_type
== "bouncing":
406 # Loop from the first frame to the last, and then back to the first again, then set done to True
409 if self
.__current
_frame
== 0:
410 if self
.__loop
_count
== 0:
413 if self
.__loop
_count
>0:
414 self
.__loop
_count
-=1
416 self
.__bouncing
= False
417 if self
.__loop
_count
== -1:
420 self
.__bouncing
= False
421 if (self
.__current
_frame
< self
.frame_count
) and (self
.__current
_frame
>0):
424 if self
.__current
_frame
== 0:
425 if self
.__loop
_count
== 0:
427 elif self
.__loop
_count
== -1:
432 self
.__loop
_count
-= 1
433 elif self
.__current
_frame
== self
.frame_count
:
435 self
.__bouncing
= True
439 if self
.__animation
_type
== "default":
440 # loop through from first frame to last, then set done to True
442 if self
.__current
_frame
== self
.frame_count
:
443 self
.__current
_frame
= 0
449 def frame_count(self
):
450 """ Returns the total number of frames in the animation """
451 return len(self
.__frames
)-1
455 """ Has the animation completed """
462 def loop(self
, no
:int=None):
463 """ Loops the animation
464 if no is None or -1 the animation will continue looping until animate.stop() is called """
467 self
.__loop
_count
= no
469 self
.__loop
_count
= -1
470 self
.__animation
_type
= "loop"
473 self
.__loop
_count
= 0
474 self
.__bouncing
= False
477 def bounce(self
, no
:int=None):
478 """ Loops the animation forwared, then backward, the number of time specified in no,
479 if there is no number provided it will animate infinately """
481 self
.__animation
_type
= "bouncing"
483 self
.__loop
_count
= no
485 self
.__loop
_count
= -1
489 """ Gets the icon width """
493 def width(self
, value
):
494 """ Sets the icon width """
499 """ Gets the icon height """
503 def height(self
, value
):
504 """ Sets the icon height """
505 self
.__height
= value
509 """ Models a button, check the status with is_pressed """
511 # The private variables
514 __button_down
= False
516 def __init__(self
, pin
:int):
517 """ Sets up the button """
519 self
.__pin
= Pin(pin
, Pin
.IN
, Pin
.PULL_DOWN
)
520 self
.__pressed
= False
523 def is_pressed(self
)->bool:
524 """ Returns the current state of the button """
526 if self
.__pin
.value() == 0:
527 self
.__button
_down
= False
529 if self
.__pin
.value() == 1:
530 if not self
.__button
_down
:
531 # print("button pressed")
532 self
.__button
_down
= True
538 """ Models events that can happen, with timers and pop up messages """
542 __timer
= -1 # -1 means no timer set
547 def __init__(self
, name
=None, sprite
=None, value
=None, callback
=None):
548 """ Create a new event """
553 self
.__sprite
= sprite
556 if callback
is not None:
557 self
.__callback
= callback
562 """ Return the name of the event"""
568 def name(self
, value
):
569 """ Set the name of the Event"""
576 """ Gets the current value """
581 def value(self
, value
):
582 """ Sets the current value """
588 """ Gets the image sprite """
594 def sprite(self
, value
):
595 """ Sets the image sprite """
602 """ Return the message """
604 return self
.__message
608 def message(self
, value
):
609 """ Set the message """
610 self
.__message
= value
612 def popup(self
, oled
):
613 # display popup window
617 fbuf
= framebuf
.FrameBuffer(bytearray(128 * 48 * 1), 128, 48, framebuf
.MONO_HLSB
)
618 fbuf
.rect(0,0,128,48, 1)
619 fbuf
.blit(self
.sprite
.image
, 5, 10)
620 fbuf
.text(self
.message
, 32, 18)
621 oled
.blit(fbuf
, 0, 16)
627 """ Gets the current timer value """
633 def timer(self
, value
):
634 """ Sets the current timer value """
641 """ Get the timer in MS """
643 return self
.__timer
_ms
647 def timer_ms(self
, value
):
648 """ Set the timer in MS """
649 self
.__timer
_ms
= value
653 """ Progresses the animation on frame """
656 if self
.__timer
_ms
>= self
.__timer
:
657 if self
.__callback
is not None:
658 print("poop check callback")
663 # print("Timer Alert!")