[ion] Add a class State in Keyboard

Change-Id: I42b2c892deaedc83a7629d8c8be66cd4e1fbcb5c
This commit is contained in:
Émilie Feral
2017-05-23 10:44:30 +02:00
parent 3d1fc8e5d1
commit f9a1caa8eb
9 changed files with 42 additions and 16 deletions

View File

@@ -39,7 +39,7 @@ AppsContainer::AppsContainer() :
bool AppsContainer::poincareCircuitBreaker(const Poincare::Expression * e) {
Ion::Keyboard::State state = Ion::Keyboard::scan();
return Ion::Keyboard::keyDown(Ion::Keyboard::Key::A6, state);
return state.keyDown(Ion::Keyboard::Key::A6);
}
int AppsContainer::numberOfApps() {

View File

@@ -30,8 +30,7 @@ bool KeyboardTestController::handleEvent(Ion::Events::Event event) {
m_LEDColorIndex = 0;
}
updateBatteryState(Ion::Battery::voltage(), Ion::Battery::isCharging());
int shift = (uint8_t)Ion::Keyboard::ValidKeys[m_view.keyboardView()->testedKeyIndex()];
Ion::Keyboard::State onlyKeyDown = (uint64_t)1 << shift;
Ion::Keyboard::State onlyKeyDown = Ion::Keyboard::State(Ion::Keyboard::ValidKeys[m_view.keyboardView()->testedKeyIndex()]);
if (state == onlyKeyDown) {
m_view.keyboardView()->setTestedKeyIndex(m_view.keyboardView()->testedKeyIndex()+1);
if (m_view.keyboardView()->testedKeyIndex() == Ion::Keyboard::NumberOfValidKeys) {

View File

@@ -18,4 +18,5 @@ objs += $(addprefix ion/src/shared/, \
tests += $(addprefix ion/test/,\
crc32.cpp\
events.cpp\
keyboard.cpp\
)

View File

@@ -31,15 +31,27 @@ constexpr Key ValidKeys[] = {
constexpr int NumberOfKeys = 54;
constexpr int NumberOfValidKeys = 46;
typedef uint64_t State;
static_assert(sizeof(State)*8>NumberOfKeys, "Ion::Keyboard::State cannot hold a keyboard snapshot");
class State {
public:
constexpr State(uint64_t s = 0) :
m_bitField(s)
{}
explicit constexpr State(Key k) :
m_bitField((uint64_t)1 << (uint8_t)k)
{}
inline bool keyDown(Key k) const {
return (m_bitField>>(uint8_t)k) & 1;
}
operator uint64_t() const { return m_bitField; }
private:
uint64_t m_bitField;
};
State scan();
inline bool keyDown(Key k, State s) {
return (s>>(uint8_t)k) & 1;
}
static_assert(sizeof(State)*8>NumberOfKeys, "Ion::Keyboard::State cannot hold a keyboard snapshot");
}
}

View File

@@ -14,7 +14,7 @@ void Keyboard(const char * input) {
char result[9+Ion::Keyboard::NumberOfKeys+1] = { 'K', 'E', 'Y', 'B', 'O', 'A', 'R', 'D', '=' };
Ion::Keyboard::State state = Ion::Keyboard::scan();
for (uint8_t i=0; i<Ion::Keyboard::NumberOfKeys; i++) {
result[9+i] = Ion::Keyboard::keyDown((Ion::Keyboard::Key)i, state) ? '1' : '0';
result[9+i] = state.keyDown((Ion::Keyboard::Key)i) ? '1' : '0';
}
result[9+Ion::Keyboard::NumberOfKeys] = 0;
reply(result);

View File

@@ -49,7 +49,7 @@ namespace Ion {
namespace Keyboard {
State scan() {
State state = 0;
uint64_t state = 0;
for (uint8_t i=0; i<Device::numberOfRows; i++) {
/* In open-drain mode, a 0 in the register drives the pin low, and a 1 lets
@@ -77,7 +77,7 @@ State scan() {
* whatever value we return. */
state = state & 0x1F7DF7FFFFF0FF;
return state;
return State(state);
}
}

View File

@@ -14,7 +14,7 @@ void Ion::Power::suspend(bool checkIfPowerKeyReleased) {
bool isPowerDown = true;
while (isPowerDown) {
Keyboard::State scan = Keyboard::scan();
isPowerDown = Keyboard::keyDown(Keyboard::Key::B2, scan);
isPowerDown = scan.keyDown(Keyboard::Key::B2);
}
}
Device::shutdownPeripherals();
@@ -56,7 +56,7 @@ void Ion::Power::suspend(bool checkIfPowerKeyReleased) {
Keyboard::State scan = Keyboard::scan();
Keyboard::Device::shutdown();
Ion::Keyboard::State OnlyPowerKeyDown = ((uint64_t)1 << (uint8_t)Keyboard::Key::B2);
Ion::Keyboard::State OnlyPowerKeyDown = Keyboard::State(Keyboard::Key::B2);
if (scan == OnlyPowerKeyDown) {
// Wake up
break;

View File

@@ -63,7 +63,7 @@ static bool sleepWithTimeout(int duration, int * timeout) {
}
Event sLastEvent = Events::None;
Keyboard::State sLastKeyboardState = 0;
Keyboard::State sLastKeyboardState;
bool sEventIsRepeating = 0;
constexpr int delayBeforeRepeat = 200;
constexpr int delayBetweenRepeat = 50;
@@ -76,8 +76,8 @@ Event getEvent(int * timeout) {
assert(*timeout > delayBeforeRepeat);
assert(*timeout > delayBetweenRepeat);
int time = 0;
Keyboard::State keysSeenUp = 0;
Keyboard::State keysSeenTransitionningFromUpToDown = 0;
uint64_t keysSeenUp = 0;
uint64_t keysSeenTransitionningFromUpToDown = 0;
while (true) {
Keyboard::State state = Keyboard::scan();
keysSeenUp |= ~state;

14
ion/test/keyboard.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include <quiz.h>
#include <ion.h>
#include <assert.h>
using namespace Ion::Keyboard;
QUIZ_CASE(ion_keyboard) {
for (Key k : ValidKeys) {
for (Key l : ValidKeys) {
assert(State(k).keyDown(l) == (k == l));
}
}
}