Compare commits

..

No commits in common. "1974376134df896c6146c0ab55175fb9d0e62276" and "92a2d39e4930bc319354e88fe2c73d5b0e7417e1" have entirely different histories.

2 changed files with 32 additions and 55 deletions

View File

@ -1,39 +1,29 @@
#include <Arduino.h> #include <Arduino.h>
#include <Wire.h> #include <Wire.h>
#define I2C_SLAVE_ADDRESS 0x08 #define I2C_SLAVE_ADDRESS 0x08
volatile uint16_t bufferA[6]; uint16_t values[6] = {0};
volatile uint16_t bufferB[6];
// Pointer to the buffer currently exposed to ISR
volatile uint16_t* activeBuffer = bufferA;
void onI2CRequest() { void onI2CRequest() {
// Safe: ISR only reads active buffer Wire.write((uint8_t*)values, sizeof(values));
Wire.write((uint8_t*)activeBuffer, 6 * sizeof(uint16_t));
} }
void setup() { void setup() {
Wire.begin(I2C_SLAVE_ADDRESS); Wire.begin(I2C_SLAVE_ADDRESS);
Wire.onRequest(onI2CRequest); Wire.onRequest(onI2CRequest);
analogReference(DEFAULT); analogReference(DEFAULT);
} }
void loop() { void loop() {
// Choose the inactive buffer values[0] = analogRead(A0);
volatile uint16_t* writeBuffer = values[1] = analogRead(A1);
(activeBuffer == bufferA) ? bufferB : bufferA; values[2] = analogRead(A2);
writeBuffer[0] = analogRead(A0); values[3] = analogRead(A3);
writeBuffer[1] = analogRead(A1); values[4] = analogRead(A6);
writeBuffer[2] = analogRead(A2); values[5] = analogRead(A7);
writeBuffer[3] = analogRead(A3); delay(50);
writeBuffer[4] = analogRead(A6);
writeBuffer[5] = analogRead(A7);
// Atomic pointer swap
noInterrupts();
activeBuffer = writeBuffer;
interrupts();
} }

View File

@ -97,21 +97,16 @@ bool readButton(uint8_t i) {
// ------------------ Neues Spiel ------------------ // ------------------ Neues Spiel ------------------
void generateNewGame(bool easy) { void generateNewGame() {
for (uint8_t i = 0; i < 6; i++) for (uint8_t i = 0; i < 6; i++) solutionOrder[i] = i;
solutionOrder[i] = i;
if (!easy) for (int i = 5; i > 0; i--) {
{
for (int i = 5; i > 0; i--)
{
int j = random(i + 1); int j = random(i + 1);
uint8_t t = solutionOrder[i]; uint8_t t = solutionOrder[i];
solutionOrder[i] = solutionOrder[j]; solutionOrder[i] = solutionOrder[j];
solutionOrder[j] = t; solutionOrder[j] = t;
} }
}
for (uint8_t i = 0; i < 6; i++) for (uint8_t i = 0; i < 6; i++)
expectedOrder[i] = solutionOrder[5 - i]; expectedOrder[i] = solutionOrder[5 - i];
@ -142,16 +137,12 @@ void generateNewGame(bool easy) {
uint8_t idx[18]; uint8_t idx[18];
for (uint8_t i = 0; i < 18; i++) idx[i] = i; for (uint8_t i = 0; i < 18; i++) idx[i] = i;
if (!easy) for (int i = 17; i > 0; i--) {
{
for (int i = 17; i > 0; i--)
{
int j = random(i + 1); int j = random(i + 1);
uint8_t t = idx[i]; uint8_t t = idx[i];
idx[i] = idx[j]; idx[i] = idx[j];
idx[j] = t; idx[j] = t;
} }
}
for (uint8_t i = 0; i < 6; i++) for (uint8_t i = 0; i < 6; i++)
buttonAssignment[idx[i]] = solutionOrder[i]; buttonAssignment[idx[i]] = solutionOrder[i];
@ -253,7 +244,7 @@ void setup() {
if (powerOnState) if (powerOnState)
{ {
generateNewGame(false); generateNewGame();
} }
} }
@ -280,7 +271,7 @@ void loop() {
// Detecting turn-on // Detecting turn-on
if (!powerOnState && digitalRead(PIN_POWER_ON) == HIGH) if (!powerOnState && digitalRead(PIN_POWER_ON) == HIGH)
{ {
generateNewGame(false); generateNewGame();
powerOnState = true; powerOnState = true;
} }
if (!powerOnState) if (!powerOnState)
@ -288,18 +279,14 @@ void loop() {
// We are turned-off and stop anything after here // We are turned-off and stop anything after here
return; return;
} }
if (digitalRead(PIN_TRIGGER_SHUFFLE) == HIGH)
{
unsigned long resetTimer = millis();
while (digitalRead(PIN_TRIGGER_SHUFFLE) == HIGH)
{
delay(10);
}
generateNewGame(millis() - resetTimer > 150);
}
unsigned long now = millis(); unsigned long now = millis();
bool trigger = digitalRead(PIN_TRIGGER_SHUFFLE);
if (trigger && !lastTriggerState) {
generateNewGame();
}
lastTriggerState = trigger;
for (uint8_t i = 0; i < 18; i++) { for (uint8_t i = 0; i < 18; i++) {
bool pressed = readButton(i); bool pressed = readButton(i);