Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a0c04a670 | ||
|
|
7466bcacff | ||
|
|
d90a38eebb |
+4
-2
@@ -24,9 +24,11 @@ class Gamepad{
|
|||||||
Gamepad(uint8_t latch, uint8_t clock, uint8_t data);
|
Gamepad(uint8_t latch, uint8_t clock, uint8_t data);
|
||||||
|
|
||||||
bool isPressed(Button button);
|
bool isPressed(Button button);
|
||||||
bool isPressedLast(Button button);
|
bool isPressedLast(Button button) const;
|
||||||
uint16_t getState();
|
uint16_t getState();
|
||||||
uint16_t getLastState();
|
uint16_t getLastState() const { return this->lastState; }
|
||||||
|
|
||||||
|
static bool isPressed(uint16_t input, Button button);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t readCon();
|
uint16_t readCon();
|
||||||
|
|||||||
+22
-9
@@ -1,6 +1,7 @@
|
|||||||
#include "gamepad.h"
|
#include "gamepad.h"
|
||||||
|
|
||||||
Gamepad::Gamepad(uint8_t latch, uint8_t clock, uint8_t data){
|
Gamepad::Gamepad(uint8_t latch, uint8_t clock, uint8_t data)
|
||||||
|
{
|
||||||
this->latch = latch;
|
this->latch = latch;
|
||||||
this->clock = clock;
|
this->clock = clock;
|
||||||
this->data = data;
|
this->data = data;
|
||||||
@@ -16,11 +17,13 @@ Gamepad::Gamepad(uint8_t latch, uint8_t clock, uint8_t data){
|
|||||||
pinMode(data, INPUT);
|
pinMode(data, INPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Gamepad::getState(){
|
uint16_t Gamepad::getState()
|
||||||
|
{
|
||||||
return this->readCon();
|
return this->readCon();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Gamepad::readCon(){
|
uint16_t Gamepad::readCon()
|
||||||
|
{
|
||||||
uint16_t res = 0;
|
uint16_t res = 0;
|
||||||
|
|
||||||
digitalWrite(this->latch, HIGH);
|
digitalWrite(this->latch, HIGH);
|
||||||
@@ -28,7 +31,8 @@ uint16_t Gamepad::readCon(){
|
|||||||
digitalWrite(this->latch, LOW);
|
digitalWrite(this->latch, LOW);
|
||||||
delayMicroseconds(6);
|
delayMicroseconds(6);
|
||||||
|
|
||||||
for (uint8_t i = 0; i < NUM_KEYS; i++){
|
for (uint8_t i = 0; i < NUM_KEYS; i++)
|
||||||
|
{
|
||||||
digitalWrite(this->clock, LOW);
|
digitalWrite(this->clock, LOW);
|
||||||
delayMicroseconds(6);
|
delayMicroseconds(6);
|
||||||
res |= (!digitalRead(this->data) << i);
|
res |= (!digitalRead(this->data) << i);
|
||||||
@@ -36,18 +40,27 @@ uint16_t Gamepad::readCon(){
|
|||||||
delayMicroseconds(6);
|
delayMicroseconds(6);
|
||||||
}
|
}
|
||||||
|
|
||||||
lastState = res;
|
this->lastState = res;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Gamepad::isPressed(Button button) {
|
bool Gamepad::isPressed(Button button)
|
||||||
if ((uint16_t) button & this->readCon())
|
{
|
||||||
|
if ((uint16_t)button & this->readCon())
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Gamepad::isPressedLast(Button button) {
|
bool Gamepad::isPressedLast(Button button) const
|
||||||
if ((uint16_t) button & this->lastState)
|
{
|
||||||
|
if ((uint16_t)button & this->lastState)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Gamepad::isPressed(uint16_t input, Button button)
|
||||||
|
{
|
||||||
|
if ((uint16_t)button & input)
|
||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user