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

Circuit Python equivalent of Arduino’s define
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Circuit Python equivalent of Arduino’s define

by Lansteys on Thu Jun 25, 2020 12:24 am

Hi All,

I’m just starting out with Circuit Python as it has some nice features I can use for a project I’m working on.
I haven’t been able to find an answer to the following though:

Is there an equivalent command in circuit python for Arduino’s define?
It would be used to add a bit more context to a variable state, without using a bunch of unnecessary characters/memory.
An example would be using the following values:
A = Active
P = Pending
C = Confirmed
The code would use the full name ‘Active’ but the value used would only be ‘A’. This would be to save memory in a data logging context.

I suspect due to the way Circuit Python is interpreted that there is no room for this, though just wanted to check.

Thanks in advance.
- Lasnteys

Lansteys
 
Posts: 4
Joined: Sun Mar 03, 2019 9:22 pm

Re: Circuit Python equivalent of Arduino’s define

by dastels on Thu Jun 25, 2020 12:07 pm

In C++ you'd probably want to use an enum or constants instead of macros.

Python doesn't have that concept of macros since there's no preprocessor stage to do the replacements.

You could use variables:
Code: Select all | TOGGLE FULL SIZE
ACTIVE = 'A'
PENDING = 'P'
CONFIRMED = 'C'

...

state = ACTIVE

...
if state is ACTIVE:
  ...


If you can use numeric states (in this example) you can use const (at least in CircuitPython 5.3):

Code: Select all | TOGGLE FULL SIZE
from micropython import const
ACTIVE = const(0)
PRNDING = const(1)
CONFIRMED = const(2)

...

state = ACTIVE

...
if state is ACTIVE:
  ...


Dave

dastels
 
Posts: 2829
Joined: Tue Oct 20, 2015 3:22 pm

Re: Circuit Python equivalent of Arduino’s define

by siddacious on Thu Jun 25, 2020 12:35 pm

Dave is right, Python doesn't have #define statements, however MicroPython and thus CircuitPython does have a const function that when wrapped around a value in the global scope which achieves a similar result

https://docs.micropython.org/en/latest/reference/asm_thumb2_hints_tips.html#named-constants

Here's an edited example from an upcoming library:
Code: Select all | TOGGLE FULL SIZE
from struct import unpack_from
from micropython import const
import adafruit_bus_device.i2c_device as i2c_device

_PCF8591_DEFAULT_ADDR = const(0x48)  # PCF8591 Default Address
_PCF8591_ENABLE_DAC = const(0x40)  # control bit for having the DAC active

# Pin constants
A0 = const(0)
A1 = const(1)
A2 = const(2)
A3 = const(3)

OUT = const(0)

class PCF8591:
    """Driver for the PCF8591 DAC & ADC Combo breakout.

    :param ~busio.I2C i2c_bus: The I2C bus the PCF8591 is connected to.
    :param address: The I2C device address for the sensor. Default is ``0x28``.

    """

    def __init__(self, i2c_bus, address=_PCF8591_DEFAULT_ADDR, reference_voltage=3.3):
        self.i2c_device = i2c_device.I2CDevice(i2c_bus, address)
        self._dacval = 0
        self._dac_enabled = False

....

siddacious
 
Posts: 271
Joined: Fri Apr 21, 2017 3:09 pm

Re: Circuit Python equivalent of Arduino’s define

by Lansteys on Thu Jun 25, 2020 7:16 pm

Thanks Dave and Siddacios,

As the goal is to save memory and have easily understandable code, the const(0) approach looks good.
I now have a key work to search, thanks!

While your here... Is there a ‘switch case’ equivalent in circuit python, or do I just need a lot of if’s?


Thanks again!

Lansteys
 
Posts: 4
Joined: Sun Mar 03, 2019 9:22 pm

Re: Circuit Python equivalent of Arduino’s define

by tannewt on Fri Jun 26, 2020 1:12 pm

Lansteys wrote:As the goal is to save memory and have easily understandable code, the const(0) approach looks good.
I now have a key work to search, thanks!

Note that the name of the variable must start with `_` in order for the const to work as well.

Lansteys wrote:While your here... Is there a ‘switch case’ equivalent in circuit python, or do I just need a lot of if’s?


No, it doesn't. Lots of if/elifs is usually the way to go. If you want to get fancy, you can have a dictionary with functions as values to call.

tannewt
 
Posts: 1685
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.