|
| 1 | +// |
| 2 | +// SegaController.cpp |
| 3 | +// |
| 4 | +// Author: |
| 5 | +// Jon Thysell <[email protected]> |
| 6 | +// |
| 7 | +// Copyright (c) 2017 Jon Thysell <http://jonthysell.com> |
| 8 | +// |
| 9 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +// of this software and associated documentation files (the "Software"), to deal |
| 11 | +// in the Software without restriction, including without limitation the rights |
| 12 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the Software is |
| 14 | +// furnished to do so, subject to the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be included in |
| 17 | +// all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +// THE SOFTWARE. |
| 26 | + |
| 27 | +#include "Arduino.h" |
| 28 | +#include "SegaController.h" |
| 29 | + |
| 30 | +SegaController::SegaController(unsigned db9_pin_7, unsigned db9_pin_1, unsigned db9_pin_2, unsigned db9_pin_3, unsigned db9_pin_4, unsigned db9_pin_6, unsigned db9_pin_9) |
| 31 | +{ |
| 32 | + // Set pins |
| 33 | + _selectPin = db9_pin_7; |
| 34 | + |
| 35 | + _inputPins[0] = db9_pin_1; |
| 36 | + _inputPins[1] = db9_pin_2; |
| 37 | + _inputPins[2] = db9_pin_3; |
| 38 | + _inputPins[3] = db9_pin_4; |
| 39 | + _inputPins[4] = db9_pin_6; |
| 40 | + _inputPins[5] = db9_pin_9; |
| 41 | + |
| 42 | + // Setup output pin |
| 43 | + pinMode(_selectPin, OUTPUT); |
| 44 | + digitalWrite(_selectPin, HIGH); |
| 45 | + |
| 46 | + // Setup input pins |
| 47 | + for (byte i = 0; i < SC_INPUT_PINS; i++) |
| 48 | + { |
| 49 | + pinMode(_inputPins[i], INPUT_PULLUP); |
| 50 | + } |
| 51 | + |
| 52 | + _currentState = 0; |
| 53 | + _sixButtonMode = false; |
| 54 | + _lastReadTime = millis(); |
| 55 | +} |
| 56 | + |
| 57 | +word SegaController::getState() |
| 58 | +{ |
| 59 | + if (max(millis() - _lastReadTime, 0) < SC_READ_DELAY_MS) |
| 60 | + { |
| 61 | + // Not enough time has elapsed, return previously read state |
| 62 | + return _currentState; |
| 63 | + } |
| 64 | + |
| 65 | + noInterrupts(); |
| 66 | + |
| 67 | + // Clear current state |
| 68 | + _currentState = 0; |
| 69 | + |
| 70 | + for (byte cycle = 0; cycle < SC_CYCLES; cycle++) |
| 71 | + { |
| 72 | + readCycle(cycle); |
| 73 | + } |
| 74 | + |
| 75 | + // When a controller disconnects, revert to three-button polling |
| 76 | + if (!(_currentState & SC_CTL_ON)) |
| 77 | + { |
| 78 | + _sixButtonMode = false; |
| 79 | + } |
| 80 | + |
| 81 | + interrupts(); |
| 82 | + |
| 83 | + _lastReadTime = millis(); |
| 84 | + |
| 85 | + return _currentState; |
| 86 | +} |
| 87 | + |
| 88 | +void SegaController::readCycle(byte cycle) |
| 89 | +{ |
| 90 | + // Set the select pin low/high |
| 91 | + digitalWrite(_selectPin, cycle % 2); |
| 92 | + |
| 93 | + delayMicroseconds(50); |
| 94 | + |
| 95 | + // Read flags |
| 96 | + switch (cycle) |
| 97 | + { |
| 98 | + case 2: |
| 99 | + // Check that a controller is connected |
| 100 | + _currentState |= (digitalRead(_inputPins[2]) == LOW && digitalRead(_inputPins[3]) == LOW) * SC_CTL_ON; |
| 101 | + |
| 102 | + // Check controller is connected before reading A/Start to prevent bad reads when inserting/removing cable |
| 103 | + if (_currentState & SC_CTL_ON) |
| 104 | + { |
| 105 | + // Read input pins for A, Start |
| 106 | + if (digitalRead(_inputPins[4]) == LOW) { _currentState |= SC_BTN_A; } |
| 107 | + if (digitalRead(_inputPins[5]) == LOW) { _currentState |= SC_BTN_START; } |
| 108 | + } |
| 109 | + break; |
| 110 | + case 3: |
| 111 | + // Read input pins for Up, Down, Left, Right, B, C |
| 112 | + if (digitalRead(_inputPins[0]) == LOW) { _currentState |= SC_BTN_UP; } |
| 113 | + if (digitalRead(_inputPins[1]) == LOW) { _currentState |= SC_BTN_DOWN; } |
| 114 | + if (digitalRead(_inputPins[2]) == LOW) { _currentState |= SC_BTN_LEFT; } |
| 115 | + if (digitalRead(_inputPins[3]) == LOW) { _currentState |= SC_BTN_RIGHT; } |
| 116 | + if (digitalRead(_inputPins[4]) == LOW) { _currentState |= SC_BTN_B; } |
| 117 | + if (digitalRead(_inputPins[5]) == LOW) { _currentState |= SC_BTN_C; } |
| 118 | + break; |
| 119 | + case 4: |
| 120 | + _sixButtonMode = (digitalRead(_inputPins[0]) == LOW && digitalRead(_inputPins[1]) == LOW); |
| 121 | + break; |
| 122 | + case 5: |
| 123 | + if (_sixButtonMode) |
| 124 | + { |
| 125 | + // Read input pins for X, Y, Z, Mode |
| 126 | + if (digitalRead(_inputPins[0]) == LOW) { _currentState |= SC_BTN_Z; } |
| 127 | + if (digitalRead(_inputPins[1]) == LOW) { _currentState |= SC_BTN_Y; } |
| 128 | + if (digitalRead(_inputPins[2]) == LOW) { _currentState |= SC_BTN_X; } |
| 129 | + if (digitalRead(_inputPins[3]) == LOW) { _currentState |= SC_BTN_MODE; } |
| 130 | + } |
| 131 | + break; |
| 132 | + } |
| 133 | +} |
0 commit comments