Skip to content
Snippets Groups Projects
Select Git revision
  • 3ab6aa3a6d0506e805caa19369bef279c1c789b4
  • wip-bootstrap default
  • dualcore
  • ch3/leds
  • ch3/time
  • master
6 results

builtin_bin_intbig.py

Blame
  • 4bit.py 2.31 KiB
    from st3m.application import Application
    import sys_display, math, random
    
    
    class App(Application):
        def __init__(self, app_ctx):
            super().__init__(app_ctx)
    
        def on_enter(self, vm):
            super().on_enter(vm)
            sys_display.set_mode(4)
            self.y = 0
            self.xa = -1.5
            self.xb = 1.5
            self.ya = -2.0
            self.yb = 1.0
    
        def draw(self, ctx: Context):
            fb_info = sys_display.fb(0)
            fb = fb_info[0]
    
            max_iterations = 30
    
            width = fb_info[1]
            height = fb_info[2]
            stride = fb_info[3]
    
            for chunk in range(3):  # do 3 scanlines at a time
                y = self.y
                if y < height:
                    zy = y * (self.yb - self.ya) / (height - 1) + self.ya
                    inners = 0
                    for x in range(width):
                        zx = x * (self.xb - self.xa) / (width - 1) + self.xa
                        z = zy + zx * 1j
                        c = z
                        reached = 0
                        for i in range(max_iterations):
                            if abs(z) > 2.0:
                                break
                            z = z * z + c
                            reached = i
                        val = reached * 255 / max_iterations
                        val = math.sqrt(val / 255) * 16
                        if val > 15:
                            val = 15
                        fb[int((y * stride * 2 + x) / 2)] |= int(val) << ((x & 1) * 4)
                    self.y += 1
    
        def think(self, ins, delta_ms):
            super().think(ins, delta_ms)
            if self.input.buttons.app.right.pressed:
                pal = bytearray(256 * 3)
                modr = random.getrandbits(7) + 1
                modg = random.getrandbits(7) + 1
                modb = random.getrandbits(7) + 1
    
                for i in range(256):
                    pal[i * 3] = int((i % modr) * (255 / modr))
                    pal[i * 3 + 1] = int((i % modg) * (255 / modg))
                    pal[i * 3 + 2] = int((i % modb) * (255 / modb))
                sys_display.set_palette(pal)
            if self.input.buttons.app.left.pressed:
                pal = bytearray(256 * 3)
                for i in range(256):
                    pal[i * 3] = i
                    pal[i * 3 + 1] = i
                    pal[i * 3 + 2] = i
                sys_display.set_palette(pal)
    
    
    if __name__ == "__main__":
        import st3m.run
    
        st3m.run.run_app(App)