Converting C++ to MicroPython for a Raspberry Pico Project

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
ChipNod2020
Posts: 1
Joined: Thu Mar 11, 2021 5:41 pm

Converting C++ to MicroPython for a Raspberry Pico Project

Post by ChipNod2020 » Thu Mar 11, 2021 7:13 pm

Greetings all,

I have a working C++ Script. I'm not 100% fluent with Python yet so I'm hoping someone could take pity on me and help.

This is the working C++ Script.

Code: Select all

// NAV - Navitgation Light Prefix
#define NAV_LED_PIN 2
// Navigation Lights
#define NAV_LED_ON 150    
#define NAV_LED_OFF 1000
unsigned long NAV_msLast;  // last time the LED changed state
boolean NAV_ledState;    // current LED state
pinMode(NAV_LED_PIN, OUTPUT);


LOOP:
// Normal Stuff
now = millis();
// Navigation Lights
if (now - NAV_msLast > (NAV_ledState ? NAV_LED_ON : NAV_LED_OFF)) {
digitalWrite(NAV_LED_PIN, NAV_ledState = !NAV_ledState);
NAV_msLast = now;
My 2 questions.
How would I initialize the boolean string for NAV_ledState?
Is the if statement syntax similar and how would one toggle the LED other than digital write?

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Converting C++ to MicroPython for a Raspberry Pico Project

Post by OlivierLenoir » Fri Mar 12, 2021 6:52 am

You can do something like this using sleep_ms().

Code: Select all

from machine import Pin
from utime import sleep_ms

NAV_LED_PIN = const(2)
NAV_LED_ON = const(150)
NAV_LED_OFF = const(1000)

led = Pin(NAV_LED_PIN, Pin.OUT)

while True:
    led(not(led()))
    if led():
        delay = NAV_LED_ON
    else:
        delay = NAV_LED_OFF
    sleep_ms(delay)

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Converting C++ to MicroPython for a Raspberry Pico Project

Post by OlivierLenoir » Fri Mar 12, 2021 7:10 am

This version is a lot closer to your C++ code and use ticks_ms() and ticks_diff().

Code: Select all

from machine import Pin
from utime import ticks_ms, ticks_diff

NAV_LED_PIN = const(2)
NAV_LED_ON = const(150)
NAV_LED_OFF = const(1000)

NAV_msLast = ticks_ms()
led = Pin(NAV_LED_PIN, Pin.OUT)

while True:
    now = ticks_ms()
    if ticks_diff(now, NAV_msLast) > (NAV_LED_ON if led() else NAV_LED_OFF):
        led(not(led()))
        NAV_msLast = now

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Converting C++ to MicroPython for a Raspberry Pico Project

Post by Roberthh » Fri Mar 12, 2021 7:12 am

There is also a supported ternary statement, which is a littel bit different to C. It would read:

Code: Select all

    delay = NAV_LED_ON if led() else NAV_LED_OFF

User avatar
OlivierLenoir
Posts: 126
Joined: Fri Dec 13, 2019 7:10 pm
Location: Picardie, FR

Re: Converting C++ to MicroPython for a Raspberry Pico Project

Post by OlivierLenoir » Fri Mar 12, 2021 8:36 am

Considering Roberthh post, we can also remove delay.

Code: Select all

from machine import Pin
from utime import sleep_ms

NAV_LED_PIN = const(2)
NAV_LED_ON = const(150)
NAV_LED_OFF = const(1000)

led = Pin(NAV_LED_PIN, Pin.OUT)

while True:
    led(not(led()))
    sleep_ms(NAV_LED_ON if led() else NAV_LED_OFF)
An other approach to use a tuple.

Code: Select all

from machine import Pin
from utime import sleep_ms

NAV_LED_PIN = const(2)
NAV_LED_OFF_ON = (1000, 150)

led = Pin(NAV_LED_PIN, Pin.OUT)

while True:
    led(not(led()))
    sleep_ms(NAV_LED_OFF_ON[led()])

Post Reply