Skip to content
Snippets Groups Projects
Select Git revision
  • 096d7319fb60ded1bed05d8b5ec0364fd99d474c
  • main default protected
  • blm_dev_chan
  • release/1.4.0 protected
  • widgets_draw
  • return_of_melodic_demo
  • task_cleanup
  • mixer2
  • dx/fb-save-restore
  • dx/dldldld
  • fpletz/flake
  • dx/jacksense-headset-mic-only
  • release/1.3.0 protected
  • fil3s-limit-filesize
  • allow-reloading-sunmenu
  • wifi-json-error-handling
  • app_text_viewer
  • shoegaze-fps
  • media_has_video_has_audio
  • fil3s-media
  • more-accurate-battery
  • v1.4.0
  • v1.3.0
  • v1.2.0
  • v1.2.0+rc1
  • v1.1.1
  • v1.1.0
  • v1.1.0+rc1
  • v1.0.0
  • v1.0.0+rc6
  • v1.0.0+rc5
  • v1.0.0+rc4
  • v1.0.0+rc3
  • v1.0.0+rc2
  • v1.0.0+rc1
35 results

4bit.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)