Code not working when 2 functions are calle one after the other

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
zoner
Posts: 6
Joined: Tue Jan 07, 2020 12:04 am

Code not working when 2 functions are calle one after the other

Post by zoner » Tue Jan 07, 2020 12:09 am

I have a weird bug,

The code works fine for either of the functions defined but not when I use both ... han ?

here is the culprit code :

Code: Select all

# main.py -- put your code here!
import pyb
import os
import utime
 
_LOGFILE = "pyb_LOG2.txt"

RED = 1
GREEN = 2
YELLOW = 3
BLUE = 4

def log_this(that):
	LOG = open(_LOGFILE, 'a') # as LOG:
	LOG.write(that)	
	pyb.delay(100)
	LOG.close()

def flash_LED(which, duration):
	led = pyb.LED(which)
	led.on()
	pyb.delay(duration)
	led.off()

flash_LED(YELLOW, 1000)
log_this("BootUP")
flash_LED(BLUE, 1000)
log_this("Done")
flash_LED(RED, 1000)
log_this("Real Done")
flash_LED(GREEN, 1000)
log_this("Yeah !")
flash_LED(YELLOW, 1000)
When I comment out one of the function call ... say flash_LED ... the logging works fine ...
When I comment out the other one ... log_this ... the LED flash fine ...

But this code as is won't ... why ?????

User avatar
jimmo
Posts: 1021
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Code not working when 2 functions are calle one after the other

Post by jimmo » Tue Jan 07, 2020 4:30 am

Hi,

I tried running this code on a Pyboard v1.1 and it mostly works exactly as expected, other than the red light also flashes when the filesystem writes are in progress.

So I see this sequence: yellow, red, blue, red, red, red, green, red, yellow. Is that what you see?

If not, which pyboard and micropython version are you using?

Unfortunately there's not much you can do about the red LED flashing during the filesystem writes -- this is a built-in feature of the pyboard to use the red LED to indicate whether there are pending flash writes.

zoner
Posts: 6
Joined: Tue Jan 07, 2020 12:04 am

Re: Code not working when 2 functions are calle one after the other

Post by zoner » Tue Jan 07, 2020 4:54 am

The problem is not the flashing leds ... they always work as programmed ... starts with yellow, ends with yellow, yeah a lot of red since read happens when writing ... but it writes nothing ... PyBv1.1 ... if I remove the code for the flashing leds, it writes ok but not if I do writing and flashing ... flashing always works but not the writing ... it writes nothing when I also flash the leds ... which does not make sense

User avatar
jimmo
Posts: 1021
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia

Re: Code not working when 2 functions are calle one after the other

Post by jimmo » Tue Jan 07, 2020 5:28 am

How are you verifying that the file is written (or not)?

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

Re: Code not working when 2 functions are calle one after the other

Post by pythoncoder » Tue Jan 07, 2020 8:33 am

It works fine here on a Pyboard 1.x. I think you are probably experiencing the curse of MSC mode. The USB standard's MSC mode assumes that devices which expose filesystems are dumb, like USB sticks. The Pyboard's MSC mode violates this because the Pyboard can alter the filesystem independently of the host PC. This leads to all sorts of problems.

I always disable MSC mode in boot.py and use rshell to access the filesystem.
Peter Hinch

zoner
Posts: 6
Joined: Tue Jan 07, 2020 12:04 am

Re: Code not working when 2 functions are calle one after the other

Post by zoner » Tue Jan 07, 2020 2:41 pm

jimmo wrote:
Tue Jan 07, 2020 5:28 am
How are you verifying that the file is written (or not)?
The pyboard is connected to my windows through USB ... I can see the files on there ... the file name is pyb_LOG.txt (now with a 2 at the end lol) ... but the next comment seems to be the culprit ...

zoner
Posts: 6
Joined: Tue Jan 07, 2020 12:04 am

Re: Code not working when 2 functions are calle one after the other

Post by zoner » Tue Jan 07, 2020 2:44 pm

pythoncoder wrote:
Tue Jan 07, 2020 8:33 am
I think you are probably experiencing the curse of MSC mode.
...
I always disable MSC mode in boot.py and use rshell to access the filesystem.
I will have to try to run it without the USB connection, run it, then check the file ... I would be great since I plan to add Ethernet over it and send the events though tcp/ip to a central computer ... so no USB usage while runnig !

Thanks for the heads up on the MSC mode lol

zoner
Posts: 6
Joined: Tue Jan 07, 2020 12:04 am

Re: Code not working when 2 functions are calle one after the other

Post by zoner » Tue Jan 07, 2020 2:49 pm

Problem solved ! the USB was the culprit !

THANKS A MILLION !

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

Re: Code not working when 2 functions are calle one after the other

Post by pythoncoder » Tue Jan 07, 2020 6:18 pm

MSC mode is more trouble than it's worth (IMO). Glad it's fixed :D
Peter Hinch

zoner
Posts: 6
Joined: Tue Jan 07, 2020 12:04 am

Re: Code not working when 2 functions are calle one after the other

Post by zoner » Tue Jan 07, 2020 6:40 pm

It's been a while since I programmed ... 15 years or so lol ... still I did some Lua for gadgets in Aspire (for cnc cuts and all) ... but I should have thought that connecting a computer to the usb would make avoc when interacting with the file system ... so thanks for waking me up lol

I also read about the difference between python and micropython ... usefull to know ;)

Next is integrating ethernet to it ... to have an intelligent ethernet button ;) Working on an architecture and an xml language (for the main computer) to describe needed sequence of events ... was sorry to find that micropython does not have xml support ... I'll have to build a json extract to have the pyboard read it on boot up ... defining the ports and all ...

So again, thanks ! I was quite discouraged lol Now I understand ! I feel much better now (like the judge's father on Night's Court) ;)

Post Reply