From f19b4c485abe93d92ba786f0d6edec6cb347ae8a Mon Sep 17 00:00:00 2001 From: Alexander Klein Date: Thu, 9 Feb 2023 11:54:14 +0100 Subject: [PATCH] Inital commit --- include/gamepad.h | 39 ++++++++++++++++++++++++++++++++++ library.json | 23 ++++++++++++++++++++ src/gamepad.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 include/gamepad.h create mode 100644 library.json create mode 100644 src/gamepad.cpp diff --git a/include/gamepad.h b/include/gamepad.h new file mode 100644 index 0000000..21603bb --- /dev/null +++ b/include/gamepad.h @@ -0,0 +1,39 @@ +#pragma once + +#include + +#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; +}; diff --git a/library.json b/library.json new file mode 100644 index 0000000..128c967 --- /dev/null +++ b/library.json @@ -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": "*" + } \ No newline at end of file diff --git a/src/gamepad.cpp b/src/gamepad.cpp new file mode 100644 index 0000000..064d687 --- /dev/null +++ b/src/gamepad.cpp @@ -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; +} \ No newline at end of file