Extensions - including variable string in Arduino code


#1

Hi,

I’ve recently added an mBlock extension that adds blocks to access the Pixy2 camera API:

Although it works, I would like to streamline this if possible but couldn’t see how to do this. The Pixy 2 API (https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:ccc_api) includes an array of objects it has detected (Pixy2 also calls these “blocks” so don’t get confused). The attributes of these objects can be accessed via member variables e.g.:

  • pixy.ccc.blocks[0].m_signature (signature)
  • pixy.ccc.blocks[0].m_x (x position)
  • pixy.ccc.blocks[0].m_y (y position)
  • etc…

I tried to create a single “get” block which had two parameters:

  1. Array index
  2. Member variable (via dropdown)

However, whatever way I tried this, I ended up with quotes in the Arduino code and this doesn’t work e.g.

  • pixy.ccc.blocks[0].“m_signature”

So I could get this working, I created a separate mBlock block explicitly for each member variable and just passed in the array index. See the following snippet:

  [
    "r",
    "get block %n signature",
    "get block signature",
	0,
      {
        "inc" : "#include <SPI.h>\n#include \"Pixy2.h\"\n", 

        "def" : "Pixy2 pixy;\n",
        "setup" : "pixy.init();\n",
        "work" : "pixy.ccc.blocks[(int) {0}].m_signature",
        "loop" : ""
      }
  ],
  [
    "r",
    "get block %n x pos",
    "get block x pos",
	0,
      {
        "inc" : "#include <SPI.h>\n#include \"Pixy2.h\"\n", 

        "def" : "Pixy2 pixy;\n",
        "setup" : "pixy.init();\n",
        "work" : "pixy.ccc.blocks[(int) {0}].m_x",
        "loop" : ""
      }
  ],  

Does anyone have a suggestion on how I could consolidate the multiple mBlock blocks into a single one with valid Arduino code as per my original plan?


#2

Thanks a lot, Grant, for all the effort you’ve taken to code this extension and for making it available for everyone. You are the only one who has contributed to this, without whom people like me without good arduino skills would never be able to use a pixy cam, the instructable using mBot is for pixy1 and the libraries of pixy2 are different.
Would like to bring it to the attention of other users that when we use “get blocks wait (true) sigmap…” mblock block within an if statement, for example
if ((get blocks wait (true) sigmap (1) maxblocks (10) ) > (1)) {…}
then it wouldn’t compile because the arduino code will have a semicolon after maxblock (10) so we’ll have to “Edit with Arduino IDE” (i.e. in mBlock3) and remove that semicolon to be able to upload onto mCore/mBot.
if((pixy.ccc.getBlocks(true,(uint8_t) 1,(uint8_t) 10)) >= (1)){…}
/Comment - Also changed > to >= to avoid using a logical OR operator in mBlock program/
Sorry if my English isn’t good.