Skip to content

Commit

Permalink
MultiBitFlag -> Counter, ByteArray stub/mock
Browse files Browse the repository at this point in the history
  • Loading branch information
nateinaction committed Jan 12, 2025
1 parent 2ebc7d8 commit 9ba8b9f
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 269 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"tests/unit"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"makefile.configureOnOpen": false
}
42 changes: 0 additions & 42 deletions lib/pysquared/bitflags/bitflags.py

This file was deleted.

38 changes: 0 additions & 38 deletions lib/pysquared/bitflags/multi_bit_flag.py

This file was deleted.

6 changes: 3 additions & 3 deletions lib/pysquared/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def beacon(self) -> None:
lora_beacon: str = (
f"{self.callsign} Hello I am {self.cubesatName}! I am: "
+ str(self.cubesat.power_mode)
+ f" UT:{self.cubesat.uptime} BN:{self.cubesat.c_boot} EC:{self.cubesat.c_error_count} "
+ f" UT:{self.cubesat.uptime} BN:{self.cubesat.boot_count.get()} EC:{self.cubesat.error_count.get()} "
+ f"IHBPFJASTMNE! {self.callsign}"
)
except Exception as e:
Expand Down Expand Up @@ -192,12 +192,12 @@ def state_of_health(self) -> None:
f"ID:{self.cubesat.current_draw}",
f"IC:{self.cubesat.charge_current}",
f"UT:{self.cubesat.uptime}",
f"BN:{self.cubesat.c_boot}",
f"BN:{self.cubesat.boot_count.get()}",
f"MT:{self.cubesat.micro.cpu.temperature}",
f"RT:{self.last_radio_temp()}",
f"AT:{self.cubesat.internal_temperature}",
f"BT:{self.last_battery_temp}",
f"EC:{self.cubesat.c_error_count}",
f"EC:{self.cubesat.error_count.get()}",
f"AB:{int(self.cubesat.burned)}",
f"BO:{int(self.cubesat.f_brownout)}",
f"FK:{int(self.cubesat.f_fsk)}",
Expand Down
20 changes: 0 additions & 20 deletions lib/pysquared/hardware/nvm.py

This file was deleted.

3 changes: 3 additions & 0 deletions lib/pysquared/nvm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
The NVM package is a collection of functionality that interacts with non-volatile memory
"""
21 changes: 21 additions & 0 deletions lib/pysquared/nvm/bitflags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class bitFlag:
"""
Single bit register WITHIN a byte that can be read or set
values are 'bool'
Assumes register is read MSB!
"""

def __init__(self, register, bit):
self.bit_mask = 1 << (bit % 8) # the bitmask *within* the byte!
self.byte = register

def __get__(self, obj, objtype=None):
return bool(obj.micro.nvm[self.byte] & self.bit_mask)

def __set__(self, obj, value):
if value:
obj.micro.nvm[self.byte] |= self.bit_mask
else:
obj.micro.nvm[self.byte] &= ~self.bit_mask
27 changes: 27 additions & 0 deletions lib/pysquared/nvm/counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
try:
from stubs.circuitpython.byte_array import ByteArray
except ImportError:
pass


class Counter:
def __init__(
self,
index: int,
datastore: ByteArray,
) -> None:
self._index = index
self._datastore = datastore

def get(self) -> int:
"""
get returns the value of the counter
"""
return self._datastore[self._index]

def increment(self) -> None:
"""
increment increases the counter by one
"""
value: int = (self.get() + 1) & 0xFF # 8-bit counter with rollover
self._datastore[self._index] = value
70 changes: 5 additions & 65 deletions lib/pysquared/pysquared.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
import lib.pysquared.rv3028 as rv3028 # Real Time Clock
from lib.adafruit_lsm6ds.lsm6dsox import LSM6DSOX # IMU
from lib.adafruit_rfm import rfm9x, rfm9xfsk # Radio
from lib.pysquared.bitflags.bitflags import bitFlag
from lib.pysquared.bitflags.multi_bit_flag import multiBitFlag
from lib.pysquared.debugcolor import co
from lib.pysquared.hardware import nvm
from lib.pysquared.nvm.bitflags import bitFlag
from lib.pysquared.nvm.counter import Counter

try:
from typing import Any, OrderedDict, TextIO, Union
Expand All @@ -45,11 +44,7 @@

# NVM register numbers
_BOOTCNT = const(0)
_VBUSRST = const(6)
_ERRORCNT = const(7)
_TOUTS = const(9)
_ICHRG = const(11)
_DIST = const(13)
_FLAG = const(16)

SEND_BUFF: bytearray = bytearray(252)
Expand All @@ -61,54 +56,8 @@ class Satellite:
"""

# General NVM counters
c_boot: multiBitFlag = multiBitFlag(
index=_BOOTCNT,
bit_length=8,
nvm_reader=nvm.reader,
nvm_writer=nvm.writer,
)
c_vbusrst: multiBitFlag = multiBitFlag(
index=_VBUSRST,
bit_length=8,
nvm_reader=nvm.reader,
nvm_writer=nvm.writer,
)
c_error_count: multiBitFlag = multiBitFlag(
index=_ERRORCNT,
bit_length=8,
nvm_reader=nvm.reader,
nvm_writer=nvm.writer,
)
c_distance: multiBitFlag = multiBitFlag(
index=_DIST,
bit_length=8,
nvm_reader=nvm.reader,
nvm_writer=nvm.writer,
)
c_ichrg: multiBitFlag = multiBitFlag(
index=_ICHRG,
bit_length=8,
nvm_reader=nvm.reader,
nvm_writer=nvm.writer,
)
c_error_count: multiBitFlag = multiBitFlag(
index=_ERRORCNT,
bit_length=8,
nvm_reader=nvm.reader,
nvm_writer=nvm.writer,
)
c_distance: multiBitFlag = multiBitFlag(
index=_DIST,
bit_length=8,
nvm_reader=nvm.reader,
nvm_writer=nvm.writer,
)
c_ichrg: multiBitFlag = multiBitFlag(
index=_ICHRG,
bit_length=8,
nvm_reader=nvm.reader,
nvm_writer=nvm.writer,
)
boot_count: Counter = Counter(index=_BOOTCNT, datastore=microcontroller.nvm)
error_count: Counter = Counter(index=_ERRORCNT, datastore=microcontroller.nvm)

# Define NVM flags
f_softboot: bitFlag = bitFlag(register=_FLAG, bit=0)
Expand All @@ -128,9 +77,7 @@ def debug_print(self, statement: Any) -> None:
print(co("[pysquared]" + str(statement), "green", "bold"))

def error_print(self, statement: Any) -> None:
self.c_error_count: multiBitFlag = (
self.c_error_count + 1
) & 0xFF # Limited to 255 errors
self.error_count.increment()
if self.debug:
print(co("[pysquared]" + str(statement), "red", "bold"))

Expand Down Expand Up @@ -203,7 +150,6 @@ def __init__(self) -> None:
"""
Define the boot time and current time
"""
self.c_boot += 1
self.BOOTTIME: int = 1577836800
self.debug_print(f"Boot time: {self.BOOTTIME}s")
self.CURRENTTIME: int = self.BOOTTIME
Expand Down Expand Up @@ -241,12 +187,6 @@ def __init__(self) -> None:
]
)

"""
NVM Parameter Resets
"""
if self.c_boot > 200:
self.c_boot = 0

if self.f_softboot:
self.f_softboot = False

Expand Down
8 changes: 6 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
import time

import microcontroller
from micropython import const

import lib.pysquared.pysquared as pysquared

_BOOTCNT = const(0)
_ERRORCNT = const(7)

print("=" * 70)
print("Hello World!")
print("PySquared FC Board Circuit Python Software Version: 2.0.0")
Expand Down Expand Up @@ -54,8 +58,8 @@ def initial_boot():
# c.watchdog_pet()

try:
c.c_boot += 1 # Increment boot number
debug_print("Boot number: " + str(c.c_boot))
c.boot_count.increment()
debug_print("Boot number: " + str(c.boot_count.get()))
debug_print(str(gc.mem_free()) + " Bytes remaining")

initial_boot()
Expand Down
24 changes: 24 additions & 0 deletions mocks/circuitpython/byte_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from typing import Union

from circuitpython_typing import ReadableBuffer

from stubs.circuitpython.byte_array import ByteArray as ByteArrayStub


class ByteArray(ByteArrayStub):
"""
ByteArray is a class that mocks the implementaion of the CircuitPython non-volatile memory API.
"""

def __init__(self, size: int = 1024) -> None:
self.memory = bytearray(size)

def __getitem__(self, index: Union[slice, int]) -> Union[bytearray, int]:
if isinstance(index, slice):
return bytearray(self.memory[index])
return int(self.memory[index])

def __setitem__(
self, index: Union[slice, int], value: Union[ReadableBuffer, int]
) -> None:
self.memory[index] = value
45 changes: 45 additions & 0 deletions stubs/circuitpython/byte_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""
Stub for Circuit Python ByteArray class
https://docs.circuitpython.org/en/stable/shared-bindings/nvm/index.html#nvm.ByteArray
This stub has been contributed to the Adafruit CircuitPython Typing repo and can be removed after it has been approved and merged:
https://github.com/adafruit/Adafruit_CircuitPython_Typing/pull/46/files
"""

from typing import Union, overload

from circuitpython_typing import ReadableBuffer
from typing_extensions import Protocol


class ByteArray(Protocol):
"""
Presents a stretch of non-volatile memory as a bytearray.
Non-volatile memory is available as a byte array that persists over reloads and power cycles. Each assignment causes an erase and write cycle so its recommended to assign all values to change at once.
"""

def __bool__(self) -> bool: ...

def __len__(self) -> int:
"""Return the length. This is used by (len)"""

@overload
def __getitem__(self, index: slice) -> bytearray: ...

@overload
def __getitem__(self, index: int) -> int: ...

def __getitem__(self, index: Union[slice, int]) -> Union[bytearray, int]:
"""Returns the value at the given index."""

@overload
def __setitem__(self, index: slice, value: ReadableBuffer) -> None: ...

@overload
def __setitem__(self, index: int, value: int) -> None: ...

def __setitem__(
self, index: Union[slice, int], value: Union[ReadableBuffer, int]
) -> None:
"""Set the value at the given index."""
Loading

0 comments on commit 9ba8b9f

Please sign in to comment.