# mPy: oscilloscope datalogger based on an array
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2026-06-11  initial release

import	os

# Implemented:
# Logger(size, fn=None)
#    Logger.__str__
#    Logger.append(value)
#    Logger.query (start=0, len=8)
#    Logger.save  ()
#    Logger.open  ()

from	array	import	array

# ======================= Logger ========================

class Logger:
    
    def __init__(self, size, fn=None):
        self.size = size
        self.fn   = fn
        self.idx  = 0
        self.data = array('B', [0]*size)
        
     
    # return stored logger data as string
    def __str__(self):
        return str(self.data)

       
    # add integer to data
    def append(self, value):
        self.data[self.idx] = value
        self.idx += 1
        
        
    # return len elements from start    
    def query(self, start=0, len=8):
        return bytes(self.data[start:start+len])
        
    
    # save logger data into file
    def save(self):
        if self.fn:
            with open(self.fn, 'wb') as f:
                f.write(self.data)
        else: print('Error, logger not associated with a file')
        
        
    # load logger data from file
    def load(self):
        if self.fn:
            with open(self.fn, 'rb') as f:
                self.data = f.read()
        else: print('Error, logger not associated with a file')
        
# ====================== test =======================

if __name__ == '__main__':
    
    db = Logger(100, 'logger.data')
    print(db)
    db.append(4)
    print(db.query(5))
    db.save()
    print()
    db1 = Logger(100, 'logger.data')
    db1.load()
    print(db1.query(0))

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





