# mPy: oscilloscope datalogger based on a list
# 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  ()

import	json

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

class Logger:
    
    def __init__(self, size, fn=None):
        self.size = size
        self.fn   = fn
        self.idx  = 0
        self.data = [None]*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 self.data[start:start+len]
        
    
    # save logger data into file
    def save(self):
        if self.fn:
            with open(self.fn, 'w') as f:
                json.dump(self.data, f)
        else: print('Error, logger not associated with a file')
        
        
    # load logger data from file
    def load(self):
        if self.fn:
            with open(self.fn, 'r') as f:
                self.data = json.load(f)
        else: print('Error, logger not associated with a file')
        
# ====================== test =======================

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

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



