Skip to content

Commit

Permalink
Working on blahlicus#34
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackCapCoder committed Feb 28, 2019
1 parent 492193c commit f6286c5
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 200 deletions.
Binary file modified animus-3/source/animus/..arduino.samd.arduino_zero_native.bin
Binary file not shown.
Binary file modified animus-3/source/animus/..arduino.samd.arduino_zero_native.elf
Binary file not shown.
2 changes: 2 additions & 0 deletions animus-3/source/animus/Global.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#define DEFAULT_ROW 12
#define DEFAULT_COL 12
#define I2C_HOST_ADDRESS 7
#define I2C_GUEST_ADDRESS 8

#ifndef ANIMUS_GLOBAL
#define ANIMUS_GLOBAL
Expand Down
156 changes: 93 additions & 63 deletions animus-3/source/animus/ModI2CGuest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,79 +12,79 @@ void CModI2CGuest::Begin(void) //TODO add method to return entire EEPROM to host

if (!Global.HasUSB) // if no usb
{
Wire.begin(8);
Wire.onRequest([] {ModI2CGuest.OnRequest();});
Wire.onReceive([] (int numBytes) {ModI2CGuest.OnReceive(numBytes);});
}
}

void CModI2CGuest::OnRequest()
{
if (SlaveIndex == 0) // if slave array is empty, add single byte
{
Wire.write(0);
isMaster = false;
SetMaster(true);
}
Wire.write(SlaveArray, SlaveIndex);
SlaveIndex = 0;
}

void CModI2CGuest::OnReceive(int numBytes)
{
byte errorOffset = 0;

byte type = Wire.read();
if (type == 1) // received templay
{
byte input = Wire.read();
Global.TempLayer = input;
}
else if (type == 2) // setEEPROM
while (Wire.available())
{
byte type = Wire.read();

byte byteA = Wire.read();
byte byteB = Wire.read();
short startAddr = (byteA << 8) | byteB;
while (Wire.available())
if (type == 1) // received templay
{
PersMem.SetEEPROM(startAddr, Wire.read());
startAddr++;
byte input = Wire.read();
Global.TempLayer = input;
}
PersMem.CommitEEPROM();
}
else if (type == 3) // return part of the EEPROM
{
byte byteA = Wire.read();
byte byteB = Wire.read();
short startAddr = (byteA << 8) | byteB;
SlaveIndex = 0;
while (SlaveIndex < 32 && startAddr < MEM_EEPROM_SIZE)
else if (type == 2) // setEEPROM
{
SlaveArray[SlaveIndex] = PersMem.GetEEPROM(startAddr);
SlaveIndex++;
startAddr++;
byte byteA = Wire.read();
byte byteB = Wire.read();
for (short i = (byteA << 8) | byteB; i < MEM_EEPROM_SIZE && Wire.available(); i++)
{
PersMem.SetEEPROM(i, Wire.read());
}
PersMem.CommitEEPROM();
}
}
else if (type == 4) // update brightness
{
Global.LEDBrightness = Wire.read();
}
else if (type == 5) // update keyboard delays
{
Global.KeyDownDelay = Wire.read();
Global.KeyUpDelay = Wire.read();
}
else if (type == 6) // resend for erreneous key
{
byte x = Wire.read();
byte y = Wire.read();
Global.LayerState[x][y] = Global.TempLayer;
if (SlaveIndex < 32)
else if (type == 3) // return part of the EEPROM
{
SlaveArray[SlaveIndex] = PersMem.GetKeyData(x, y, Global.TempLayer);
SlaveIndex++;
SlaveArray[SlaveIndex] = PersMem.GetKeyType(x, y, Global.TempLayer);
SlaveIndex++;
byte byteA = Wire.read();
byte byteB = Wire.read();
SlaveIndex = 0;
for (short i = (byteA << 8) | byteB; i < MEM_EEPROM_SIZE && Wire.available(); i++)
{
SlaveArray[SlaveIndex] = PersMem.GetEEPROM(i);
SlaveIndex++;
}
}
else if (type == 4) // update brightness
{
Global.LEDBrightness = Wire.read();
}
else if (type == 5) // update refresh rate
{
Global.KeyDownDelay = Wire.read();
Global.KeyUpDelay = Wire.read();
}
else if (type == 6) // erreneous key
{
byte x = Wire.read();
byte y = Wire.read();
Global.LayerState[x][y] = Global.TempLayer;

if (32 - SlaveIndex >= 3)
{
// We want all erreneous keys to appear before any keypresses that might have
// occured, in chronological order.
if (SlaveIndex > 0)
{
memcpy(SlaveArray + errorOffset + 3, SlaveArray + errorOffset, SlaveIndex - errorOffset);
}
SlaveArray[errorOffset + 0] = PersMem.GetKeyData(x, y, Global.TempLayer);
SlaveArray[errorOffset + 1] = PersMem.GetKeyType(x, y, Global.TempLayer);
SlaveArray[errorOffset + 2] = 5;
SlaveIndex += 3;
errorOffset += 3;
}
}
}

SetMaster(true);
pullTimeout = 0;
}

void CModI2CGuest::LoadData(void)
Expand All @@ -101,12 +101,23 @@ void CModI2CGuest::Loop(void)
{
CModTemplate::Loop();

if (Animus.Async1MSDelay())
{

if (Global.HasUSB)
if (!Global.HasUSB && isMaster) {
if (SlaveIndex > 0) {
Wire.beginTransmission(I2C_HOST_ADDRESS);
Wire.write(SlaveArray, SlaveIndex);
Wire.endTransmission();
SlaveIndex = 0;
SetMaster(false);
}
else if (Animus.Async1MSDelay())
{

pullTimeout++;
if (pullTimeout >= PULL_RATE) // Send an empty message to pull updates from the host
{
Wire.beginTransmission(I2C_HOST_ADDRESS);
Wire.endTransmission();
SetMaster(false);
}
}
}
}
Expand Down Expand Up @@ -187,4 +198,23 @@ void CModI2CGuest::SerialComms(byte mode)
}


void CModI2CGuest::SetMaster(bool value)
{
if (isMaster == value) return;

isMaster = value;
Wire.end();

if (value)
{
Wire.begin();
Wire.onReceive([] (int numBytes) {ModI2CGuest.OnReceive(numBytes);});
}
else
{
Wire.begin(I2C_GUEST_ADDRESS);
}
}


CModI2CGuest ModI2CGuest;
6 changes: 5 additions & 1 deletion animus-3/source/animus/ModI2CGuest.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
#include "Animus.h"
#include "Wire.h"

#define PULL_RATE 50

class CModI2CGuest : public CModTemplate
{
private:
byte SlaveArray[32];
byte SlaveIndex = 0;
bool isMaster = false;
byte pullTimeout = 0;
public:
CModI2CGuest(void);
void Begin(void);
Expand All @@ -18,11 +22,11 @@ class CModI2CGuest : public CModTemplate
void PrePress(byte val, byte type);
void PressKey(byte val, byte type);
void ReleaseKey(byte val, byte type);
void OnRequest();
void OnReceive(int numBytes);
static void RequestEvent();
static void ReceiveEvent(int numBytes);
void SerialComms(byte mode);
void SetMaster(bool value);
};
extern CModI2CGuest ModI2CGuest;

Expand Down
Loading

0 comments on commit f6286c5

Please sign in to comment.