Skip to content
Snippets Groups Projects
Commit e8598bfd authored by Andreas Gattringer's avatar Andreas Gattringer
Browse files

formatted code with black

parent 37e13603
No related branches found
No related tags found
No related merge requests found
import sys import sys
if sys.implementation.name == "micropython": if sys.implementation.name == "micropython":
# noinspection PyUnresolvedReferences # noinspection PyUnresolvedReferences
from machine import I2C from machine import I2C
else: else:
from umnp.microcontroller.umock.machine import I2C from umnp.microcontroller.umock.machine import I2C
I2C_ADDRESS_EEPROM24LC32A = 0x50 I2C_ADDRESS_EEPROM24LC32A = 0x50
...@@ -25,12 +25,13 @@ class EEPROM24LC32A: ...@@ -25,12 +25,13 @@ class EEPROM24LC32A:
def __calculate_address(self, block_idx, offset=0): def __calculate_address(self, block_idx, offset=0):
address = block_idx * self.__block_size + offset address = block_idx * self.__block_size + offset
if address > self.__max_size or address < 0: if address > self.__max_size or address < 0:
raise RuntimeError(f"Invalid address {address}, out of range [0, {self.__max_size}]") raise RuntimeError(
f"Invalid address {address}, out of range [0, {self.__max_size}]"
)
result = bytearray(2) result = bytearray(2)
result[0] = address >> 8 result[0] = address >> 8
result[1] = address & 0xFF result[1] = address & 0xFF
def __set_read_address(self, address): def __set_read_address(self, address):
self.__i2c.writeto(self.__i2c_address, address) self.__i2c.writeto(self.__i2c_address, address)
...@@ -66,7 +67,7 @@ class EEPROM24LC32A: ...@@ -66,7 +67,7 @@ class EEPROM24LC32A:
buf[0:bytes_to_read] = self.__i2c.readfrom(self.__i2c_address, bytes_to_read) buf[0:bytes_to_read] = self.__i2c.readfrom(self.__i2c_address, bytes_to_read)
def __write_page(self, address, data): def __write_page(self, address, data):
self.__i2c.writevto(self.__i2c_address, (address, data)) self.__i2c.writevto(self.__i2c_address, (address, data))
def writeblocks(self, block_num: int, buf: bytearray, offset=0) -> None: def writeblocks(self, block_num: int, buf: bytearray, offset=0) -> None:
""" """
...@@ -90,7 +91,7 @@ class EEPROM24LC32A: ...@@ -90,7 +91,7 @@ class EEPROM24LC32A:
* page writes: write 32 bytes at once at a random address, but wrap around at the page boundary, * page writes: write 32 bytes at once at a random address, but wrap around at the page boundary,
potentially overwriting data - so we need to ensure that we start a page write at a page boundary so we potentially overwriting data - so we need to ensure that we start a page write at a page boundary so we
don't cross page boundaries don't cross page boundaries
""" """
bytes_to_write = len(buf) bytes_to_write = len(buf)
...@@ -110,10 +111,12 @@ class EEPROM24LC32A: ...@@ -110,10 +111,12 @@ class EEPROM24LC32A:
else: else:
block_count, remainder = divmod(bytes_to_write, self.__block_size) block_count, remainder = divmod(bytes_to_write, self.__block_size)
if remainder != self.__block_size - offset: if remainder != self.__block_size - offset:
raise RuntimeError("Buffer length not a multiple of the block size + offset") raise RuntimeError(
"Buffer length not a multiple of the block size + offset"
)
address = self.__calculate_address(block_num, offset) address = self.__calculate_address(block_num, offset)
self.__write_page(address, buf[:self.__block_size - offset]) self.__write_page(address, buf[: self.__block_size - offset])
for i in range(block_count): for i in range(block_count):
address = self.__calculate_address(block_num + i + 1, 0) address = self.__calculate_address(block_num + i + 1, 0)
......
...@@ -20,8 +20,18 @@ class RTC: ...@@ -20,8 +20,18 @@ class RTC:
print("RTC.datetime() setting not implemented") print("RTC.datetime() setting not implemented")
return return
now = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(seconds=self._time_offset) now = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(
return now.year, now.month, now.day, now.hour, now.minute, now.second, now.microsecond / 1000 / 1000 seconds=self._time_offset
)
return (
now.year,
now.month,
now.day,
now.hour,
now.minute,
now.second,
now.microsecond / 1000 / 1000,
)
class SPI: class SPI:
...@@ -55,11 +65,22 @@ class I2C: ...@@ -55,11 +65,22 @@ class I2C:
def writeto(self, addr: int, buf: bytearray, sopt=True): def writeto(self, addr: int, buf: bytearray, sopt=True):
return 1 return 1
def readfrom_info(self, addr: int, buf, stop=True, ): def readfrom_info(
self,
addr: int,
buf,
stop=True,
):
return None return None
def readfrom(self, addr: int, nbytes, stop=True) -> bytes: def readfrom(self, addr: int, nbytes, stop=True) -> bytes:
return b'' return b""
def writevto(self,add: int, vector, stop=True): def writevto(self, add: int, vector, stop=True):
return 1 return 1
\ No newline at end of file
def readfrom_mem(self, i2c_address, address, n_bytes):
return 1
def writeto_mem(self, i2c_address, address, buffer):
pass
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment