私は SDCC 3.4 を使用しており、これは楽器の MIDI プロジェクトであり、これに数日間苦労しています... 説明するのが難しいとさえ思っているので、ここで私はより良いものを作ろうとしています例。基本的に、ボタンの押下をスキャンし、MIDI 情報を送信し、それに応じて LED を点灯させます。私が必要としているのは、各ボタンに関連するすべてのデータを保持する一種のデータベースです。その一部は一定である必要があり (ボタンと LED の ID)、一部はユーザーが変更できるため可変にすることができます。初期化フェーズでは、定数部分を構造体に割り当て、変数部分を変更しないでおく必要があります。ユーザーがボタンの機能を変更するとき、変数部分を上書きし、定数部分を変更しないでおく必要があります。
// A structure for the constant part
typedef struct
{
unsigned char btnID; // this holds the n. of input pin for the button
unsigned char ledID; // this holds the n. of output pin for the LED
} sBtnConst;
// A structure for the variable part
typedef struct
{
unsigned char CCnum; // this holds the CC number to send
unsigned char CCval; // this holds the CC value tu send
} sBtnVar;
// A structure containing all data
typedef struct
{
sBtnConst c;
sBtnVar v;
} sButton;
// Declare an array of button structures
// These will contain both the constant and the variable data
sButton Button[4];
// Now initialize a constant structure for the constant part
const sBtnConst cBtnDefinitions[4] =
{
{ 15, 0 },
{ 14, 1 },
{ 10, 8 },
{ 12, 5 },
};
今、私がする必要があるのは、の内容全体を to にコピーすることcBtnDefinitions[]
ですButton[]->c
。私が行った場合
memcpy(&Button->c, &cBtnDefinitions, sizeof(cBtnDefinitions));
データは、メンバー c だけでなく、c と v にも順次コピーされます。
メイン loop() の残りのコードは次のようになります。
void doButton(sButton *btn, unsigned char value)
{
LitLED(btn->c.ledID, !value);
SendMidiCC(btn->v.CCnum, btn->v.CCval);
}
// This is a callback function called every time a button has been pushed
void aButtonHasBeenPushed(unsigned char ID, unsigned char value)
{
unsigned char i;
for (i=0; i<NUM_OF_BUTTONS; ++i)
if (i == Button[i].c.btnID)
doButton(&Button[i], value);
}
もちろん、さまざまな種類のボタンを使用できるので、sButton 構造を他の目的に使用して、それらをすべて同じ関数で処理することができます。