Mblock 4.0.4 (linux) problems creating extension


#1

I have defined a new extension for mBlock. The arduino code generated does not however correspond to my definition.

			"def":"int pinA1 = 11;\nint pinA2 = 10;\nint pinB1 = 6;\nint pinB2 = 5;\nvoid L298control(int motors, int mode){\n if(motors == 1 || motors == 2) {\n  switch (mode) {\n   case 0: \n    digitalWrite(pinA1,1) ;\n    digitalWrite(pinA2,0);\n    break;\n   case 1: \n    digitalWrite(pinA1,0);\n    digitalWrite(pinA2,1);\n    break;\n   case 2: \n    digitalWrite(pinA1,1);\n    digitalWrite(pinA2,1);\n    break;\n   case 3: \n    digitalWrite(pinA1,0);\n    digitalWrite(pinA2,0);\n  }\n}\nif(motors == 0 || motors == 2) {\n  switch (mode) {\n   case 0:\n    digitalWrite(pinB1,0);\n    digitalWrite(pinB2,1);\n    break;\n   case 1: \n    digitalWrite(pinB1,1);\n    digitalWrite(pinB2,0);\n    break;\n   case 2: \n    digitalWrite(pinB1,1);\n    digitalWrite(pinB2,1);\n    break;\n   case 3: \n    digitalWrite(pinB1,0);\n    digitalWrite(pinB2,0);\n  }\n}\n}\n",

Somehow becomes
int pinA1 = 11;
int pinA2 = 10;
int pinB1 = 6;
int pinB2 = 5;
void L298control(int motors, int mode){
if(motors == 1 || motors == 2) {
switch (mode) {
case 0:
digitalWrite(pinA1,1) ;
digitalWrite(pinA2,0);
break;
case 1:
digitalWrite(pinA1,0);
digitalWrite(pinA2,1);
case 2:
digitalWrite(pinA1,1);
case 3:
}
if(motors == 0 || motors == 2) {
digitalWrite(pinB1,0);
digitalWrite(pinB2,1);
digitalWrite(pinB1,1);
digitalWrite(pinB2,0);

The second if-statement has lost a layer of switch-statement, and it is not terminated. But the problems start in the case 2 part of the first if statement.

This whole function works fine if I inline it in “work” instead of defining a function, but that leads to a lot of duplicated code.

Also, I can use tabulators in “work”, but not in “def”.

Can I work around this in some efficient manner? Or is there a secret beta of mBlock 5 for linux?


#2

Good afternoon,
This is instruction for writing extension with mBlock: http://www.mblock.cc/docs/create-extensions-for-mblock/?
noredirect=en_US
mBlock 5 for Linux, it is not decided yet. We’ll forward to corresponding department.


#3

I have looked at those. I thought my extension was so simple I didn’t need to have .cpp and .h files. But now I have tried moving my code for this motor controller into a set of .cpp and .h files instead.

Now my entry in the s2e file looks like this
[
“w”,
“Set motor %d.mselect to %d.mcontrol”,
“motorDriver”,
“BOTH”,
“BRAKE”,
{
“setup”:"",
“inc”:"#include “L298.h”\n",
“def”:“L298 l298;\n”,
“work”:“l298.control({0},{1});\n”,
“loop”:""
}
],
My L298.h file (in the src subdir) looks like this
#ifndef l298_h
#define l298_h

#include <Arduino.h>
///@brief Class for L298
class L298
{
	public:
		void control(int motors, int mode);
	private:
		int pinAE;
		int pinA1;
		int pinA2;
		int pinBE;
		int pinB1;
		int pinB2;
};


#endif

And my L298.cpp file:
#include “L298.h”

L298::L298(){
	pinAE=0;
	pinA1=11;
	pinA2=10;
	pinBE=0;
	pinB1=6;
	pinB2=5;
}

void L298::control(int motors, int mode){
	if(motors == 1 || motors == 2) {
		switch (mode) {
			case 0:
				digitalWrite(pinA1,1);
				digitalWrite(pinA2,0);
				break;
			case 1:
				digitalWrite(pinA1,0);
				digitalWrite(pinA2,1);
				break;
			case 2:
				digitalWrite(pinA1,1);
				digitalWrite(pinA2,1);
				break;
			case 3:
				digitalWrite(pinA1,0);
				digitalWrite(pinA2,0);
				break;
		}
	}
	if(motors == 0 || motors == 2)
		switch (mode) {
			case 0:
				digitalWrite(pinB1,0);
				digitalWrite(pinB2,1);
				break;
			case 1:
				digitalWrite(pinB1,1);
				digitalWrite(pinB2,0);
				break;
			case 2:
				digitalWrite(pinB1,1);
				digitalWrite(pinB2,1);
				break;
			case 3:
				digitalWrite(pinB1,0);
				digitalWrite(pinB2,0);
				break;
		}
	}
}

Now I get the following error:

fatal error: L298.h: No such file or directory

(That error dialog prevents me from copying text in it. That’s a bit dumb when it contains output you want people to hand out when seeking help)

So how do I go about letting that compiler understand where to look for the file?