# mPy - FSK leds64 base oscilloscope (0..+8) commanded by potentiometer
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2026-05-21  initial release
# rev 0.11  2026-06-18  Potm8.get() renamed to Potm8.get8() to maintan access tp super().get()

from	drivers	import	Potm, Leds64

# Implemented:
# Potm8()
#    Potm8.get()				-->  int
# Oscilloscope64()
#    Oscilloscope64.add(y)

# ===================== Potm8 =======================

# Provides potmeter position in range 0..8
class Potm8(Potm):
    
    def __init__(self):
        super().__init__()		# IO26 = POT
        
        
    # get potmeter resolution in range 0..8
    def get8(self):
         return int(super().get() * 2.7)
   
# =================== Oscilloscope64 ================

# provide oscilloscope image from .add(y) signal
class Oscilloscope64(Leds64):
    
    def __init__(self):
        super().__init__()		# IO15=DIN, IO2 = CS, IO0 = CLK
        self.bars  = {0: 0x00,
                      1: 0x01, 2: 0x03, 3: 0x07, 4: 0x0F,
                      5: 0x1F, 6: 0x3F, 7: 0x7F, 8: 0xFF}
        self.chart = bytearray([0x00]*8)
        self.on()
        self.write(self.chart)
        
        
    # shift chart to left and add y (0..+8) to the right
    def add(self, y):
        y = min(max(y, 0), 8)
        bar = self.bars[y]
        self.chart = bytearray([bar]) + self.chart[:-1]
        self.write(self.chart)
        
# ===================== tests =======================
    
if __name__ == '__main__':
    
    import	time
    
    # print potmeter output @ 500 mSec
    def test1():
        potm = Potm8()
        while True:
            print(potm.get())
            time.sleep_ms(500)
            
            
    # show triangle wave ascilloscope signal on leds64
    def test2():
        osc = Oscilloscope64()
        while True:
            for y in range(8):
                osc.add(y)
                time.sleep_ms(500)
            for y in range(8, 0, -1):
                osc.add(y)
                time.sleep_ms(500)


    # show potmeter signal as ascilloscope image on leds64
    def test3():
        potm = Potm8()
        osc  = Oscilloscope64()
        while True:
            osc.add(potm.get())
            time.sleep_ms(250)


    test3()

# ======================= end ==========================
