-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MultiBitFlag -> Counter, ByteArray stub/mock
- Loading branch information
1 parent
2ebc7d8
commit 9ba8b9f
Showing
14 changed files
with
169 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.""" |
Oops, something went wrong.