avdwebLibraries / avdweb_Switch

Switch library with longPress and doubleClick
http://www.avdweb.nl/arduino/hardware-interfacing/simple-switch-debouncer.html
GNU General Public License v3.0
45 stars 16 forks source link

Suggestion - Ability to set pin via setter #11

Closed LoveMyData closed 5 years ago

LoveMyData commented 6 years ago

Thanks for the library, this is the easiest one to use by far.

I have a challenge at this point as I am trying to create code that is can work on various boards and they have different PIN number to start.

I am trying to create something like this and I am hoping this make sense (sorry, pretty new to C++). With a 'setter', I think it may works.

Switch vswitch[16];

void setup() {
  for (unsigned char i=0; i<sizeof(vswitch); i++) {
    vswitch[i] = Switch.setPin(54+i);     // for Mega, A0 starts at 54;
  }
}

void loop() {
  for (unsigned char i=0; i<sizeof(vswitch); i++) {
    vswitch[54+i].poll;     // for Mega, A0 starts at 54;
    if ( vSwitch[54+i].singleClick() ) {
      // do stuff
    }
  }  
}
Martin-Laclaustra commented 6 years ago

It could be done but because of the way this library works I am not sure if it makes sense to change the pin at run-time. By looking at your code I can not figure out what are your goals. You may need to devote some time to study pointers and make sure if you really need an array of switch objects. In your code a setter would overwrite the pin of the object, and it would not create a different object to control every pin (which seems to be you intention).

I think that what you need (same code - different platforms) is to change the pin at compile time. I suggest you to have a look at preprocessor statements and conditional compilation. Something like this:

'

if defined(ESP8266)

#define PIN 12

else

#define PIN 4

endif

Switch pushButton = Switch(PIN); '

Please close the issue if this answers you question.

LoveMyData commented 6 years ago

I am coding a web interface on the mega and would like to have the ability to change what pin to use as vswitch (input) at run time and depending on the action, ability to program which pin or pins to use as output.

But I do see your point

Martin-Laclaustra commented 6 years ago

That is clearer. Let me know if the following suggestion helps:

LoveMyData commented 5 years ago

Do you have a snippet of code that I can try?

I am kind of understand the concept on what you are saying but I have no idea on how CPP works in detail.

Many Thanks in advance

Martin-Laclaustra commented 5 years ago

I recommend that you start by getting a deeper understanding of c++ before going for more complex hardware programming. See here a resource on the particular topic of "new" and "delete": http://www.cplusplus.com/doc/tutorial/dynamic/

I am closing the issue because this is no longer related to the library. Best luck in your quest for harnessing c++ and hardware.

LoveMyData commented 5 years ago

Thanks for the information and suggestions

LoveMyData commented 5 years ago

for anyone in the future may find this. I am testing this on Arduino mega and using pin 22 to 38 as input. Using dynamic memory allocation to assigned switch port based on a variable. Not sure is this right or not, but seems working so far based on Martin's suggestion.

#include <avdweb_Switch.h>

Switch *vSwitch[16];
int i;

void setup() {
  Serial.begin(9600);
  for ( unsigned int i=0; i<16; i++) {
    vSwitch[i] = new Switch(22+i);
  }
}

void loop() {
  for ( unsigned int i=0; i<16; i++) {
    vSwitch[i]->poll();
    if(vSwitch[i]->switched()) { Serial.print("pushButton switched on switch "); Serial.println(i); }
    if(vSwitch[i]->pushed()) { Serial.print("pushButton pushed on switch "); Serial.println(i); }
    if(vSwitch[i]->released()) { Serial.print("pushButton released on switch "); Serial.println(i); }
  }
}