Implemented button input using 74HC165 shift registers.

This commit is contained in:
2018-10-27 18:54:48 +01:00
parent 43428a27aa
commit e3d379de05
8 changed files with 135 additions and 11 deletions

75
arduino/ShiftInput.cpp Normal file
View File

@@ -0,0 +1,75 @@
/*
Copyright 2018 Sam Stevens
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <Arduino.h>
#include "ShiftInput.h"
ShiftInput::ShiftInput(uint8_t chipCount, uint8_t loadPin, uint8_t clockPin, uint8_t clockInhPin, uint8_t dataPin) {
this->input = (uint8_t*)calloc(chipCount, sizeof(uint8_t));
this->chipCount = chipCount;
this->loadPin = loadPin;
this->clockPin = clockPin;
this->clockInhPin = clockInhPin;
this->dataPin = dataPin;
pinMode(loadPin, OUTPUT);
digitalWrite(loadPin, HIGH);
pinMode(clockPin, OUTPUT);
digitalWrite(clockPin, LOW);
pinMode(clockInhPin, OUTPUT);
if (clockInhPin > 0) {
digitalWrite(clockInhPin, HIGH);
pinMode(dataPin, INPUT);
}
}
uint8_t* ShiftInput::getInput() {
return this->input;
}
uint8_t ShiftInput::getChipCount() {
return this->chipCount;
}
bool ShiftInput::updateInput() {
//Load values
digitalWrite(this->loadPin, LOW);
digitalWrite(this->clockPin, HIGH);
digitalWrite(this->loadPin, HIGH);
digitalWrite(this->clockPin, LOW);
bool change = false;
uint8_t input = 0;
if (this->clockInhPin > 0){
digitalWrite(this->clockInhPin, LOW);
}
for(uint8_t chip = 0; chip < this->chipCount; chip++) {
input = 0;
for(uint8_t inputBit = 0; inputBit < 8; inputBit++) {
input |= (digitalRead(this->dataPin) == HIGH ? 1 : 0) << inputBit;
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}
if (this->input[chip] != input) {
this->input[chip] = input;
change = true;
}
}
if (this->clockInhPin > 0){
digitalWrite(this->clockInhPin, HIGH);
}
return change;
}

36
arduino/ShiftInput.h Normal file
View File

@@ -0,0 +1,36 @@
/*
Copyright 2018 Sam Stevens
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef UVM_SHIFTINPUT_H
#define UVM_SHIFTINPUT_H
#include <stdint.h>
class ShiftInput {
uint8_t *input;
uint8_t chipCount;
uint8_t loadPin;
uint8_t clockPin;
uint8_t clockInhPin;
uint8_t dataPin;
public:
ShiftInput(uint8_t chipCount, uint8_t loadPin, uint8_t clockPin, uint8_t clockInhPin, uint8_t dataPin);
uint8_t getChipCount();
uint8_t *getInput();
bool updateInput();
};
#endif //UVM_SHIFTINPUT_H

View File

@@ -19,15 +19,17 @@ limitations under the License.
* Hello World: 0x311833021210c0323401411412103416e4203406d400000048656c6c6f20576f726c640a * Hello World: 0x311833021210c0323401411412103416e4203406d400000048656c6c6f20576f726c640a
*/ */
#include "vm.h" #include "VM.h"
#include "console.h" #include "Console.h"
#include "memory.h" #include "Memory.h"
#include "ShiftInput.h"
VM vm; VM vm;
Console console(&vm); Console console(&vm);
ShiftInput buttons(1, 14, 15, 0, 16);
//Expecting a Microchip 23LCV512 connected over MOSI/MISO/MCLK //Expecting a Microchip 23LCV512 connected over MOSI/MISO/MCLK
Memory mem(20000000, MSBFIRST, SPI_MODE0, 4); Memory mem(4);
MemoryCache ICache(64, mem); MemoryCache ICache(64, mem);
MemoryCache DCache(32, mem); MemoryCache DCache(32, mem);
@@ -68,6 +70,17 @@ void setup() {
void loop() { void loop() {
console.loop(); console.loop();
if (buttons.updateInput()) {
uint8_t *input = buttons.getInput();
for(uint8_t i = 0; i < buttons.getChipCount(); i++) {
Serial.print("Button Group ");
Serial.print(i);
Serial.print(" = ");
Serial.print(input[i], BIN);
Serial.println();
}
}
} }
void vm_print_error(uint8_t err) { void vm_print_error(uint8_t err) {

View File

@@ -17,7 +17,7 @@ limitations under the License.
#include <Arduino.h> #include <Arduino.h>
#include <HardwareSerial.h> #include <HardwareSerial.h>
#include <errno.h> #include <errno.h>
#include "console.h" #include "Console.h"
Console::Console(VM* _vm) { Console::Console(VM* _vm) {
this->state = CONSOLE_NONE; this->state = CONSOLE_NONE;

View File

@@ -18,7 +18,7 @@ limitations under the License.
#define UVM_CONSOLE_H #define UVM_CONSOLE_H
#include <HardwareSerial.h> #include <HardwareSerial.h>
#include "vm.h" #include "VM.h"
enum ConsoleState_t { enum ConsoleState_t {
CONSOLE_NONE, CONSOLE_NONE,

View File

@@ -14,9 +14,9 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
#include "memory.h" #include "Memory.h"
Memory::Memory(uint32_t clock, uint8_t bitOrder, uint8_t dataMode, uint8_t csPin) { Memory::Memory(uint8_t csPin) {
//Set cs pin high //Set cs pin high
this->csPin = csPin; this->csPin = csPin;
pinMode(csPin, OUTPUT); pinMode(csPin, OUTPUT);
@@ -24,7 +24,7 @@ Memory::Memory(uint32_t clock, uint8_t bitOrder, uint8_t dataMode, uint8_t csPin
//Default to 64k ram //Default to 64k ram
this->memSize = 0xFFFF; this->memSize = 0xFFFF;
//Store spi setting //Store spi setting
this->setting = SPISettings(clock, bitOrder, dataMode); this->setting = SPISettings(20000000, MSBFIRST, SPI_MODE0);
} }
void Memory::start() { void Memory::start() {

View File

@@ -43,7 +43,7 @@ class Memory {
void start(); void start();
void end(); void end();
public: public:
Memory(uint32_t clock, uint8_t bitOrder, uint8_t dataMode, uint8_t csPin); Memory(uint8_t csPin);
uint16_t getSize(); uint16_t getSize();
void setSize(uint16_t memSize); void setSize(uint16_t memSize);
bool init(); bool init();

View File

@@ -16,7 +16,7 @@ limitations under the License.
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "vm.h" #include "VM.h"
VM *new_vm() { VM *new_vm() {
VM *vm = calloc(1, sizeof(VM)); VM *vm = calloc(1, sizeof(VM));