| 13 | # be shown, handling also text wrapping if a line is longer than |
| 14 | # the screen width. |
| 15 | class Scroller: |
| 16 | Font8x8 = 0 |
| 17 | Font4x6 = 1 |
| 18 | StateActive = 0 # Display active |
| 19 | StateDimmed = 1 # Dispaly still active but minimum contrast set |
| 20 | StateSaver = 2 # Screen saver: only icons at random places on screen. |
| 21 | |
| 22 | def __init__(self, display, icons=None, dim_time=10, ss_time=120, xres=128, yres=64): |
| 23 | self.display = display # Display driver |
| 24 | self.icons = icons |
| 25 | self.lines = [] |
| 26 | self.xres = xres |
| 27 | self.yres = yres |
| 28 | # The framebuffer of MicroPython only supports 8x8 fonts so far, so: |
| 29 | self.select_font("big") |
| 30 | self.last_update = time.time() |
| 31 | # OLED saving system state. We write text at an x,y offset, that |
| 32 | # can be of 0 or 1 pixels. This way we use pixels more evenly |
| 33 | # creating a less evident image ghosting effect in the more |
| 34 | # used pixels. |
| 35 | self.xoff = 0 |
| 36 | self.yoff = 0 |
| 37 | self.dim_t = dim_time # Inactivity to set to lower contrast. |
| 38 | self.screensave_t = ss_time # Inactivity to enable screen saver. |
| 39 | self.state = self.StateActive |
| 40 | self.contrast = 255 |
| 41 | |
| 42 | # Set maximum display contrast. It will be dimmed after some inactivity |
| 43 | # time. |
| 44 | def set_contrast(self,contrast): |
| 45 | self.contrast = contrast |
| 46 | |
| 47 | # Get current contrast based on inactivity time. |
| 48 | def get_contrast(self): |
| 49 | if self.state == self.StateActive: |
| 50 | return self.contrast |
| 51 | elif self.state == self.StateDimmed or self.state == self.StateSaver: |
| 52 | return 1 # Still pretty visible but in direct sunlight |
| 53 | |
| 54 | # Update self.state based on last activity time. |
| 55 | def update_screensaver_state(self): |
| 56 | inactivity = time.time() - self.last_update |
| 57 | if inactivity > self.screensave_t: |
| 58 | self.state = self.StateSaver |
| 59 | elif inactivity > self.dim_t: |
| 60 | self.state = self.StateDimmed |
| 61 | else: |
| 62 | self.state = self.StateActive |
| 63 | |
| 64 | def select_font(self,fontname): |
| 65 | if fontname == "big": |
| 66 | self.font = self.Font8x8 |
| 67 | self.font_width = 8 |
| 68 | self.font_height = 8 |
| 69 | elif fontname == "small": |
| 70 | # Use 5/7 to provide the required spacing. The font 8x8 |
| 71 | # already includes spacing. |
| 72 | self.font = self.Font4x6 |