【发布时间】:2020-01-31 20:53:59
【问题描述】:
第二次编辑:我现在以某种方式设法让脚本运行(将 int main(void) 更改为 int main())并定义了“for”,但是它严重地忽略了我并且无论如何都无休止地运行:
int main() { // was int main(void)!!
// We'll start by performing hardware and peripheral setup.
SetupHardware();
// We'll then enable global interrupts for our use.
GlobalInterruptEnable();
//int counter = 0;
//int x_times = 1000;
// Once that's done, we'll enter an infinite loop.
for (day = 0;day = 2;day++) // RE-ENABLE THIS TO PUT THE INFINITE LOOP BACK IN!
// trying to make a loop that will only run a certain number of times (5)...
{
// We need to run our task to process and deliver data for our IN and OUT endpoints.
HID_Task();
// We also need to run the main USB management task.
USB_USBTask();
}
return 0;
}
编辑1:有一些澄清:
此代码取自一个项目,以使 Arduino 看起来像 Nintendo Switch 的操纵杆,基于 https://github.com/shinyquagsire23/Switch-Fightstick。
我认为 Arduino 使用的固件与其随附的固件不同,因为在“make”编译一个包含 Joystick.c 和其他脚本的 .hex 文件之后,整个脚本在 Arduino 上闪烁。我将把 Joystick.c 的全部内容包含在下面来自原始开发者的 cmets 中,希望能更清楚地说明这个请求。
/*
Nintendo Switch Fightstick - Proof-of-Concept
Based on the LUFA library's Low-Level Joystick Demo
(C) Dean Camera
Based on the HORI's Pokken Tournament Pro Pad design
(C) HORI
This project implements a modified version of HORI's Pokken Tournament Pro Pad
USB descriptors to allow for the creation of custom controllers for the
Nintendo Switch. This also works to a limited degree on the PS3.
Since System Update v3.0.0, the Nintendo Switch recognizes the Pokken
Tournament Pro Pad as a Pro Controller. Physical design limitations prevent
the Pokken Controller from functioning at the same level as the Pro
Controller. However, by default most of the descriptors are there, with the
exception of Home and Capture. Descriptor modification allows us to unlock
these buttons for our use.
*/
#include "Joystick.h"
typedef enum {
UP,
DOWN,
LEFT,
RIGHT,
X,
Y,
A,
B,
L,
R,
THROW,
NOTHING,
TRIGGERS,
HOME
} Buttons_t;
typedef struct {
Buttons_t button;
uint16_t duration;
} command;
static const command step[] = {
// Setup controller
{ NOTHING, 250 },
{ TRIGGERS, 5 },
{ NOTHING, 150 },
{ TRIGGERS, 5 },
{ NOTHING, 150 },
{ TRIGGERS, 5 },
{ NOTHING, 250 },
// Talk to Pondo
{ NOTHING, 100 },
{ A, 10 }, // Start
{ NOTHING, 20 },
{ A, 10 },
{ NOTHING, 20 },
{ A, 10 },
{ NOTHING, 20 },
{ A, 10 },
{ NOTHING, 50 },
{ HOME, 10 },
{ NOTHING, 30 },
{ DOWN, 5 },
{ NOTHING, 5 },
{ RIGHT, 5 },
{ NOTHING, 5 },
{ RIGHT, 5 },
{ NOTHING, 5 },
{ RIGHT, 5 },
{ NOTHING, 5 },
{ RIGHT, 5 },
{ NOTHING, 5 },
{ A, 10 },
{ NOTHING, 20 },
{ DOWN, 60 },
{ NOTHING, 5 },
{ A, 10 },
{ NOTHING, 5 },
{ DOWN, 5 },
{ NOTHING, 5 },
{ DOWN, 5 },
{ NOTHING, 5 },
{ DOWN, 5 },
{ NOTHING, 5 },
{ DOWN, 5 },
{ NOTHING, 5 },
{ A, 10 },
{ NOTHING, 10 },
{ DOWN, 5 },
{ NOTHING, 5 },
{ DOWN, 5 },
{ NOTHING, 5 },
{ A, 10 },
{ NOTHING, 10 },
{ UP, 5 },
{ NOTHING, 5 },
{ A, 10 },
{ NOTHING, 5 },
{ A, 10 },
{ NOTHING, 5 },
{ A, 10 },
{ NOTHING, 5 },
{ A, 10 },
{ NOTHING, 5 },
{ A, 10 },
{ NOTHING, 5 },
{ A, 10 },
{ NOTHING, 5 },
{ HOME, 10 },
{ NOTHING, 20 },
{ HOME, 10 },
{ NOTHING, 30 },
{ B, 10 },
{ NOTHING, 20 },
{ A , 10 },
{ NOTHING, 100 }
};
// Main entry point.
int main(void) {
// We'll start by performing hardware and peripheral setup.
SetupHardware();
// We'll then enable global interrupts for our use.
GlobalInterruptEnable();
// Once that's done, we'll enter an infinite loop.
for (;;)
{
// We need to run our task to process and deliver data for our IN and OUT endpoints.
HID_Task();
// We also need to run the main USB management task.
USB_USBTask();
}
}
// Configures hardware and peripherals, such as the USB peripherals.
void SetupHardware(void) {
// We need to disable watchdog if enabled by bootloader/fuses.
MCUSR &= ~(1 << WDRF);
wdt_disable();
// We need to disable clock division before initializing the USB hardware.
clock_prescale_set(clock_div_1);
// We can then initialize our hardware and peripherals, including the USB stack.
#ifdef ALERT_WHEN_DONE
// Both PORTD and PORTB will be used for the optional LED flashing and buzzer.
#warning LED and Buzzer functionality enabled. All pins on both PORTB and \
PORTD will toggle when printing is done.
DDRD = 0xFF; //Teensy uses PORTD
PORTD = 0x0;
//We'll just flash all pins on both ports since the UNO R3
DDRB = 0xFF; //uses PORTB. Micro can use either or, but both give us 2 LEDs
PORTB = 0x0; //The ATmega328P on the UNO will be resetting, so unplug it?
#endif
// The USB stack should be initialized last.
USB_Init();
}
// Fired to indicate that the device is enumerating.
void EVENT_USB_Device_Connect(void) {
// We can indicate that we're enumerating here (via status LEDs, sound, etc.).
}
// Fired to indicate that the device is no longer connected to a host.
void EVENT_USB_Device_Disconnect(void) {
// We can indicate that our device is not ready (via status LEDs, sound, etc.).
}
// Fired when the host set the current configuration of the USB device after enumeration.
void EVENT_USB_Device_ConfigurationChanged(void) {
bool ConfigSuccess = true;
// We setup the HID report endpoints.
ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_OUT_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
ConfigSuccess &= Endpoint_ConfigureEndpoint(JOYSTICK_IN_EPADDR, EP_TYPE_INTERRUPT, JOYSTICK_EPSIZE, 1);
// We can read ConfigSuccess to indicate a success or failure at this point.
}
// Process control requests sent to the device from the USB host.
void EVENT_USB_Device_ControlRequest(void) {
// We can handle two control requests: a GetReport and a SetReport.
// Not used here, it looks like we don't receive control request from the Switch.
}
// Process and deliver data from IN and OUT endpoints.
void HID_Task(void) {
// If the device isn't connected and properly configured, we can't do anything here.
if (USB_DeviceState != DEVICE_STATE_Configured)
return;
// We'll start with the OUT endpoint.
Endpoint_SelectEndpoint(JOYSTICK_OUT_EPADDR);
// We'll check to see if we received something on the OUT endpoint.
if (Endpoint_IsOUTReceived())
{
// If we did, and the packet has data, we'll react to it.
if (Endpoint_IsReadWriteAllowed())
{
// We'll create a place to store our data received from the host.
USB_JoystickReport_Output_t JoystickOutputData;
// We'll then take in that data, setting it up in our storage.
while(Endpoint_Read_Stream_LE(&JoystickOutputData, sizeof(JoystickOutputData), NULL) != ENDPOINT_RWSTREAM_NoError);
// At this point, we can react to this data.
// However, since we're not doing anything with this data, we abandon it.
}
// Regardless of whether we reacted to the data, we acknowledge an OUT packet on this endpoint.
Endpoint_ClearOUT();
}
// We'll then move on to the IN endpoint.
Endpoint_SelectEndpoint(JOYSTICK_IN_EPADDR);
// We first check to see if the host is ready to accept data.
if (Endpoint_IsINReady())
{
// We'll create an empty report.
USB_JoystickReport_Input_t JoystickInputData;
// We'll then populate this report with what we want to send to the host.
GetNextReport(&JoystickInputData);
// Once populated, we can output this data to the host. We do this by first writing the data to the control stream.
while(Endpoint_Write_Stream_LE(&JoystickInputData, sizeof(JoystickInputData), NULL) != ENDPOINT_RWSTREAM_NoError);
// We then send an IN packet on this endpoint.
Endpoint_ClearIN();
}
}
typedef enum {
SYNC_CONTROLLER,
SYNC_POSITION,
BREATHE,
PROCESS,
CLEANUP,
DONE
} State_t;
State_t state = SYNC_CONTROLLER;
#define ECHOES 2
int echoes = 0;
USB_JoystickReport_Input_t last_report;
int report_count = 0;
int xpos = 0;
int ypos = 0;
int bufindex = 0;
int duration_count = 0;
int portsval = 0;
// Prepare the next report for the host.
void GetNextReport(USB_JoystickReport_Input_t* const ReportData) {
// Prepare an empty report
memset(ReportData, 0, sizeof(USB_JoystickReport_Input_t));
ReportData->LX = STICK_CENTER;
ReportData->LY = STICK_CENTER;
ReportData->RX = STICK_CENTER;
ReportData->RY = STICK_CENTER;
ReportData->HAT = HAT_CENTER;
// Repeat ECHOES times the last report
if (echoes > 0)
{
memcpy(ReportData, &last_report, sizeof(USB_JoystickReport_Input_t));
echoes--;
return;
}
// States and moves management
switch (state)
{
case SYNC_CONTROLLER:
state = BREATHE;
break;
case SYNC_POSITION:
bufindex = 0;
ReportData->Button = 0;
ReportData->LX = STICK_CENTER;
ReportData->LY = STICK_CENTER;
ReportData->RX = STICK_CENTER;
ReportData->RY = STICK_CENTER;
ReportData->HAT = HAT_CENTER;
state = BREATHE;
break;
case BREATHE:
state = PROCESS;
break;
case PROCESS:
switch (step[bufindex].button)
{
case UP:
ReportData->LY = STICK_MIN;
break;
case LEFT:
ReportData->LX = STICK_MIN;
break;
case DOWN:
ReportData->LY = STICK_MAX;
break;
case RIGHT:
ReportData->LX = STICK_MAX;
break;
case A:
ReportData->Button |= SWITCH_A;
break;
case B:
ReportData->Button |= SWITCH_B;
break;
case R:
ReportData->Button |= SWITCH_R;
break;
case THROW:
ReportData->LY = STICK_MIN;
ReportData->Button |= SWITCH_R;
break;
case TRIGGERS:
ReportData->Button |= SWITCH_L | SWITCH_R;
break;
case HOME:
ReportData->Button |= SWITCH_HOME;
break;
default:
ReportData->LX = STICK_CENTER;
ReportData->LY = STICK_CENTER;
ReportData->RX = STICK_CENTER;
ReportData->RY = STICK_CENTER;
ReportData->HAT = HAT_CENTER;
break;
}
duration_count++;
if (duration_count > step[bufindex].duration)
{
bufindex++;
duration_count = 0;
}
if (bufindex > (int)( sizeof(step) / sizeof(step[0])) - 1)
{
// state = CLEANUP;
bufindex = 7;
duration_count = 0;
state = BREATHE;
ReportData->LX = STICK_CENTER;
ReportData->LY = STICK_CENTER;
ReportData->RX = STICK_CENTER;
ReportData->RY = STICK_CENTER;
ReportData->HAT = HAT_CENTER;
// state = DONE;
// state = BREATHE;
}
break;
case CLEANUP:
state = DONE;
break;
case DONE:
#ifdef ALERT_WHEN_DONE
portsval = ~portsval;
PORTD = portsval; //flash LED(s) and sound buzzer if attached
PORTB = portsval;
_delay_ms(250);
#endif
return;
}
// Prepare to echo this report
memcpy(&last_report, ReportData, sizeof(USB_JoystickReport_Input_t));
echoes = ECHOES;
}
大家好,第一次发帖,主要是 C++ 菜鸟 - 请怜悯!
我认为我有一个不寻常的问题,因为我有一个用于 Arduino 的 C++ 脚本,它会无限重复。我希望这个脚本只执行一定次数,但到目前为止我尝试的一切都失败了。
这是带有无限循环的原始脚本:
int main(void) {
SetupHardware();
GlobalInterruptEnable();
for (;;)
{
HID_Task();
USB_USBTask();
}
}
这是我迄今为止尝试过的,让脚本只运行两次:
int main(void) {
SetupHardware();
GlobalInterruptEnable();
int day = 0;
for (day = 0; day <= 2; day++)
{
HID_Task();
USB_USBTask();
}
return;
}
我已经尝试摆脱“return”,根据“make”,代码看起来没问题,因为我没有收到任何错误消息。但是,当我用这个脚本刷新 Arduino 时,它根本不起作用。在我恢复无限循环的那一刻,一切都恢复正常(并且无休止地)工作。
作为参考,这里是 GitHub 项目的链接,我要编辑的文件是 Joystick.c:https://github.com/bertrandom/snowball-thrower
我有点迷茫,全神贯注地寻求建议。
谢谢!
【问题讨论】:
-
1.
for (day = 0; day <= 2; day++)将循环 三 次,而不是两次。 2. Arduino 有没有main函数?我以为他们使用了setup()和loop()? -
Arduino 是否具有主要功能? 这也让我感到困惑。必须使用一些备用 IDE
-
从 C++ 的角度来看,您的循环似乎没问题。尝试完全删除循环并查看它是否停止。如果是这样,罪魁祸首很可能是前两行之一
-
它根本不起作用是什么意思?如果你只运行循环两次,它可能在有时间做任何事情之前就关闭了。我好像你已经假设错误在循环中,而它很可能是在其余代码的工作方式中。
-
@Seifz:停止意味着它停止并且永远不会返回。如果这是您对“停止”的理解,则表示前两个功能之一存在问题。重复该过程以确定问题出在第一个函数还是第二个函数中。要进一步调试,您需要一种与您的软件交互的方法,否则您不会走得太远。我不太了解 Arduino,但也许有某种解码器,您可以写入一些 IO 以检查程序停止的位置。 (如果评论有帮助,你也可以顺便投票)