
19 changed files with 147 additions and 63 deletions
@ -0,0 +1,42 @@ |
|||
#pragma once |
|||
|
|||
#include "non_copyable.hpp" |
|||
#include "mcp23017.hpp" |
|||
|
|||
class PressKeys : NonCopyable |
|||
{ |
|||
public: |
|||
enum class Key : uint8_t { KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5 }; |
|||
|
|||
PressKeys(MCP23017 & _mcp) : mcp(_mcp) {} |
|||
bool setup(); |
|||
|
|||
/**
|
|||
* @brief Read a specific presskey |
|||
* |
|||
* Read one of the six presskeys. |
|||
* |
|||
* @param key presskey (KEY_0, KEY_1, or KEY_2) |
|||
* @return uint8_t 1 if pressed, 0 if not |
|||
*/ |
|||
uint8_t read_key(Key key); |
|||
|
|||
/**
|
|||
* @brief Read all keys |
|||
* |
|||
* @return uint8_t keys bitmap (bit = 1 if pressed) keys 0..5 are mapped to bits 0 to 5. |
|||
*/ |
|||
uint8_t read_all_keys(); |
|||
|
|||
private: |
|||
static constexpr char const * TAG = "PressKeys"; |
|||
MCP23017 & mcp; |
|||
|
|||
const MCP23017::Pin PRESS_0 = MCP23017::Pin::IOPIN_10; |
|||
const MCP23017::Pin PRESS_1 = MCP23017::Pin::IOPIN_11; |
|||
const MCP23017::Pin PRESS_2 = MCP23017::Pin::IOPIN_12; |
|||
const MCP23017::Pin PRESS_3 = MCP23017::Pin::IOPIN_13; |
|||
const MCP23017::Pin PRESS_4 = MCP23017::Pin::IOPIN_14; |
|||
const MCP23017::Pin PRESS_5 = MCP23017::Pin::IOPIN_15; |
|||
|
|||
}; |
@ -0,0 +1,52 @@ |
|||
#define __PRESS_KEYS__ 1 |
|||
#include "press_keys.hpp" |
|||
#include "wire.hpp" |
|||
|
|||
bool |
|||
PressKeys::setup() |
|||
{ |
|||
Wire::enter(); |
|||
mcp.set_direction(PRESS_0, MCP23017::PinMode::INPUT_PULLUP); |
|||
mcp.set_direction(PRESS_1, MCP23017::PinMode::INPUT_PULLUP); |
|||
mcp.set_direction(PRESS_2, MCP23017::PinMode::INPUT_PULLUP); |
|||
mcp.set_direction(PRESS_3, MCP23017::PinMode::INPUT_PULLUP); |
|||
mcp.set_direction(PRESS_4, MCP23017::PinMode::INPUT_PULLUP); |
|||
mcp.set_direction(PRESS_5, MCP23017::PinMode::INPUT_PULLUP); |
|||
|
|||
// Prepare the MCP device to allow for interrupts
|
|||
// coming from any of the presskeys. Interrupts will be raised
|
|||
// for any change of state of the 6 presskeys. The GPIO_NUM_34
|
|||
// must be programmed as per the ESP-IDF documentation to get
|
|||
// some interrupts.
|
|||
|
|||
mcp.set_int_pin(PRESS_0, MCP23017::IntMode::FALLING); |
|||
mcp.set_int_pin(PRESS_1, MCP23017::IntMode::FALLING); |
|||
mcp.set_int_pin(PRESS_2, MCP23017::IntMode::FALLING); |
|||
mcp.set_int_pin(PRESS_3, MCP23017::IntMode::FALLING); |
|||
mcp.set_int_pin(PRESS_4, MCP23017::IntMode::FALLING); |
|||
mcp.set_int_pin(PRESS_5, MCP23017::IntMode::FALLING); |
|||
|
|||
mcp.set_int_output(MCP23017::IntPort::INTPORTB, false, false, MCP23017::SignalLevel::HIGH); |
|||
Wire::leave(); |
|||
|
|||
return true; |
|||
} |
|||
|
|||
uint8_t |
|||
PressKeys::read_all_keys() |
|||
{ |
|||
Wire::enter(); |
|||
uint16_t value = mcp.get_ports(); |
|||
Wire::leave(); |
|||
return ((value >> 10) & 0x3F) ^ 0x3F; |
|||
} |
|||
|
|||
uint8_t |
|||
PressKeys::read_key(Key key) |
|||
{ |
|||
Wire::enter(); |
|||
MCP23017::SignalLevel value = mcp.digital_read((MCP23017::Pin)(((uint8_t)key) + 10)); // Not clean
|
|||
Wire::leave(); |
|||
|
|||
return value == MCP23017::SignalLevel::HIGH ? 0 : 1; |
|||
} |
Loading…
Reference in new issue