diff --git a/arduino/ShiftInput.cpp b/arduino/ShiftInput.cpp new file mode 100644 index 0000000..b439fd6 --- /dev/null +++ b/arduino/ShiftInput.cpp @@ -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 +#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; +} diff --git a/arduino/ShiftInput.h b/arduino/ShiftInput.h new file mode 100644 index 0000000..ddf3048 --- /dev/null +++ b/arduino/ShiftInput.h @@ -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 + +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 diff --git a/arduino/arduino.ino b/arduino/arduino.ino index 559a883..932ae86 100644 --- a/arduino/arduino.ino +++ b/arduino/arduino.ino @@ -19,15 +19,17 @@ limitations under the License. * Hello World: 0x311833021210c0323401411412103416e4203406d400000048656c6c6f20576f726c640a */ -#include "vm.h" -#include "console.h" -#include "memory.h" +#include "VM.h" +#include "Console.h" +#include "Memory.h" +#include "ShiftInput.h" VM vm; Console console(&vm); +ShiftInput buttons(1, 14, 15, 0, 16); //Expecting a Microchip 23LCV512 connected over MOSI/MISO/MCLK -Memory mem(20000000, MSBFIRST, SPI_MODE0, 4); +Memory mem(4); MemoryCache ICache(64, mem); MemoryCache DCache(32, mem); @@ -68,6 +70,17 @@ void setup() { void 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) { diff --git a/arduino/console.cpp b/arduino/console.cpp index ff8482d..2477629 100644 --- a/arduino/console.cpp +++ b/arduino/console.cpp @@ -17,7 +17,7 @@ limitations under the License. #include #include #include -#include "console.h" +#include "Console.h" Console::Console(VM* _vm) { this->state = CONSOLE_NONE; diff --git a/arduino/console.h b/arduino/console.h index ef4e740..c88adaa 100644 --- a/arduino/console.h +++ b/arduino/console.h @@ -18,7 +18,7 @@ limitations under the License. #define UVM_CONSOLE_H #include -#include "vm.h" +#include "VM.h" enum ConsoleState_t { CONSOLE_NONE, diff --git a/arduino/memory.cpp b/arduino/memory.cpp index 52592ca..9a62d6c 100644 --- a/arduino/memory.cpp +++ b/arduino/memory.cpp @@ -14,9 +14,9 @@ See the License for the specific language governing permissions and 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 this->csPin = csPin; 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 this->memSize = 0xFFFF; //Store spi setting - this->setting = SPISettings(clock, bitOrder, dataMode); + this->setting = SPISettings(20000000, MSBFIRST, SPI_MODE0); } void Memory::start() { diff --git a/arduino/memory.h b/arduino/memory.h index 1404494..63fbcae 100644 --- a/arduino/memory.h +++ b/arduino/memory.h @@ -43,7 +43,7 @@ class Memory { void start(); void end(); public: - Memory(uint32_t clock, uint8_t bitOrder, uint8_t dataMode, uint8_t csPin); + Memory(uint8_t csPin); uint16_t getSize(); void setSize(uint16_t memSize); bool init(); diff --git a/arduino/vm.c b/arduino/vm.c index 73c45d2..1d05b82 100644 --- a/arduino/vm.c +++ b/arduino/vm.c @@ -16,7 +16,7 @@ limitations under the License. #include #include -#include "vm.h" +#include "VM.h" VM *new_vm() { VM *vm = calloc(1, sizeof(VM));