ジョイスティック切換えで色々な処理をやります。 C言語学習用です。

Dependencies:   C12832 MMA7660 mbed

src/main.cpp

Committer:
INTRA\mitsuru.suzuki
Date:
2018-04-11
Revision:
15:56e31e726469
Parent:
14:15447d4751c3
Child:
19:caab1538fa62

File content as of revision 15:56e31e726469:

#include "./mbed.h"
#include "C12832.h"

#include "commands.h"
#include "ringBuffer.h"

extern COMMAND_TRRIGER   commandList[];
extern COMMAND_TRRIGER   triggerNull;

BusIn joyStick(p15,p12,p13,p16,p14);
C12832 lcd(p5, p7, p6, p8, p11);

int main()
{
    lcd.cls();
    lcd.locate(0,3);
    lcd.printf("mbed Switches application");
    
    // デバイスの初期化
    for(int index = 0; commandList[index].trigger != nothing; index++) {
        commandList[index].command->initialize();
    }
    
    COMMAND_TRRIGER *current = &triggerNull;
    COMMAND_TRRIGER *previous = NULL;
    while(true) {
        
        // 要求の取得
        COMMAND_TRRIGER *request = NULL;
        for(int index = 0; commandList[index].trigger != nothing; index++) {
            if (joyStick == commandList[index].trigger) {
                if (previous != &commandList[index]) {
                    request = &commandList[index];
                    previous = request;
                }
            }
        }

        // 要求があったら
        if (request != NULL) {
            bool full = ringbufferPut(request);
            if (full) {
                printf("full\n");
            }
        }

        // 要求があって実行中のものと異なる場合
        if (request != NULL && request != current) {

            // コマンドの終了
            current->command->finalize();
            current = &triggerNull;
        }

        // 何も処理していない場合に
        if (current->trigger == nothing) {

            // 要求があったら
            if (request != NULL) {
                
                // コマンドを開始
                current = request;
                lcd.locate(0,15);
                lcd.printf("        ");
                lcd.locate(0,15);
                lcd.printf(current->command->name);
                current->command->processInitialize();
            }
        } else {
            
            // コマンドの実行
            current->command->processRunning();
            
            // コマンドが終わったら
            if (! current->command->processIsContinue()){

                // コマンドの終了
                current->command->finalize();
                current = &triggerNull;
            }
        }
    }
}