mirror of https://github.com/probonopd/MiniDexed
parent
85a4d9953b
commit
2c614f532a
@ -0,0 +1,132 @@ |
||||
//
|
||||
// uibuttons.cpp
|
||||
//
|
||||
// MiniDexed - Dexed FM synthesizer for bare metal Raspberry Pi
|
||||
// Copyright (C) 2022 The MiniDexed Team
|
||||
//
|
||||
// Original author of this class:
|
||||
// R. Stange <rsta2@o2online.de>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
#include "uibuttons.h" |
||||
#include <circle/logger.h> |
||||
#include <assert.h> |
||||
|
||||
LOGMODULE ("uibuttons"); |
||||
|
||||
CUIButton::CUIButton (unsigned nPin) |
||||
: m_Pin (nPin, GPIOModeInputPullUp), |
||||
m_nLastValue (0) |
||||
{ |
||||
} |
||||
|
||||
CUIButton::~CUIButton (void) |
||||
{ |
||||
} |
||||
|
||||
boolean CUIButton::Initialize (void) |
||||
{ |
||||
return TRUE; |
||||
} |
||||
|
||||
#define DEBOUNCER 50 |
||||
|
||||
boolean CUIButton::Read (void) |
||||
{ |
||||
unsigned nValue = m_Pin.Read(); |
||||
|
||||
if (nValue != 0) |
||||
{ |
||||
// Some simple debouncing...
|
||||
if (m_nLastValue < DEBOUNCER) |
||||
{ |
||||
m_nLastValue++; |
||||
} |
||||
else if (m_nLastValue == DEBOUNCER) |
||||
{ |
||||
m_nLastValue++; |
||||
return TRUE; |
||||
} |
||||
else |
||||
{ |
||||
// Do nothing until reset/cleared
|
||||
} |
||||
} |
||||
else |
||||
{ |
||||
m_nLastValue = 0; |
||||
} |
||||
|
||||
return FALSE; |
||||
} |
||||
|
||||
|
||||
CUIButtons::CUIButtons (unsigned nLeftPin, unsigned nRightPin, unsigned nUpPin, unsigned nDownPin, unsigned nSelectPin) |
||||
: m_LeftButton (nLeftPin), |
||||
m_RightButton (nRightPin), |
||||
m_UpButton (nUpPin), |
||||
m_DownButton (nDownPin), |
||||
m_SelectButton (nSelectPin) |
||||
{ |
||||
} |
||||
|
||||
CUIButtons::~CUIButtons (void) |
||||
{ |
||||
} |
||||
|
||||
boolean CUIButtons::Initialize (void) |
||||
{ |
||||
return TRUE; |
||||
} |
||||
|
||||
void CUIButtons::RegisterEventHandler (TBtnEventHandler *pHandler, void *pParam) |
||||
{ |
||||
assert (!m_pEventHandler); |
||||
m_pEventHandler = pHandler; |
||||
assert (m_pEventHandler); |
||||
m_pEventParam = pParam; |
||||
} |
||||
|
||||
void CUIButtons::Update (void) |
||||
{ |
||||
assert (m_pEventHandler); |
||||
|
||||
if (m_LeftButton.Read ()) |
||||
{ |
||||
LOGNOTE ("Left"); |
||||
(*m_pEventHandler) (BtnEventLeft, m_pEventParam); |
||||
} |
||||
if (m_RightButton.Read ()) |
||||
{ |
||||
LOGNOTE ("Right"); |
||||
(*m_pEventHandler) (BtnEventRight, m_pEventParam); |
||||
} |
||||
if (m_UpButton.Read ()) |
||||
{ |
||||
LOGNOTE ("Up"); |
||||
(*m_pEventHandler) (BtnEventUp, m_pEventParam); |
||||
} |
||||
if (m_DownButton.Read ()) |
||||
{ |
||||
LOGNOTE ("Down"); |
||||
(*m_pEventHandler) (BtnEventDown, m_pEventParam); |
||||
} |
||||
if (m_SelectButton.Read ()) |
||||
{ |
||||
LOGNOTE ("Select"); |
||||
(*m_pEventHandler) (BtnEventSelect, m_pEventParam); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,82 @@ |
||||
//
|
||||
// uibuttons.h
|
||||
//
|
||||
// MiniDexed - Dexed FM synthesizer for bare metal Raspberry Pi
|
||||
// Copyright (C) 2022 The MiniDexed Team
|
||||
//
|
||||
// Original author of this class:
|
||||
// R. Stange <rsta2@o2online.de>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
//
|
||||
#ifndef _uibuttons_h |
||||
#define _uibuttons_h |
||||
|
||||
#include <circle/gpiopin.h> |
||||
#include <circle/types.h> |
||||
#include "config.h" |
||||
|
||||
class CUIButton |
||||
{ |
||||
public: |
||||
CUIButton (unsigned nPin); |
||||
~CUIButton (void); |
||||
|
||||
boolean Initialize (void); |
||||
|
||||
boolean Read (void); |
||||
|
||||
private: |
||||
CGPIOPin m_Pin; |
||||
unsigned m_nLastValue; |
||||
}; |
||||
|
||||
|
||||
class CUIButtons |
||||
{ |
||||
public: |
||||
enum TBtnEvent |
||||
{ |
||||
BtnEventLeft, |
||||
BtnEventRight, |
||||
BtnEventUp, |
||||
BtnEventDown, |
||||
BtnEventSelect, |
||||
BtnEventUnknown |
||||
}; |
||||
|
||||
typedef void TBtnEventHandler (TBtnEvent Event, void *pParam); |
||||
|
||||
public: |
||||
CUIButtons (unsigned nLeftPin, unsigned nRightPin, unsigned nUpPin, unsigned nDownPin, unsigned nSelectPin); |
||||
~CUIButtons (void); |
||||
|
||||
boolean Initialize (void); |
||||
|
||||
void RegisterEventHandler (TBtnEventHandler *pHandler, void *pParam = 0); |
||||
|
||||
void Update (void); |
||||
|
||||
private: |
||||
CUIButton m_LeftButton; |
||||
CUIButton m_RightButton; |
||||
CUIButton m_UpButton; |
||||
CUIButton m_DownButton; |
||||
CUIButton m_SelectButton; |
||||
|
||||
TBtnEventHandler *m_pEventHandler; |
||||
void *m_pEventParam; |
||||
}; |
||||
|
||||
#endif |
Loading…
Reference in new issue