Example #1
0
void setup()
{
    ebox_init();
    uart1.begin(9600);
    btn.begin();
    PB8.mode(OUTPUT_PP);
    PB9.mode(OUTPUT_PP);
    PB10.mode(OUTPUT_PP);
}
Example #2
0
VOID
render_do_lbuttonup(HWND hWnd, LONG wParam, LONG lParam)
{
    SKINDATA* skind = &m_current_skin;
    POINT pt = {LOWORD(lParam), HIWORD(lParam)};
    BUTTON *b;
    int i;

    for (i = 0; i < skind->m_num_buttons; i++)
    {
	b = &skind->m_buttons[i];
	if (PtInRect(&b->dest, pt) &&
	    b->enabled)
	{
	    b->mode = BTN_MODE_HOT;
	    b->clicked();
	}
	do_refresh(&b->dest);
    }
}
Example #3
0
void setup()
{
	ebox_init();

	PPS_PIN.mode(OUTPUT_PP);
	pps_off();
	uart1.begin(115200);
	led.begin();
	btn.begin();
	date_time.begin(115200);

}
Example #4
0
int main(void)
{
    setup();
    while(1)
    {
        btn.loop();
        if(btn.click())
        {
            PB8.write(!PB8.read());
            uart1.printf("\r\nclick event!");
        }
        if(btn.release())
        {
            PB9.write(!PB9.read());
            uart1.printf("\r\nrelease event!");
        }
        if(btn.pressed_for(2000,5))//长按两秒,执行5次
        {
            PB10.write(!PB10.read());
            uart1.printf("\r\nlong press event!");
        }
    }
}
void setup()
{
    Serial.begin(SERIAL_BAUD);
    
    trafficLight0.SetPins(
        PIN_GREEN_LIGHT_0, 
        PIN_ORANGE_LIGHT_0, 
        PIN_RED_LIGHT_0,
        PIN_FILTERTURN_LIGHT_0,
        PIN_PED_LIGHT_0,
        PIN_PEDCROSS_BUZZER_0
    );
    
    trafficLight90.SetPins(
        PIN_GREEN_LIGHT_90, 
        PIN_ORANGE_LIGHT_90, 
        PIN_RED_LIGHT_90,
        PIN_FILTERTURN_LIGHT_90,
        PIN_PED_LIGHT_90,
        PIN_PEDCROSS_BUZZER_90
    );
    
    trafficLight270.SetPins(
        PIN_GREEN_LIGHT_270, 
        PIN_ORANGE_LIGHT_270, 
        PIN_RED_LIGHT_270,
        PIN_FILTERTURN_LIGHT_270,
        PIN_PED_LIGHT_270,
        PIN_PEDCROSS_BUZZER_270
    );
    
    mainCounter.Reset();
    
    pot.SetPin(PIN_POT);
    pot.ApplySmoothing();
    
    ther.SetPin(PIN_THER);
    ther.SetFixedResistorR2(10000);
    ther.SetVIn(5);
    ther.ApplySmoothing();
    
    ldr.SetPin(PIN_LDR);
    ldr.SetFixedResistorR2(10000);
    ldr.SetVIn(5);
    ldr.ApplySmoothing();
    
    btnEmergency.SetPin(PIN_BTN_EMERGENCY);
    btnRoadWork.SetPin(PIN_BTN_ROADWORK);
    
    btnSelectSequence.SetPin(PIN_BTN_SELECTSEQUENCE);
    btnPedCross0.SetPin(PIN_BTN_PEDCROSS_0);
    btnPedCross270.SetPin(PIN_BTN_PEDCROSS_270);
    btnPedCross90.SetPin(PIN_BTN_PEDCROSS_90);
}
// Update normal light sequence by checking the main counter
int UpdateNormalSequence(int goLightTime) {     
    int i = 1;
    while (mainCounter.GetCounter() > i * goLightTime)
        i++;
    
    // On Select Sequence Button, cycle through the sequences immediately
    if (btnSelectSequence.IsPressed()) {
        // reset the main counter to start of new sequence
        mainCounter.Reset(i * goLightTime);
        i++;
    }
        
    // if maximum sequences has reached reset the main counter
    if (i == MAX_SEQUENCES + 1) {
        mainCounter.Reset();
        i = 1;
    }
    
    return i;
}
Example #7
0
int LEVELSELECT::Select()
{
    const char noDirectoryName[] = "[kein Verzeichnis]";

    BUTTON prevDirButton ({{10, windY - 100}, {90, windY - 50}}, 0, 3, 10, CYAN, "<", 20, YELLOW);
    BUTTON nextDirButton ({{windX - 100, windY - 100}, {windX - 10, windY - 50}}, 0, 3, 10, CYAN, ">", 20, YELLOW);
    BUTTON dirCaption ({{100, windY - 100}, {windX - 110, windY - 50}}, 3, 3, 10, CYAN, "(Directory)", 20, YELLOW);
    BUTTON prevButton ({{10, windY - 180}, {90, windY - 130}}, 0, 3, 10, CYAN, "<", 20, YELLOW);
    BUTTON nextButton ({{windX - 100, windY - 180}, {windX - 10, windY - 130}}, 0, 3, 10, CYAN, ">", 20, YELLOW);
    BUTTON selectButton ({{windX - 200, 10}, {windX - 10, 60}}, 0, 3, 10, CYAN, "Spiel starten", 20, YELLOW);
    BUTTON cancelButton ({{10, 10}, {190, 60}}, 0, 3, 10, CYAN, "Zurück", 20, YELLOW);
    BUTTON levelCaption ({{100, windY - 180}, {windX - 110, windY - 130}}, 3, 3, 10, CYAN, "(Levelname)", 20, YELLOW);
    AREA highscoreOutput {{100, 140}, {windX / 2 - 100, windY - 240}};
    prevDirButton.assignKeyboardButton(0,'Q');
    nextDirButton.assignKeyboardButton(0,'E');
    prevButton   .assignKeyboardButton(0,GLFW_KEY_LEFT ,'A');
    nextButton   .assignKeyboardButton(0,GLFW_KEY_RIGHT,'D');
    levelCaption .assignKeyboardButton(0,'S');
    cancelButton .assignKeyboardButton(0,GLFW_KEY_ESC);
    selectButton .assignKeyboardButton(0,GLFW_KEY_ENTER,GLFW_KEY_SPACE);

    ReloadHighscore();

    prepare_GameLoop();

    do
    {
        prepare_graphics();

        char levelText[MAX_PATH];
        if (GetLevel() != NULL)
            sprintf(levelText, "%s (%d/%d)", GetLevel()->name, GetLevel()->index + 1, GetLevelCount());
        else
            strcpy(levelText, "[Kein Level vorhanden]");

        levelCaption.setText(levelText);

        const char* directoryName;
        if (strlen(GetDirectory()->name) > 0)
            directoryName = GetDirectory()->name;
        else
            directoryName = noDirectoryName;

        char directoryText[strlen(directoryName) + 9];

        sprintf(directoryText, "%s (%d/%d)", directoryName, GetDirectory()->index + 1, GetDirectoryCount());
        dirCaption.setText(directoryText);

        //handle clicks
        if (cancelButton.clicked())
            break;
        else if (selectButton.clicked() && isInputValid())
        {
            currentLevel->score.setTimesPlayed(currentLevel->score.getTimesPlayed() + 1);
            return true;
        }
        else if (prevButton.clicked() || levelCaption.clicked() == 2)
            SwitchLevel(-1);
        else if (nextButton.clicked() || levelCaption.clicked() == 1)
            SwitchLevel(1);
        else if (prevDirButton.clicked() || dirCaption.clicked() == 2)
            SwitchDirectory(-1);
        else if (nextDirButton.clicked() || dirCaption.clicked() == 1)
            SwitchDirectory(1);

        //output
        prevDirButton.print();
        nextDirButton.print();
        dirCaption.print();
        prevButton.print();
        nextButton.print();
        selectButton.print();
        cancelButton.print();
        levelCaption.print();

        if (currentLevel != NULL)
        {
            drawBox({{windX/2 + 80, 140}, {windX - 110, windY - 240}}, 10, 3, WHITE);
            currentLevel->level->printPreview();

            TIME time = currentLevel->score.getTime();
            drawBox(highscoreOutput,10,3,WHITE);
            normalFont.printf({highscoreOutput.a.x + 20, highscoreOutput.b.y - 40}, taLEFT, "Highscore");
            normalFont.printf({highscoreOutput.a.x + 20, highscoreOutput.b.y - 80}, taLEFT, "Züge: %d", currentLevel->score.getMoves());
            normalFont.printf({highscoreOutput.a.x + 20, highscoreOutput.b.y - 120}, taLEFT, "Zeit: %02d:%02d:%05.2f", time.Hours, time.Minutes, time.Seconds);
            normalFont.printf({highscoreOutput.a.x + 20, highscoreOutput.b.y - 160}, taLEFT, "%d mal gespielt", currentLevel->score.getTimesPlayed());
        }
    } while (complete_graphics());

    logger(true, "LEVELSELECT: Cancelled, Returning false.");
    return false;
}
void loop() {
    delay(50);
    
    boolean hotDay = ther.GetKelvinTempUsingHart() >= THER_CRITICAL_DEG;
    boolean nightMode = ldr.ReadADC() <= LDR_DARK_ADC;
    
    static int goLightTimeCalibed = DEFAULT_GO_LIGHT_TIME;
    static int orangeLightTimeCalibed = DEFAULT_ORANGE_LIGHT_TIME;
    
    // if both emergency button and road work are pressed activate the calibration
    if (btnEmergency.IsPressed() && btnRoadWork.IsPressed()) {
        goLightTimeCalibed = map(pot.ReadADC(), 0, 1023, DEFAULT_GO_LIGHT_TIME/2, DEFAULT_GO_LIGHT_TIME * 2);
        orangeLightTimeCalibed = map(pot.ReadADC(), 0, 1023, DEFAULT_ORANGE_LIGHT_TIME/2, DEFAULT_ORANGE_LIGHT_TIME * 2);
    } 
    else {
        boolean emergencyMode = btnEmergency.ReadSwitch();
        boolean roadWorkMode = btnRoadWork.ReadSwitch();
        
        if (emergencyMode) {
            OnEmergencyMode();
        }
        
        if (roadWorkMode) {
            FlashForRoadWork();
        }
        
        // pause the main counter when one of the modes are activated
        if(roadWorkMode || emergencyMode) {
            mainCounter.Pause();
            return;
        }
    }
    
    int goLightTime = goLightTimeCalibed;
    int orangeLightTime = orangeLightTimeCalibed;
    double multiplier =  1.0;
    
    if (nightMode) { 
        multiplier += 0.4;
    }
    
    if(hotDay) {
        multiplier += 0.3;
    }
    
    // extended the go or green signal time and the orange light time
    int goLightTime = goLightTimeCalibed * multiplier;
    int orangeLightTime = orangeLightTimeCalibed * multiplier;
    
    // get the current state of the sequence
    int state = UpdateNormalSequence(goLightTime);
    
    // print out the state of the system for debugging
    Serial.print("Temperature: ");
    Serial.print(ther.GetKelvinTempUsingHart());
    Serial.print(" LDR: ");
    Serial.print(ldr.ReadADC());
    Serial.print(" PotADC: ");
    Serial.print(pot.ReadADC());
    Serial.print(" Cross: ");
    Serial.print(btnPedCross0.IsPressed() || btnPedCross90.IsPressed() || btnPedCross270.IsPressed());
        
    Serial.print(" main Counter: ");
    Serial.print(mainCounter.GetCounter());
    Serial.print(" goTime: ");
    Serial.print(goLightTime);
    Serial.print(" Orange: ");
    Serial.print(orangeLightTime);
    Serial.print(" state: ");   
    Serial.println(state);
        
    // if alarm is in progress, exit from normal sequence
    if (AddPedCrossStageOnRequest(state, goLightTime, orangeLightTime))
        return;
    
    // unpause the main counter if it was paused
    mainCounter.UnPause();
    
    // update the leds on the traffic light depending on the values in the sequence array
    trafficLight270.UpdateWithLightTransition(sequence[state - 1][0], sequence[state - 1][1], sequence[state - 1][2], orangeLightTime);
    trafficLight0.UpdateWithLightTransition(sequence[state - 1][3], sequence[state - 1][4], sequence[state - 1][5], orangeLightTime);
    trafficLight90.UpdateWithLightTransition(sequence[state - 1][6], sequence[state - 1][7], sequence[state - 1][8], orangeLightTime);
}
// function to allow for ped crossing on the press of a button
// ped crossing works by pausing the main counter at the end of a sequence 
// and resuming after the ped has crossed.
// returns whether alarm is currently on progression or not
bool AddPedCrossStageOnRequest(int state, int pedCrossTime, int orangeLightTime){
#define NO_PED_CROSS 0
#define PED_CROSS_BTN_PRESSED 1
#define PED_CROSS_IN_PROGRESS 2
    static int alarmState = 0;
    
    // check for button presses at all the junctions
    if(btnPedCross0.IsPressed() || btnPedCross90.IsPressed() || btnPedCross270.IsPressed())
        if (alarmState == NO_PED_CROSS) alarmState = PED_CROSS_BTN_PRESSED;
    
    // update the buzzer asyncronously
    trafficLight0.crossingBuzzer.UpdateAsync();
    trafficLight90.crossingBuzzer.UpdateAsync();
    trafficLight270.crossingBuzzer.UpdateAsync();
    
    static int prevState = 0;
    static Counter pedCrossCounter;
    
    // check if the current sequence has ended
    if (prevState != state && alarmState == PED_CROSS_BTN_PRESSED) {
        // pause the timer
        mainCounter.Pause();
        // reset the crossing counter to 0
        pedCrossCounter.Reset();
        // activate the flag to transition all the green signal to red first
        alarmState = PED_CROSS_IN_PROGRESS; 
        return true;
    }

    if (alarmState == PED_CROSS_IN_PROGRESS) {
        if (pedCrossCounter.GetCounter() <= pedCrossTime) {
                // if there were no green lights from the previous sequence, directly turn on the red light
                if (trafficLight0.ledRed.IsOn() && trafficLight90.ledRed.IsOn() && trafficLight270.ledRed.IsOn())
                {
                    trafficLight270.UpdateWithLightTransition(0, 0, 1, orangeLightTime);
                    trafficLight0.UpdateWithLightTransition(0, 0, 1, orangeLightTime);
                    trafficLight90.UpdateWithLightTransition(0, 0, 1, orangeLightTime);
                    trafficLight0.crossingBuzzer.TriggerAlarm();
                    trafficLight90.crossingBuzzer.TriggerAlarm();
                    trafficLight270.crossingBuzzer.TriggerAlarm();
                }
                // if there were green lights on the previous sequence, transition to red first
                else {
                    trafficLight270.UpdateWithLightTransition(0, 0, 0, orangeLightTime);
                    trafficLight0.UpdateWithLightTransition(0, 0, 0, orangeLightTime);
                    trafficLight90.UpdateWithLightTransition(0, 0, 0, orangeLightTime);
                    trafficLight0.crossingBuzzer.TurnOffAlarm();
                    trafficLight90.crossingBuzzer.TurnOffAlarm();
                    trafficLight270.crossingBuzzer.TurnOffAlarm();
                }
                return true;
        } 
        // if ped cross time has finish blink the ped light as in australian ped crossing
        else if (pedCrossCounter.GetCounter() <= pedCrossTime + orangeLightTime) {
            FlashForPedCrossFinish();
            trafficLight0.crossingBuzzer.TurnOffAlarm();
            trafficLight90.crossingBuzzer.TurnOffAlarm();
            trafficLight270.crossingBuzzer.TurnOffAlarm();
            return true;
        } 
        else {
            alarmState = NO_PED_CROSS;
        }
    }
    prevState = state;
    return false;
}