I know nothing of Python and have been going in circles trying to read up on where to start with this little project.
Overview of what I want to do/learn:
- How can I read a binary file into CircuitPython and write to fram?
I'm trying to read out a range of bytes into binary files and write other files into those byte ranges within the fram.
- Understand how to read a file into Python
Understand how to write binary data out to a file
Can I write a binary file directly to fram? akin using dd?
Understand how to read from & write to fram
Understand what formatting is happening with the print statements
As a first step/test I followed the example from Adafruit and just tweaked it to write a range of bytes into an array and from that array into fram, then to write directly to fram to verify my understanding of those functions.
Code: Select all
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
import board
import busio
import adafruit_fram
i2c = busio.I2C(board.SCL, board.SDA)
fram = adafruit_fram.FRAM_I2C(i2c)
## Method 1 - Assigning values this way results in a readable print
FileBytes1=bytearray(10)
FileBytes1[0] = 0x31
FileBytes1[1] = 0x32
FileBytes1[2] = 0x33
FileBytes1[3] = 0x34
FileBytes1[4] = 0x35
FileBytes1[5] = 0x36
FileBytes1[6] = 0x37
FileBytes1[7] = 0x38
FileBytes1[8] = 0x39
FileBytes1[9] = 0x41
## Method 2 - Assigning values this way results in a different printed format
FileBytes2 = [0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41]
fram[0:9] = FileBytes1
fram[10:19] = FileBytes2
## Setting fram values directly prints the same
fram[20] = 0x31
fram[21] = 0x32
fram[22] = 0x33
fram[23] = 0x34
fram[24] = 0x35
fram[25] = 0x36
fram[26] = 0x37
fram[27] = 0x38
fram[28] = 0x39
fram[29] = 0x41
fram[30:39] = [0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x41]
## taken from example to fill the range with random values. Range makes sense, but list just pulls sequential numbers?
## from where and why do they print differently if I print the variable or the Fram after it's copied?
listvalues = list(range(20))
fram[40:59]=listvalues
print("")
print(FileBytes1)
print("Method 1 Var printed")
print("")
print(fram[0:9])
print("Method 1 Var to Fram Printed")
print("")
print(FileBytes2)
print("Method 2 Var printed")
print("")
print(fram[10:19])
print("Method 2 Var to Fram Printed")
print("")
print(fram[20:29])
print("Direct to Fram 1 Printed")
print("")
print(fram[30:39])
print("Direct to Fram 2 Printed")
print("")
print(listvalues)
print("listvalue Var Printed")
print("")
print(fram[40:59])
print("random list in Fram Printed")
Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
bytearray(b'123456789A')
Method 1 Var printed
bytearray(b'123456789')
Method 1 Var to Fram Printed
[49, 50, 51, 52, 53, 54, 55, 56, 57, 65]
Method 2 Var printed
bytearray(b'123456789')
Method 2 Var to Fram Printed
bytearray(b'123456789')
Direct to Fram 1 Printed
bytearray(b'123456789')
Direct to Fram 2 Printed
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
listvalue Var Printed
bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f\x10\x11\x12')
random list in Fram Printed
Code done running.