sui77 / rc-switch

Arduino lib to operate 433/315Mhz devices like power outlet sockets.
1.89k stars 651 forks source link

ReceiveDemo_Advanced work with Platform.IO and VSCode #489

Open heman22union opened 1 year ago

heman22union commented 1 year ago

I'm trying to get the ReceiveDemo_Advanced example to work with Platform.IO and VS Code. I'm able to get it to work with the Arduino IDE, but I prefer VS Code.

Here is what I have including adding of the #include to both files? It seems I need to add to the output.cpp file too?

.\test_rec\ReceiveDemo_Advanced.cpp file

/*
  Example for receiving

  https://github.com/sui77/rc-switch/

  If you want to visualize a telegram copy the raw data and 
  paste it into http://test.sui.li/oszi/
*/

#include <Arduino.h>
#include <RCSwitch.h>
#include "output.cpp"

RCSwitch mySwitch = RCSwitch();

void setup() {
  Serial.begin(9600);
  mySwitch.enableReceive(0);  // Receiver on interrupt 0 => that is pin #2
}

void loop() {
  if (mySwitch.available()) {
    output(mySwitch.getReceivedValue(), mySwitch.getReceivedBitlength(), mySwitch.getReceivedDelay(), mySwitch.getReceivedRawdata(),mySwitch.getReceivedProtocol());
    mySwitch.resetAvailable();
  }
}

.\test_rec\output.cpp file

#include <Arduino.h>

static const char* bin2tristate(const char* bin);
static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength);

void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {

  const char* b = dec2binWzerofill(decimal, length);
  Serial.print("Decimal: ");
  Serial.print(decimal);
  Serial.print(" (");
  Serial.print( length );
  Serial.print("Bit) Binary: ");
  Serial.print( b );
  Serial.print(" Tri-State: ");
  Serial.print( bin2tristate( b) );
  Serial.print(" PulseLength: ");
  Serial.print(delay);
  Serial.print(" microseconds");
  Serial.print(" Protocol: ");
  Serial.println(protocol);

  Serial.print("Raw data: ");
  for (unsigned int i=0; i<= length*2; i++) {
    Serial.print(raw[i]);
    Serial.print(",");
  }
  Serial.println();
  Serial.println();
}

static const char* bin2tristate(const char* bin) {
  static char returnValue[50];
  int pos = 0;
  int pos2 = 0;
  while (bin[pos]!='\0' && bin[pos+1]!='\0') {
    if (bin[pos]=='0' && bin[pos+1]=='0') {
      returnValue[pos2] = '0';
    } else if (bin[pos]=='1' && bin[pos+1]=='1') {
      returnValue[pos2] = '1';
    } else if (bin[pos]=='0' && bin[pos+1]=='1') {
      returnValue[pos2] = 'F';
    } else {
      return "not applicable";
    }
    pos = pos+2;
    pos2++;
  }
  returnValue[pos2] = '\0';
  return returnValue;
}

static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength) {
  static char bin[64]; 
  unsigned int i=0;

  while (Dec > 0) {
    bin[32+i++] = ((Dec & 1) > 0) ? '1' : '0';
    Dec = Dec >> 1;
  }

  for (unsigned int j = 0; j< bitLength; j++) {
    if (j >= bitLength - i) {
      bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
    } else {
      bin[j] = '0';
    }
  }
  bin[bitLength] = '\0';

  return bin;
}

platform.io terminal output

 *  Executing task in folder esp_doorbell: C:\Users\Heman\.platformio\penv\Scripts\platformio.exe run --target upload --environment nanoatmega328_test_rec 

Processing nanoatmega328_test_rec (platform: atmelavr; board: nanoatmega328; framework: arduino)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/nanoatmega328.html
PLATFORM: Atmel AVR (4.1.0) > Arduino Nano ATmega328
HARDWARE: ATMEGA328P 16MHz, 2KB RAM, 30KB Flash
DEBUG: Current (avr-stub) External (avr-stub, simavr)
PACKAGES: 
 - framework-arduino-avr @ 5.1.0 
 - tool-avrdude @ 1.60300.200527 (6.3.0) 
 - toolchain-atmelavr @ 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 6 compatible libraries
Scanning dependencies...
Dependency Graph
|-- rc-switch @ 2.6.4
Building in release mode
Linking .pio\build\nanoatmega328_test_rec\firmware.elf
.pio\build\nanoatmega328_test_rec\src\test_rec\output.cpp.o (symbol from plugin): In function `output(unsigned long, unsigned int, unsigned int, unsigned int*, unsigned int)':
(.text+0x0): multiple definition of `output(unsigned long, unsigned int, unsigned int, unsigned int*, unsigned int)'
.pio\build\nanoatmega328_test_rec\src\test_rec\ReceiveDemo_Advanced.cpp.o (symbol from plugin):(.text+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
*** [.pio\build\nanoatmega328_test_rec\firmware.elf] Error 1
============================================================================================= [FAILED] Took 0.93 seconds =============================================================================================

Environment             Status    Duration
----------------------  --------  ------------
nanoatmega328_test_rec  FAILED    00:00:00.934
======================================================================================= 1 failed, 0 succeeded in 00:00:00.934 ======================================================================================= 

 *  The terminal process "C:\Users\Heman\.platformio\penv\Scripts\platformio.exe 'run', '--target', 'upload', '--environment', 'nanoatmega328_test_rec'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it. 

platformio.ini file

[env:nanoatmega328_transmitter]
platform = atmelavr
board = nanoatmega328
framework = arduino
build_src_filter = +<transmitter/> -<receiver/> -<test_rec/>
upload_port = COM5
monitor_port = COM5
lib_deps = sui77/rc-switch@^2.6.4

[env:nanoatmega328_receiver]
platform = atmelavr
board = nanoatmega328
framework = arduino
build_src_filter = -<transmitter/> +<receiver/> -<test_rec/>
upload_port = COM6
monitor_port = COM6
lib_deps = sui77/rc-switch@^2.6.4

[env:nanoatmega328_test_rec]
platform = atmelavr
board = nanoatmega328
framework = arduino
build_src_filter = -<transmitter/> -<receiver/> +<test_rec/>
upload_port = COM6
monitor_port = COM6
lib_deps = sui77/rc-switch@^2.6.4
heman22union commented 1 year ago

If I remove the #include from .\test_rec\output.cpp file

//#include <Arduino.h>

static const char* bin2tristate(const char* bin);
static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength);

void output(unsigned long decimal, unsigned int length, unsigned int delay, unsigned int* raw, unsigned int protocol) {

  const char* b = dec2binWzerofill(decimal, length);
  Serial.print("Decimal: ");
  Serial.print(decimal);
  Serial.print(" (");
  Serial.print( length );
  Serial.print("Bit) Binary: ");
  Serial.print( b );
  Serial.print(" Tri-State: ");
  Serial.print( bin2tristate( b) );
  Serial.print(" PulseLength: ");
  Serial.print(delay);
  Serial.print(" microseconds");
  Serial.print(" Protocol: ");
  Serial.println(protocol);

  Serial.print("Raw data: ");
  for (unsigned int i=0; i<= length*2; i++) {
    Serial.print(raw[i]);
    Serial.print(",");
  }
  Serial.println();
  Serial.println();
}

static const char* bin2tristate(const char* bin) {
  static char returnValue[50];
  int pos = 0;
  int pos2 = 0;
  while (bin[pos]!='\0' && bin[pos+1]!='\0') {
    if (bin[pos]=='0' && bin[pos+1]=='0') {
      returnValue[pos2] = '0';
    } else if (bin[pos]=='1' && bin[pos+1]=='1') {
      returnValue[pos2] = '1';
    } else if (bin[pos]=='0' && bin[pos+1]=='1') {
      returnValue[pos2] = 'F';
    } else {
      return "not applicable";
    }
    pos = pos+2;
    pos2++;
  }
  returnValue[pos2] = '\0';
  return returnValue;
}

static char * dec2binWzerofill(unsigned long Dec, unsigned int bitLength) {
  static char bin[64]; 
  unsigned int i=0;

  while (Dec > 0) {
    bin[32+i++] = ((Dec & 1) > 0) ? '1' : '0';
    Dec = Dec >> 1;
  }

  for (unsigned int j = 0; j< bitLength; j++) {
    if (j >= bitLength - i) {
      bin[j] = bin[ 31 + i - (j - (bitLength - i)) ];
    } else {
      bin[j] = '0';
    }
  }
  bin[bitLength] = '\0';

  return bin;
}

platformio terminal output

 *  Executing task in folder esp_doorbell: C:\Users\Heman\.platformio\penv\Scripts\platformio.exe run --target upload --environment nanoatmega328_test_rec 

Processing nanoatmega328_test_rec (platform: atmelavr; board: nanoatmega328; framework: arduino)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/atmelavr/nanoatmega328.html
PLATFORM: Atmel AVR (4.1.0) > Arduino Nano ATmega328
HARDWARE: ATMEGA328P 16MHz, 2KB RAM, 30KB Flash
DEBUG: Current (avr-stub) External (avr-stub, simavr)
PACKAGES: 
 - framework-arduino-avr @ 5.1.0 
 - tool-avrdude @ 1.60300.200527 (6.3.0) 
 - toolchain-atmelavr @ 1.70300.191015 (7.3.0)
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 6 compatible libraries
Scanning dependencies...
Dependency Graph
|-- rc-switch @ 2.6.4
Building in release mode
Compiling .pio\build\nanoatmega328_test_rec\src\test_rec\ReceiveDemo_Advanced.cpp.o
Compiling .pio\build\nanoatmega328_test_rec\src\test_rec\output.cpp.o
src\test_rec\output.cpp: In function 'void output(long unsigned int, unsigned int, unsigned int, unsigned int*, unsigned int)':
src\test_rec\output.cpp:10:3: error: 'Serial' was not declared in this scope
   Serial.print("Decimal: ");
   ^~~~~~
src\test_rec\output.cpp:10:3: note: suggested alternative: 'decimal'
   Serial.print("Decimal: ");
   ^~~~~~
   decimal
*** [.pio\build\nanoatmega328_test_rec\src\test_rec\output.cpp.o] Error 1
============================================================================================= [FAILED] Took 0.73 seconds =============================================================================================

Environment             Status    Duration
----------------------  --------  ------------
nanoatmega328_test_rec  FAILED    00:00:00.729
======================================================================================= 1 failed, 0 succeeded in 00:00:00.729 ======================================================================================= 

 *  The terminal process "C:\Users\Heman\.platformio\penv\Scripts\platformio.exe 'run', '--target', 'upload', '--environment', 'nanoatmega328_test_rec'" terminated with exit code: 1. 
 *  Terminal will be reused by tasks, press any key to close it.