As per previous posts here, Blockly style programming has been configured to simplify remote orchestration of a RPi Pico W running in Arduino mode from using .NET. The Blockly package used, NETCoreBlockly, includes function authoring and calls but at the time of writing this it is undocumented.

In NetCore Blockly there is a option to add a function as a separate set of blocks on the program page that can be called with or without parameters and/or return a result. I had a need to call some code that displays a level acting as a sound level meter where the level is supplied a a number 0 to 8. The Blockly example provided in Softata is the Neopixel8 one. This has been renamed to indicate it does not use a function.

In that code a 8 bit bitmask is created depending upon a supplied (in a loop 0..8) integer 0 to 8. Each LED is assigned to one of the bitmask LEDs and so providing the byte 0 to 8 determines what is displayed. In the sound level example it was desired to reuse the bitmasking code and call to the display in a function with the parameter 0…8.

Including a function

  • Expand Blockly Core on left
  • Click on Functions
  • Click on the top block, for now
  • Replace the “do something” with the function name, eg MyFunction.

  • The function as a new block is now available in Functions:

  • The function block can now be added to the main code:
  • You would then insert the function code.

Function Parameter

This is a bit more subtle!

  • On the function block click on the small icon top left of the block (the red circle below).
  • Enter the input (parameter) name (yellow box above).
  • Drag that block into the inputs block there, you can have more than one (green box above and below).
  • The function block under functions (yellow box above) and in the code (red box above) now has the parameter/input included.
  • Drag the function block call into the main app (in pink).
  • To close the parameter balloon click on the small blue icon (as per red circle previous) again.

A Simple Blockly app with a parameter function call

A Function with a return value

  • To create a function with a return value, choose the function block in the function tab a return monicker at its bottom right.
  • Give the function a name as previous and an input parameter if required, as previous.
  • Provide in function code to do the processing.
  • Add the return value, bottom right of the function.
  • Add the function call (from the function tab) with a call parameter (if required).
  • It can either return to a print action or assign to a variable.
  • An example follows:

Conclusion

NetCoreBlockly provides a simple but useful set of blocks for implementing block collections as a function that can be called, and returns from the “main” program.


 TopicSubtopic
   
 This Category Links 
Category:Softata Index:Softata
  Next: > Softata
<  Prev:   Softata