ヌンチャクでコントロールするチョロQ機能に限定した物です。

Fork of StarBoardOrangeExample3 by Shinichiro Nakamura

main.cpp

Committer:
shintamainjp
Date:
2010-08-24
Revision:
0:127b9ca59547
Child:
1:03c8bc666945

File content as of revision 0:127b9ca59547:

/**
 * StarBoard Orange - Example application No.3 (Version 0.0.1)
 * Drive a CHORO Q HYBRID with wii nunchuk
 *
 * See also ... http://mbed.org/users/shintamainjp/notebook/starboard_example3_ja/
 * See also ... http://mbed.org/users/shintamainjp/notebook/starboard_example3_en/
 *
 * Copyright (C) 2010 Shinichiro Nakamura (CuBeatSystems)
 * http://shinta.main.jp/
 */
 
#include <mbed.h>
#include <algorithm>
#include <RemoteIR.h>
#include <TransmitterIR.h>
#include <I2CConfig.h>
#include <WiiNunchuckReader.h>
#include <TextLCD.h>
#include "ChoroQ.h"

WiiNunchuckReader wn(I2CPort_A::SDA, I2CPort_A::SCL);
ChoroQ cq(p21, ChoroQ::ChC);
TextLCD lcd(p24, p26, p27, p28, p29, p30);
BusOut led(LED4, LED3, LED2, LED1);

ChoroQ::Action getAction(const int x, const int y, bool dash) {
    static const int MAX_X = 200;
    static const int MIN_X = 20;
    static const int MAX_Y = 200;
    static const int MIN_Y = 20;
    int px = ((x - MIN_X) * 100) / (MAX_X - MIN_X);
    int py = ((y - MIN_Y) * 100) / (MAX_Y - MIN_Y);
    px = std::max(0, std::min(100, px)) - 50;   // Range of a value is -50 to +50
    py = std::max(0, std::min(100, py)) - 50;   // Range of a value is -50 to +50

    if ((std::abs(px) <= 10) && (std::abs(py) <= 10)) {
        return ChoroQ::Stop;
    }

    if (std::abs(px) < 10) {
        if (py < 0) {
            if (dash) {
                return ChoroQ::DownDash;
            } else {
                return ChoroQ::Down;
            }
        } else {
            if (dash) {
                return ChoroQ::UpDash;
            } else {
                return ChoroQ::Up;
            }
        }
    }
    if (std::abs(py) < 10) {
        if (px < -20) {
#if 0
            return ChoroQ::Left;
#else
        if (dash) {
            return ChoroQ::UpLeftDash;
        } else {
            return ChoroQ::UpLeft;
        }
#endif
        }
        if (20 < px) {
#if 0
            return ChoroQ::Right;
#else
        if (dash) {
            return ChoroQ::UpRightDash;
        } else {
            return ChoroQ::UpRight;
        }
#endif
        }
    }
    if ((10 < px) && (10 < py)) {
        if (dash) {
            return ChoroQ::UpRightDash;
        } else {
            return ChoroQ::UpRight;
        }
    }
    if ((px < -10) && (10 < py)) {
        if (dash) {
            return ChoroQ::UpLeftDash;
        } else {
            return ChoroQ::UpLeft;
        }
    }
    if ((px < -10) && (py < -10)) {
        if (dash) {
            return ChoroQ::DownLeftDash;
        } else {
            return ChoroQ::DownLeft;
        }
    }
    if ((10 < px) && (py < -10)) {
        if (dash) {
            return ChoroQ::DownRightDash;
        } else {
            return ChoroQ::DownRight;
        }
    }
    return ChoroQ::Stop;
}

int main() {
    lcd.cls();
    while (true) {
        wn.RequestRead();

        ChoroQ::Action ac = getAction(wn.getJoyX(), wn.getJoyY(), (wn.getButtonZ() == 1) ? true : false);
        cq.execute(ac);

        lcd.locate(0, 0);
        lcd.printf("(%3d,%3d) %-6.6s", wn.getJoyX(), wn.getJoyY(), ((wn.getButtonZ() == 1) ? "-Dash-" : "      "));

        lcd.locate(0, 1);
        switch (ac) {
            case ChoroQ::Undef:
                lcd.printf("%-16.16s", "Undef");
                break;
            case ChoroQ::Up:
                lcd.printf("%-16.16s", "Up");
                break;
            case ChoroQ::Down:
                lcd.printf("%-16.16s", "Down");
                break;
            case ChoroQ::Left:
                lcd.printf("%-16.16s", "Left");
                break;
            case ChoroQ::Right:
                lcd.printf("%-16.16s", "Right");
                break;
            case ChoroQ::UpDash:
                lcd.printf("%-16.16s", "UpDash");
                break;
            case ChoroQ::UpLeft:
                lcd.printf("%-16.16s", "UpLeft");
                break;
            case ChoroQ::UpRight:
                lcd.printf("%-16.16s", "UpRight");
                break;
            case ChoroQ::UpRightDash:
                lcd.printf("%-16.16s", "UpRightDash");
                break;
            case ChoroQ::UpLeftDash:
                lcd.printf("%-16.16s", "UpLeftDash");
                break;
            case ChoroQ::DownLeft:
                lcd.printf("%-16.16s", "DownLeft");
                break;
            case ChoroQ::DownRight:
                lcd.printf("%-16.16s", "DownRight");
                break;
            case ChoroQ::DownDash:
                lcd.printf("%-16.16s", "DownDash");
                break;
            case ChoroQ::DownLeftDash:
                lcd.printf("%-16.16s", "DownLeftDash");
                break;
            case ChoroQ::DownRightDash:
                lcd.printf("%-16.16s", "DownRightDash");
                break;
            case ChoroQ::Stop:
                lcd.printf("%-16.16s", "Stop");
                break;
        }

        wait_ms(50);
        led = led + 1;
    }

    return EXIT_SUCCESS;
}