Skip to content

Commit

Permalink
UNTESTED ("it compiles, ship it"), getting rid of modulos since I hav…
Browse files Browse the repository at this point in the history
…e a nagiing feeling they are really expensive
  • Loading branch information
rambo committed Dec 16, 2013
1 parent 443e31e commit c9373cf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 12 additions & 2 deletions TinyWireS/examples/attiny85_i2c_slave/attiny85_i2c_slave.ino
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ volatile uint8_t i2c_regs[] =
};
// Tracks the current register pointer position
volatile byte reg_position;
const byte reg_size = sizeof(i2c_regs);
const byte reg_size_lessone = reg_size-1;

/**
* This is called for each read request we receive, never put more than one byte of data (with TinyWireS.send) to the
Expand All @@ -60,7 +62,11 @@ void requestEvent()
{
TinyWireS.send(i2c_regs[reg_position]);
// Increment the reg position on each read, and loop back to zero
reg_position = (reg_position+1) % sizeof(i2c_regs);
reg_position++;
if (reg_position >= reg_size_lessone)
{
reg_position = 0;
}
}

// TODO: Either update this to use something smarter for timing or remove it alltogether
Expand Down Expand Up @@ -104,8 +110,12 @@ void receiveEvent(uint8_t howMany)
}
while(howMany--)
{
i2c_regs[reg_position%sizeof(i2c_regs)] = TinyWireS.receive();
i2c_regs[reg_position] = TinyWireS.receive();
reg_position++;
if (reg_position >= reg_size_lessone)
{
reg_position = 0;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ volatile uint8_t i2c_regs[] =
};
// Tracks the current register pointer position
volatile byte reg_position;
const byte reg_size = sizeof(i2c_regs);
const byte reg_size_lessone = reg_size-1;



Expand Down Expand Up @@ -138,7 +140,13 @@ I2CStopCheck::I2CStopCheck()
bool I2CStopCheck::canRun(uint32_t now)
{
yield_counter++;
return ((yield_counter % I2CStopCheck_YIELD_TICKS) == 1);
bool ret = false;
if (yield_counter == I2CStopCheck_YIELD_TICKS)
{
ret = true;
yield_counter = 0;
}
return ret;
}

void I2CStopCheck::run(uint32_t now)
Expand Down Expand Up @@ -166,7 +174,11 @@ void requestEvent()
{
TinyWireS.send(i2c_regs[reg_position]);
// Increment the reg position on each read, and loop back to zero
reg_position = (reg_position+1) % sizeof(i2c_regs);
reg_position++;
if (reg_position >= reg_size_lessone)
{
reg_position = 0;
}
}

/**
Expand Down Expand Up @@ -197,8 +209,12 @@ void receiveEvent(uint8_t howMany)
}
while(howMany--)
{
i2c_regs[reg_position%sizeof(i2c_regs)] = TinyWireS.receive();
i2c_regs[reg_position] = TinyWireS.receive();
reg_position++;
if (reg_position >= reg_size_lessone)
{
reg_position = 0;
}
}
}

Expand Down

0 comments on commit c9373cf

Please sign in to comment.