taweili / ardublock

ArduBlock is a Block Programming Language for Arduino.
GNU General Public License v3.0
413 stars 292 forks source link

Can DigitalOutputBlock be changed back without breaking anything? #149

Closed TonyCallear closed 7 years ago

TonyCallear commented 8 years ago

The DigitalOutputBlock no longer uses a function "__ardublockDigitalWrite" to set the pin state. This used to make sure the pin was actually set for output at the time of the 'Write' - this was a good idea as without it some code get broken. Can it be changed back without breaking any of the "new stuff"?

One example. This sketch capture04 produces this code

int _ABVAR_1_pin_var = 0 ;

void setup()
{
  pinMode( _ABVAR_1_pin_var , OUTPUT); // Ooops! pin 0 set to OUTPUT instead of pin 4 !
  _ABVAR_1_pin_var = 4 ;

}

void loop()
{
  digitalWrite(_ABVAR_1_pin_var , HIGH);
}

Another example, some hardware can use the same pin for input and output. This used to work...

void __ardublockDigitalWrite(int pinNumber, boolean status)
{
  pinMode(pinNumber, OUTPUT);
  digitalWrite(pinNumber, status);
}

bool _ABVAR_1_digitalvariablename= false ;
boolean __ardublockDigitalRead(int pinNumber)
{
  pinMode(pinNumber, INPUT);
  return digitalRead(pinNumber);
}

void setup()
{
}

void loop()
{
  __ardublockDigitalWrite(2, HIGH);

// Do Something interesting here

  _ABVAR_1_digitalvariablename = __ardublockDigitalRead(2) ;

// Then carry on

}

But now it doesn't (DigitalOutputBlock now just sets pinMode once in setup) ...

bool _ABVAR_1_Bool= false ;
boolean __ardublockDigitalRead(int pinNumber)
{
  pinMode(pinNumber, INPUT);
  return digitalRead(pinNumber);
}

void setup()
{
  pinMode( 2 , OUTPUT);
}

void loop()
{
  digitalWrite(2 , HIGH); // will work only once as pin becomes INPUT later

// Do something interesting here...

  _ABVAR_1_Bool = __ardublockDigitalRead(2) ;

// And then something else here...

}

See pull request #146