默认标签显然在switch语句中不起作用(已解决)

问题描述 投票:0回答:2

我已经看到了有关此问题的相关帖子的一些答案,但并不能完全解决我的问题。这是我的代码:

#include <VirtualWire.h>
uint8_t RFbuff[VW_MAX_MESSAGE_LEN];   
uint8_t RFbuflen = VW_MAX_MESSAGE_LEN;
const int REMOTE = 0; 
String remoteScriptName(){
  String scriptName;
  switch(REMOTE){

    case 1:
      vw_get_message(RFbuff, &RFbuflen);
      String RFSTRBUFF = RFbuff;
      scriptName = RFSTRBUFF; 
      delay(1200);
      break;
    default:
      scriptName = "script"; 
      delay(500); 
    }
  return(scriptName);
}

void setup() {
  pinMode(LED_BUILTIN, HIGH);
  switch(REMOTE){
    case 1:
      vw_set_rx_pin(8);
      vw_setup(2000);
      vw_rx_start();
      break; 
    default: 
      digitalWrite(LED_BUILTIN, LOW); 
      delay(500);
      digitalWrite(LED_BUILTIN, HIGH);
      delay(500);
      digitalWrite(LED_BUILTIN, LOW); 
      delay(500);
      digitalWrite(LED_BUILTIN, HIGH);
  }
}

void loop(){}

remoteScriptName()switch(REMOTE)功能中,有case:default:。当我将代码上传到Arduino micro时,它会跳过default中的switch(REMOTE)。但是然后在void setup,我有相同的switch语句,但是在这种情况下(上传代码后)它确实执行了default:

我在switch()方面没有太多经验,因此任何建议都很受欢迎。

c++ arduino switch-statement arduino-c++
2个回答
0
投票

您的问题促使我提供带存根代码的副本。

基于Lubuntu 18.10开发

使用g ++(Ubuntu 9.2.1-9ubuntu2)9.2.1 20191008


#ifndef                 DTB_CHRONO_HH
#include "../../bag/src/dtb_chrono.hh"
using  namespace std::chrono_literals; // support suffixes like 100ms, 2s, 30us
#endif

#include <iostream>
using std::cout, std::cerr, std::endl;

#include <string>
using std::string, std::to_string;

#include <thread>
using std::thread, std::this_thread::sleep_for;

#ifndef                 DTB_PCKLRT_HH
#include "../../bag/src/dtb_pclkrt.hh"  // class PosixClockRT_t
using  DTB::PClk_t;
#endif

#ifndef                 DTB_SOPS_HH
#include "../../bag/src/dtb_sops.hh" // class StringOps_t
using DTB::Sops_t;
#endif


namespace // anonymous
{
   // functor
   class F838_t  // a UDT (user defined type)
   {
      PClk_t  pclk; // class PosixClockRT_t
      Sops_t  sops; // class StringOps_t

      enum { VW_MAX_MESSAGE_LEN = 10, LED_BUILTIN, LOW=0x05, HIGH=0x0a };

      uint8_t RFbuff     [VW_MAX_MESSAGE_LEN];
      uint8_t RFbuflen = VW_MAX_MESSAGE_LEN;

      const int REMOTE = 0;

   public:
      // ctor and dtor use compiler provided defaults
      int operator()(int argc, char* argv[]) { return exec(argc, argv);  }

   private:

      int exec(int , char** )
         {
            int retVal = 0;
            uint64_t start_ns = pclk.ns();

            setup();
            remoteScriptName();

            cout << "\n\n  F838_t::exec() duration "
                 << sops.digiComma(pclk.ns() - start_ns)
                 << " ns    (" <<  __cplusplus  << ")"
                 << std::endl;
            return retVal;
         }

      void setup()
         {
            pinMode(LED_BUILTIN, HIGH);
            switch(REMOTE)
            {

            case 1:
            {
               vw_set_rx_pin(8);
               vw_setup(2000);
               vw_rx_start();
            }  break;

            default:
            {
               digitalWrite(LED_BUILTIN, LOW);
               sleep_for(500ms);  // delay(500);
               digitalWrite(LED_BUILTIN, HIGH);
               sleep_for(500ms);  // delay(500);
               digitalWrite(LED_BUILTIN, LOW);
               sleep_for(500ms);  // delay(500);
               digitalWrite(LED_BUILTIN, HIGH);
            } break;

            }
         }

      string remoteScriptName()
         {
            string scriptName;
            switch(REMOTE)
            {

            case 1:
            {
               vw_get_message(RFbuff, &RFbuflen);
               //string RFSTRBUFF = RFbuff;
               string RFSTRBUFF;
               for (int i; i < RFbuflen; ++i)
                  RFSTRBUFF += static_cast<char>(RFbuff[i]);
               scriptName = RFSTRBUFF;
               sleep_for(1200ms); //delay(1200);
            }

            break;

            default:
            {
               scriptName = "script";
               sleep_for(500ms); // delay(500);
            }
            }

            return(scriptName);
         } // string remoteScriptName()

      // stubs:
      void pinMode(int led, int v) { cout << "\n  pinMode("
                                          << led << "," << v << ")"; }

      void vw_set_rx_pin(int v) { cout << "\n  vw_set_rx_pin("
                                       << v << ")"; }
      void vw_setup(int v)      { cout << "\n  vw_setup     ("
                                       << v << ")"; }
      void vw_rx_start()        { cout << "\n  vw_rx_start  ()"; }

      void digitalWrite(int w,
                        int v) { cout << "\n  digitalWrite  ("
                                      << w << "," << v << ")"; }

      void vw_get_message(uint8_t*  bf,
                          uint8_t*  ln)
         { cout << "\n  vw_get_message  ("
                << bf << "," <<  *ln << ")"; }

   }; // class F838_t

}  // anonymous namespace

// one line main invokes functor ---------vvvvvvvv
int main(int argc, char* argv[]) { return F838_t()(argc, argv); }

注意:142行,仅14行存根


生成输出以显示编译器选项:

time make CC='g++ $(m) -O0 ' dumy838 ; ./dumy838

>>>cvs-tools/lmbm/src800/Makefile.i686

R_exe: dumy838.cc
rm -f dumy838
g++ -std=c++17 -m64 -ggdb -O3 -Wall -Wextra -Wshadow -pedantic -Werror=vla 
-Wcast-align -Wcast-qual -Wconversion -Wsign-conversion -Wsign-compare 
-Wsign-promo -Wpointer-arith -Wunused -Wold-style-cast -Woverloaded-virtual
-Wsequence-point -Wdelete-incomplete -Wmaybe-uninitialized 
-Wmisleading-indentation -Wunreachable-code -Wnon-virtual-dtor 
-MMD -MP -O0   dumy838.cc  -o dumy838 
-L../../bag/src -lbag_i686 -lrt -pthread -lncurses -lgmpxx -lgmp

real    0m1.506s
user    0m1.289s
sys 0m0.181s

存根的执行输出,包括posix时间测量

  pinMode(11,10)
  digitalWrite  (11,5)
  digitalWrite  (11,10)
  digitalWrite  (11,5)
  digitalWrite  (11,10)

  F838_t::exec() duration 2,000,584,200 ns    (201703)

Compilation finished at Sat Dec 28 11:06:06

1
投票

可能范围不足,因此请尝试每种情况

//..

     case 1:{
          vw_get_message(RFbuff, &RFbuflen);
          String RFSTRBUFF = RFbuff;
          scriptName = RFSTRBUFF; 
          delay(1200);
       }
          break;
        default:
          scriptName = "script"; 
          delay(500); 
        }
 //...
© www.soinside.com 2019 - 2024. All rights reserved.