[英]How to make what function I use in c be controlled by a variable
這是我第一年的vex.
我擔任程序員。 我有這個想法可以快速自主創建,記錄驅動程序。 代替通常的功率級原始流的數組/調試器轉儲,我想到了從驅動程序移動中提取函數的想法。 我不會詳細介紹,我可以自己編寫代碼,但是我需要一些幫助。
由於缺乏編碼經驗,我無法做一件事。
我想創建一個for循環,逐個檢查每個操縱桿按鈕。
例如:
struct button
{
bool pressed;
}
for(int i = 0; i>12; i++) //12 is number of buttons on the joystick
{
struct button button<cycle through buttons>;
}
我希望在那里:
struct button button6U;
struct button button6D;
struct button button6R;
etc.
然后,我想要這個:
for(int i = 0; i>12; i++) // 12 is number of buttons on the joystick
{
if(VexRT[<currentButton>])
{
button<currentButton>.pressed = true;
}
}
我不知道如何使用通配符修改我要寫入的實際變量名來執行此操作。
有兩個想法:for語句不知道如何提高操縱桿按鈕的順序。 所以我想我可能需要的是:
orderOfButtons
{
VexRT[6U];
VexRT[6D];
VexRT[6R];
// etc.
}
我似乎無法弄清楚如何使用一個變量定義我正在讀取的VexRT[]
按鈕。
任何幫助,將不勝感激! 謝謝。
因此,您想要的是對用戶輸入進行采樣(以指定的速率),然后將其記錄到數組中,然后在以后播放? 如果您具有驅動VEX的函數(我不熟悉),每個函數都與輸入關聯,則可以使用函數指針數組來創建輸出。
#define MAX_ACTION 12
#define MAX_RECORDED 200
// Declare your array of function pointers
int (*action[MAX_ACTION])(void);
// Declare your stored array of recorded actions
int recorded[MAX_RECORDED];
...
// Assign function pointers to associated functions
action[0] = go_forward;
action[1] = turn_right;
...
// Record your actions into some array
while (...)
{
// Record the action
recorded[i++] = get_action();
// Sample delay
}
...
// Playback the actions
for (i=0;i<RECORDED;i++)
{
(*action[recorded[i]])();
// delay here
}
PS您的for循環向后(i <12不是i> 12)。
聽起來像您想要一個數組:
#define NUMBER_OF_BUTTONS 12
...
struct button VexRT[NUMBER_OF_BUTTONS];
如果要使用符號常量來引用數組中的特定按鈕,則可以使用枚舉:
enum btn_id { BTN_6U, // maps to 0
BTN_6D, // maps to 1
BTN_6R, // maps to 2
...
}
枚舉常量用整數表示,默認情況下,它們從0開始,以1遞增。如果需要,可以將它們初始化為不同的值,並且多個枚舉常量可以映射為相同的值。 當我要標識用於循環的“第一個”和“最后一個”枚舉時,可以利用這一點,例如:
enum btn_id {
BTN_6U,
BTN_FIRST = BTN_6U, // both BTN_FIRST and BTN_6U will map to 0
BTN_6D,
BTN_6R,
...
BTN_whatever,
BTN_LAST
};
因此, VexRT[BTN_6U]
映射到VexRT[0]
, VexRT[BTN_6D]
映射到VexRT[1]
, VexRT[1]
。
請注意,通過這種方式,您不必遍歷所有按鈕即可設置一個按鈕:
enum btn_id currentButton = BTN_6D;
...
VexRT[currentButton].pressed = true;
如果確實要遍歷整個集合,則可以使用
for ( enum btn_id i = BTN_FIRST; i < BTN_LAST; i++ )
{
VexRT[i].pressed = false;
}
我認為您正在嘗試訪問來自操縱桿的事件。 您可以循環遍歷值數組並記錄它們。 我認為操縱桿上的通道很容易訪問,例如:vexRT [x]其中x為1-12。 如果您只想存儲每個通道的最新值,則可以執行以下操作:
int value[12];
for(i=0; i<12; i++)
{
value[i] = vexRT[i];
}
如果要存儲所有值以便可以映射它們或對其進行回放或其他操作,則將需要一個更復雜的數據結構來存儲它們,例如值數組列表。
我還找到了說明說可以通過vexRT [Chx]來訪問值的文檔,其中x為1-12,因此您可以選擇創建一個字符串並使用它來訪問循環內的操縱桿通道:
string *s = (char *)malloc(5*sizeof(char)); //5 is the max length of the null terminated string
for() . . . {
sprintf(s,"Ch%d", i);
value[i] = vertRT[s];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.