默认标签显然在 switch 语句中不起作用
default label apparently not working in switch statement
我看过一些关于这个问题的相关帖子的答案,但并不完全是我的问题的答案。这是我的代码:
#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(){}
在 switch(REMOTE)
处的 remoteScriptName()
函数中有 case:
和 default:
。当我将我的代码上传到我的 Arduino micro 时,它会跳过 switch(REMOTE)
中的 default
。
但是在 void setup
我有相同的 switch 语句,但在这种情况下(在上传代码之后)它确实执行了 default:
!
我对switch()
没有太多经验,所以任何推荐都很好。
可能范围不够,所以尝试每个案例
//..
case 1:{
vw_get_message(RFbuff, &RFbuflen);
String RFSTRBUFF = RFbuff;
scriptName = RFSTRBUFF;
delay(1200);
}
break;
default:
scriptName = "script";
delay(500);
}
//...
你的问题促使我提供一份带存根的代码。
在 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
我看过一些关于这个问题的相关帖子的答案,但并不完全是我的问题的答案。这是我的代码:
#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(){}
在 switch(REMOTE)
处的 remoteScriptName()
函数中有 case:
和 default:
。当我将我的代码上传到我的 Arduino micro 时,它会跳过 switch(REMOTE)
中的 default
。
但是在 void setup
我有相同的 switch 语句,但在这种情况下(在上传代码之后)它确实执行了 default:
!
我对switch()
没有太多经验,所以任何推荐都很好。
可能范围不够,所以尝试每个案例
//..
case 1:{
vw_get_message(RFbuff, &RFbuflen);
String RFSTRBUFF = RFbuff;
scriptName = RFSTRBUFF;
delay(1200);
}
break;
default:
scriptName = "script";
delay(500);
}
//...
你的问题促使我提供一份带存根的代码。
在 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