commit 855cde0d1061ddf3952710da281e33fb44a0320c
parent 6c15dbb7f2084763dadae426c519fb4b534b1907
Author: Henry Wilson <m3henry@googlemail.com>
Date: Sun, 11 Feb 2018 17:05:07 +0000
Refactored HID module and provided better test
Diffstat:
5 files changed, 75 insertions(+), 35 deletions(-)
diff --git a/hidModule.cpp b/hidModule.cpp
@@ -1,39 +1,62 @@
#include "hidModule.h"
#include "ctrl.h"
+#include <util/delay.h>
-void hid::test()
+void hid::init()
{
- ctrl::disableJTAG();
-
io::direction::B() = ledRED;
- io::direction::C() = 0;
- io::out::C() = 0xFF;
+ io::out::D() = wheelCOM | buttonCOM;
+ io::direction::D() = wheelCOM | buttonCOM | ledYEL | ledGRN;
+}
+
+uint8_t hid::sampleInput()
+{
+ constexpr uint8_t maskC = 0b00111100;
+ constexpr uint8_t maskD = wheelCOM | buttonCOM;
- uint8_t stateD = 0;
- io::out::D() = buttonCOM;
- io::direction::D() = buttonCOM | wheelCOM | ledYEL | ledGRN;
+ const auto saveCdir = io::direction::C();
+ io::direction::C() = saveCdir & ~maskC;
+
+ const auto saveCout = io::out::C();
+ io::out::C() = saveCout | maskC;
+ io::out::D() = mix(io::out::D(), maskD, wheelCOM);
+
+ _delay_us(1);
+ auto buttons = (maskC & ~io::pin::C()) << 1;
+ io::pin::D() = maskD;
+
+ _delay_us(1);
+ buttons |= (maskC & ~io::pin::C()) >> 2;
+
+ io::out::C() = saveCout;
+ io::direction::C() = saveCdir;
+
+ return buttons;
+}
+
+void hid::setLeds(const uint8_t val)
+{
+ io::out::B() = mix(io::out::B(), ledRED, val);
+ io::out::D() = mix(io::out::D(), ledYEL | ledGRN, val);
+}
+
+void hid::test()
+{
+ ctrl::disableJTAG();
+
+ init();
for (;;)
{
- auto val = ~io::pin::C();
- io::out::D() = wheelCOM | stateD;
- auto r = (val << 3) & ledRED;
- auto a = (val << 2) & ledYEL;
- auto b = (val << 3) & ledGRN;
-
- io::out::B() = r;
- io::out::D() = wheelCOM | (stateD = a | b);
+ auto val = sampleInput();
- auto butts = ~io::pin::C();
- io::out::D() = buttonCOM | stateD;
+ const auto r = (val & (buttonL | wheelA)) ? ledRED : 0;
+ const auto y = (val & (buttonD | buttonU)) ? ledYEL : 0;
+ const auto g = (val & (buttonR | wheelB)) ? ledGRN : 0;
- if (butts & buttonD) butts = 0xFF;
- auto x = (butts << 3) & ledRED;
- auto y = (butts << 2) & ledYEL;
- auto z = (butts << 3) & ledGRN;
+ const auto leds = r | y | g;
- io::out::B() = x;
- io::out::D() = buttonCOM | (stateD = y | z);
+ setLeds((val & buttonC) ? ~leds : leds);
}
}
diff --git a/hidModule.h b/hidModule.h
@@ -41,20 +41,26 @@
namespace hid {
constexpr auto buttonCOM = bit(0);
-constexpr auto buttonL = bit(2);
-constexpr auto buttonU = bit(3);
-constexpr auto buttonR = bit(4);
-constexpr auto buttonD = bit(5);
-
constexpr auto wheelCOM = bit(1);
-constexpr auto wheelA = bit(2);
-constexpr auto wheelB = bit(3);
-constexpr auto buttonC = bit(4);
+
+constexpr auto wheelA = bit(0);
+constexpr auto wheelB = bit(1);
+constexpr auto buttonC = bit(2);
+constexpr auto buttonL = bit(3);
+constexpr auto buttonU = bit(4);
+constexpr auto buttonR = bit(5);
+constexpr auto buttonD = bit(6);
constexpr auto ledRED = bit(7);
constexpr auto ledYEL = bit(4);
constexpr auto ledGRN = bit(6);
+void init();
+
+uint8_t sampleInput();
+
+void setLeds(const uint8_t);
+
[[noreturn]]
void test();
diff --git a/lcd.h b/lcd.h
@@ -1,6 +1,7 @@
#pragma once
#include "io.h"
+#include "hidModule.h"
#include <util/delay.h>
@@ -105,8 +106,9 @@ void test()
{
if (0 == ++p.r) if (0 == ++p.b) --p.g;
- send(p.word);
+ //send(p.word);
//send(uint16_t(0xF800));
+ send(hid::sampleInput());
}
}
diff --git a/main.cpp b/main.cpp
@@ -10,9 +10,13 @@ int main(void)
{
ctrl::disableJTAG();
- lcd::test();
+ hid::init();
- //hid::test();
+ io::direction::C() = 0xFF;
+
+ hid::test();
+
+ //lcd::test();
/*
for (;;)
{
diff --git a/utils.h b/utils.h
@@ -18,3 +18,8 @@ inline constexpr uint16_t wbit(const uint8_t shift)
// static_assert(8 > shift, "must be in range (0, 7)");
return uint8_t(1) << shift;
}
+
+inline constexpr uint8_t mix(const uint8_t a, const uint8_t mask, const uint8_t b)
+{
+ return (a & ~mask) | (b & mask);
+}