view main/robots/odr-display/buttons.cpp @ 235:8af48f7c2f34 main

Initial commit for the odr-display node.
author Bob Cook <bob@bobcookdev.com>
date Sat, 12 Jul 2014 13:20:14 -0700
parents
children 43f43ebe35df
line wrap: on
line source

// ----------------------------------------------------------------------------------------
//
//  Copyright (C) 2014 Bob Cook
//    
//  Bob Cook Development, Robotics Library
//  http://www.bobcookdev.com/rl/
// 
//  Button functions for the Outdoor Robot user interface board.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//
// ----------------------------------------------------------------------------------------

#include <avr/io.h>

#include "func.h"
#include "leds.h"

// ----------------------------------------------------------------------------------------

static const uint8_t DEBOUNCE_THRESHOLD = 10;

// ----------------------------------------------------------------------------------------

button_press_t g_current_state;
button_press_t g_transition_state;
uint8_t        g_transition_count;

// ----------------------------------------------------------------------------------------

void button_init()
{
    //---   The button (joystick) is on PORTB.3, PORTB.4, PORTC.4, PORTC.5, PORTC.6
    //      Enable the pull-ups.

    DDRB &= ~( ( 1 << PB4 ) | ( 1 << PB3 ) );
    DDRC &= ~( ( 1 << PC6 ) | ( 1 << PC5 ) | ( 1 << PC4 ) );

    PORTB |= ( 1 << PB4 ) | ( 1 << PB3 );
    PORTC |= ( 1 << PC6 ) | ( 1 << PC5 ) | ( 1 << PC4 );
}

// ----------------------------------------------------------------------------------------

static button_press_t determine_button_direction()
{
    if ( ( PINC & ( 1 << PC5 ) ) == 0 )
    {
        return button_center_e;
    }
    else if ( ( PINC & ( 1 << PC4 ) ) == 0 )
    {
        return button_up_e;
    }
    else if ( ( PINB & ( 1 << PB3 ) ) == 0 )
    {
        return button_down_e;
    }
    else if ( ( PINB & ( 1 << PB4 ) ) == 0 )
    {
        return button_left_e;
    }
    else if ( ( PINC & ( 1 << PC6 ) ) == 0 )
    {
        return button_right_e;
    }
    else
    {
        return button_not_pressed_e;
    }
}

// ----------------------------------------------------------------------------------------

void button_update()
{
    button_press_t dir = determine_button_direction();

    if ( dir == button_not_pressed_e )
    {
        led_blue_off();
    }
    else
    {
        led_blue_on();
    }

    if ( dir == g_current_state )
    {
        return;
    }

    if ( dir == g_transition_state )
    {
        if ( ++g_transition_count > DEBOUNCE_THRESHOLD )
        {
            g_current_state = g_transition_state;
        }
    }
    else if ( g_transition_count > DEBOUNCE_THRESHOLD / 2 )
    {
        --g_transition_count;
    }
    else
    {
        g_transition_state = dir;
        g_transition_count = 0;
    }
}

// ----------------------------------------------------------------------------------------

button_press_t button_state()
{
    return g_current_state;
}

// ----------------------------------------------------------------------------------------

bool button_is_pressed()
{
    return ( g_current_state != button_not_pressed_e );
}

// ----------------------------------------------------------------------------------------