Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Writing CircuitPython for different boards with different fe
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Writing CircuitPython for different boards with different fe

by kevinjwalters on Sun Apr 26, 2020 6:33 pm

Are there any recommended approaches for writing CircuitPython that can either detect the board or detect the features present? For one example, a program might be able to run in slightly different ways on:

  • a CLUE,
  • a CPB (no gizmo),
  • a CPB with gizmo or
  • a CPX.

Sometimes it's nice to be able to detect if a feature like the screen is present and then use it.. But the code might also want to pick usable pads and that's going to be very tailored for each supporterd board and affected by add-ons like a gizmo.

The CPX is the harder case there because the code may also need to be a lot smaller when it runs there.

kevinjwalters
 
Posts: 635
Joined: Sun Oct 01, 2017 3:15 pm

Re: Writing CircuitPython for different boards with differen

by danhalbert on Sun Apr 26, 2020 9:17 pm

There are two ways to detect the board: `os.uname()`: see the `machine` field, and `sys.platform` (just tells you the chip: nRF52840, etc.).

The CircuitPlayground library uses `sys.platform`, and is going to switch to `os.uname()`.

I'm not sure how you'd detect a gizmo or not; if you can get something back via SPI then you could check. Or you could try reading some of the connected pins and see if they are pulled-up or down when connected but not if not connected.

danhalbert
 
Posts: 2055
Joined: Tue Aug 08, 2017 12:37 pm

Re: Writing CircuitPython for different boards with differen

by kevinjwalters on Fri May 01, 2020 10:58 am

Thanks, I'll use os.uname() for now. Importing the library and hoping the user doesn't install the gizmo library on a CLUE is another option but probably a bad one.

The return object from os.uname() look a bit special. It appears like namedtuple which on "big" python get their own type but class just has it down as a regular tuple?

Code: Select all | TOGGLE FULL SIZE
Adafruit CircuitPython 5.2.0 on 2020-04-09; Adafruit Circuit Playground Bluefruit with nRF52840
>>> import os
>>> something = os.uname()
>>> type(something)
<class 'tuple'>
>>> something
(sysname='nrf52', nodename='nrf52', release='5.2.0', version='5.2.0 on 2020-04-09', machine='Adafruit Circuit Playground Bluefruit with nRF52840')
>>> something.machine
'Adafruit Circuit Playground Bluefruit with nRF52840'
>>> something[4]
'Adafruit Circuit Playground Bluefruit with nRF52840'


If it was a normal tuple these two methods would work:

Code: Select all | TOGGLE FULL SIZE
>>> something.index('5.2.0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'index'
>>> something.count('5.2.0')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'tuple' object has no attribute 'count'

kevinjwalters
 
Posts: 635
Joined: Sun Oct 01, 2017 3:15 pm

Re: Writing CircuitPython for different boards with differen

by danhalbert on Fri May 01, 2020 12:35 pm

The return object from os.uname() look a bit special. It appears like namedtuple which on "big" python get their own type but class just has it down as a regular tuple?

It is weird. It's not a namedtuple in CPython either, it's its own class.

danhalbert
 
Posts: 2055
Joined: Tue Aug 08, 2017 12:37 pm

Please be positive and constructive with your questions and comments.