tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
15.32k stars 905 forks source link

Problems debugging and a proposal. #242

Closed DazWilkin closed 5 years ago

DazWilkin commented 5 years ago

Problem

I don't understand why this simple example does not work correctly: https://gist.github.com/DazWilkin/7c6daa9c6048d8b5761831a7420f603c

enumerate correctly enumerates each LED in turn so I know I'm deploying code and that all the LEDs work. However, display incorrectly repeatedly flashes (every second, OK) the LED that corresponds to machine.setLEDMatric(4,4); I intend display to render the provided number using the LEDs as its binary digits.

I tried replacing the binary operators (&1 and >>1) with their equivalents (%2 and /2) but it makes no difference. So, then feeling crazy, I repro'd the code in The Go Playground and it works as expected: https://play.golang.org/p/jBSgaIlNK1R

Debugging

Is there an obvious way to debug tinygo code? There's no local debugger (see below) and a limited set of outputs on a device for logging. Are there obvious best practices for this that elude me?

Proposal

Has any thought been given to a virtual device and possible an emulator that would run on the host machine? Simplistically, if I could replace --target=microbit with --target=vdevice and then run the resulting code locally -- like with The Go Playground 'equivalent` -- I could better test code before deploying it.

I appreciate implementing the machine package using fmt.PrintX commands is likely entirely insufficient but it's better than nothing. A possibly (!?) reasonably simple but more compelling solution would be to implement the vdevice as an HTTP server that would render a basic (exemplar) device, could provide a more elegant manifestation of LEDs and could implement buttons.

DazWilkin commented 5 years ago

I just discovered: https://github.com/tinygo-org/tinygo/blob/5569cd1b6b3aff8f52052d5ff1028d9bb87fba24/src/machine/board_microbit.go#L113

Explaining why my code isn't working as intended.

aykevl commented 5 years ago

So these are really two issues:

DazWilkin commented 5 years ago

Thanks the reply!

The issues are correlated because when my code didn't work, I was at a loss as to how to debug.

conejoninja commented 5 years ago

The led matrix in the micro:bit is multiplexed, that means that the leds can not be set on/off permanently, they need to be switched on/off continuously (and let POV do their magic). In a multiplexed array, you are not enabling an individual led, you are enabling the whole row or column and the led where they cross will light up. This will led to undesired results if you try to light up two or more leds. Here are some more information on the topic : https://en.wikipedia.org/wiki/Charlieplexing

For the micro:bit matrix, I have this example : https://github.com/conejoninja/tinygoexamples/tree/master/microbit/scrolltext that will enable several leds at a time. I had plans to make a driver out of it, but didn't find the time yet to do it.

DazWilkin commented 5 years ago

This is very helpful, thank you.

I rewrote my sample app in C using the mbed tooling and was going to try to better understand that implementation.

I'll have a look at your code.

deadprogram commented 5 years ago

The core of the TinyGo machine package has changed, and so now more closely resembles some of the architectural features discussed in this issue. See https://github.com/tinygo-org/tinygo/pull/426 for more on this.

As such, I am going to close this, please re-open, or open new issues, as needed.

Thank you everyone!