Some basic WiPy starter questions

Questions and discussion about The WiPy 1.0 board and CC3200 boards.
Target audience: Users with a WiPy 1.0 or CC3200 board.
Post Reply
relentless
Posts: 2
Joined: Mon May 02, 2016 9:57 pm

Some basic WiPy starter questions

Post by relentless » Mon May 02, 2016 10:04 pm

Hi,
I just received my WiPy (with Expansion board) which I wanted to play with in preparation for the LoPy.

I am running some very basic code from the examples that I am hoping you will be able to answer a few questions. The aim of this code was to test the SLEEP functionality as I am hoping that these boards can be used for ultra-low power periodic sensor logging.

[code]
import machine
from machine import RTC
from machine import Pin
import utime

rtc = machine.RTC() # init with default time and date

# GPIO16 is User LED
# GPIO17 is User Button
user_led = Pin('GP16', mode=Pin.OUT)
user_button = Pin('GP17', mode=Pin.IN)

# this handler gets called on wake and allows the main loop to pass the rtc_wake if
def alarm_handler (rtc_o):
# why does this global have to be defined in the alarm_handler?
# it doesnt work if defined as a true global?
global rtc_wake
rtc_wake = True

# set initial value
rtc_wake = True

while True:
# create a RTC alarm that expires after 5 seconds
rtc.alarm(time=5000, repeat=False)
# enable RTC interrupts
rtc_i = rtc.irq(trigger=RTC.ALARM0, handler=alarm_handler, wake=machine.SLEEP)
# go to sleep
user_led.value(1)
print("machine.sleep()")
machine.sleep()
if rtc_wake:
print("machine.irq()")

rtc_wake = False
# why does value=0 equal to ON instead of 1?
user_led.value(0)
utime.sleep_ms(500)
[/code]

My questions are:
1. Why is the User LED (from the Expansion board on GPIO16) turning ON with a value of 0 and OFF with a value of 1?
2. Why does the global variable have to be set in the alarm_handler function? is there something special about how these handler functions are called where they have a separate memory space or something?

Thanks

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Some basic WiPy starter questions

Post by pythoncoder » Tue May 03, 2016 5:45 am

Regarding question 2 I think you may not understand Python globals. Consider the following code:

Code: Select all

a = 11
def foo():
    a = 1
If you run foo() you will find that the value of a is still 11. This is because foo() assumes you are creating a local variable a and sets it accordingly, leaving the global a set to 11.

The global keyword is used in the body of a function to tell the function to look for a global variable a. So the correct way to code the above is:

Code: Select all

a = 11
def foo():
    global a
    a = 1
Peter Hinch
Index to my micropython libraries.

relentless
Posts: 2
Joined: Mon May 02, 2016 9:57 pm

Re: Some basic WiPy starter questions

Post by relentless » Tue May 03, 2016 7:49 am

Hi pythoncoder,
I had a read about python global scope as you suggest and i get it now... they behave differently to how most languages I have played with work.

Thanks for helping the stupid question ;)

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Some basic WiPy starter questions

Post by pythoncoder » Tue May 03, 2016 4:27 pm

Nothing stupid about it ;) Python is counterintuitive in this regard and it catches out most beginners. Note that if the function needed only to read the global variable, the global declaration would be unnecessary. This is because Python searches the local scope first, then works outwards through containing scopes until it reaches the global scope. In my view it's clearest always to use an explicit declaration.
Peter Hinch
Index to my micropython libraries.

Post Reply