Inital commit

This commit is contained in:
2023-02-09 11:54:14 +01:00
commit f19b4c485a
3 changed files with 115 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
#pragma once
#include <Arduino.h>
#define NUM_KEYS 12
class Gamepad{
public:
enum Button {
B = 0x1,
Y = 0x2,
Select = 0x4,
Start = 0x8,
Up = 0x10,
Down = 0x20,
Left = 0x40,
Right = 0x80,
A = 0x100,
X = 0x200,
LeftShoulder = 0x400,
RightShoulder = 0x800
};
Gamepad(uint8_t latch, uint8_t clock, uint8_t data);
bool isPressed(Button button);
bool isPressedLast(Button button);
uint16_t getState();
uint16_t getLastState();
private:
uint16_t readCon();
uint16_t lastState = 0;
uint8_t latch;
uint8_t clock;
uint8_t data;
};
+23
View File
@@ -0,0 +1,23 @@
{
"name": "SNES-Gamepad",
"version": "1.0.0",
"description": "Read SNES-Gamepad",
"keywords": "shift, register, gamepad, snes",
"repository":
{
"type": "git",
"url": "https://git.kleiax.de/PlatformIO-Libs/SNES-Gamepad.git"
},
"authors":
[
{
"name": "Alexander Klein",
"email": "alex@kleiax.de",
"url": "https://www.kleiax.de/contact"
}
],
"license": "MIT",
"homepage": "https://www.kleiax.de/",
"frameworks": "*",
"platforms": "*"
}
+53
View File
@@ -0,0 +1,53 @@
#include "gamepad.h"
Gamepad::Gamepad(uint8_t latch, uint8_t clock, uint8_t data){
this->latch = latch;
this->clock = clock;
this->data = data;
pinMode(latch, OUTPUT);
digitalWrite(latch, LOW);
pinMode(clock, OUTPUT);
digitalWrite(clock, HIGH);
pinMode(data, OUTPUT);
digitalWrite(data, HIGH);
pinMode(data, INPUT);
}
uint16_t Gamepad::getState(){
return this->readCon();
}
uint16_t Gamepad::readCon(){
uint16_t res = 0;
digitalWrite(this->latch, HIGH);
delayMicroseconds(12);
digitalWrite(this->latch, LOW);
delayMicroseconds(6);
for (uint8_t i = 0; i < NUM_KEYS; i++){
digitalWrite(this->clock, LOW);
delayMicroseconds(6);
res |= (!digitalRead(this->data) << i);
digitalWrite(this->clock, HIGH);
delayMicroseconds(6);
}
lastState = res;
return res;
}
bool Gamepad::isPressed(Button button) {
if ((uint16_t) button & this->readCon())
return true;
return false;
}
bool Gamepad::isPressedLast(Button button) {
if ((uint16_t) button & this->lastState)
return true;
return false;
}