ES ordening

This commit is contained in:
Rens Pastoor
2025-05-27 23:20:05 +02:00
parent d9244f1ae3
commit 39269a71a7
49 changed files with 1952 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,85 @@
#include "BME280.h"
// I2C read 1 byte
uint8_t readRegister(uint8_t reg) {
Wire.beginTransmission(BME280_ADDRESS);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(BME280_ADDRESS, 1);
return Wire.read();
}
// I2C write 1 byte
void writeRegister(uint8_t reg, uint8_t value) {
Wire.beginTransmission(BME280_ADDRESS);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
// Public functions
uint8_t BME280_GetID() {
return readRegister(BME280_REG_ID);
}
void BME280_Reset() {
writeRegister(BME280_REG_RESET, BME280_RESET_CMD);
}
uint8_t BME280_CtrlHum() {
return readRegister(BME280_REG_CTRL_HUM);
}
void BME280_CtrlHum(uint8_t bitpattern) {
writeRegister(BME280_REG_CTRL_HUM, bitpattern);
}
uint8_t BME280_CtrlMeas() {
return readRegister(BME280_REG_CTRL_MEAS);
}
void BME280_CtrlMeas(uint8_t bitpattern) {
writeRegister(BME280_REG_CTRL_MEAS, bitpattern);
}
long BME280_ReadTemperature() {
Wire.beginTransmission(BME280_ADDRESS);
Wire.write(BME280_REG_TEMP_MSB);
Wire.endTransmission();
Wire.requestFrom(BME280_ADDRESS, 3);
long msb = Wire.read();
long lsb = Wire.read();
long xlsb = Wire.read();
long adc_T = ((msb << 16) | (lsb << 8) | xlsb) >> 4;
return adc_T; // Raw value (compensation needed)
}
int BME280_ReadHumidity() {
Wire.beginTransmission(BME280_ADDRESS);
Wire.write(BME280_REG_HUM_MSB);
Wire.endTransmission();
Wire.requestFrom(BME280_ADDRESS, 2);
int msb = Wire.read();
int lsb = Wire.read();
int adc_H = (msb << 8) | lsb;
return adc_H; // Raw value
}
long BME280_ReadPressure() {
Wire.beginTransmission(BME280_ADDRESS);
Wire.write(BME280_REG_PRESS_MSB);
Wire.endTransmission();
Wire.requestFrom(BME280_ADDRESS, 3);
long msb = Wire.read();
long lsb = Wire.read();
long xlsb = Wire.read();
long adc_P = ((msb << 16) | (lsb << 8) | xlsb) >> 4;
return adc_P; // Raw value
}

View File

@@ -0,0 +1,43 @@
#ifndef BME280_H
#define BME280_H
#include <Arduino.h>
#include <Wire.h>
// I2C address for BME280 (default)
#define BME280_ADDRESS 0x76
// Register addresses from datasheet
#define BME280_REG_ID 0xD0
#define BME280_REG_RESET 0xE0
#define BME280_REG_CTRL_HUM 0xF2
#define BME280_REG_STATUS 0xF3
#define BME280_REG_CTRL_MEAS 0xF4
#define BME280_REG_CONFIG 0xF5
#define BME280_REG_PRESS_MSB 0xF7
#define BME280_REG_TEMP_MSB 0xFA
#define BME280_REG_HUM_MSB 0xFD
// Reset command
#define BME280_RESET_CMD 0xB6
// Function declarations
uint8_t BME280_GetID();
void BME280_Reset();
uint8_t BME280_CtrlHum();
void BME280_CtrlHum(uint8_t bitpattern);
uint8_t BME280_CtrlMeas();
void BME280_CtrlMeas(uint8_t bitpattern);
long BME280_ReadTemperature();
int BME280_ReadHumidity();
long BME280_ReadPressure();
#endif

View File

@@ -0,0 +1,37 @@
#include <Arduino.h>
#include <Wire.h>
#include "BME280.h"
void setup() {
Serial.begin(9600);
Wire.begin();
Serial.println("Initializing BME280...");
uint8_t id = BME280_GetID();
Serial.print("Sensor ID: 0x");
Serial.println(id, HEX);
BME280_Reset();
// Set oversampling for humidity = x1 (00000001)
BME280_CtrlHum(0x01);
// Set oversampling for temp and pressure = x1, mode = normal (00100111)
BME280_CtrlMeas(0x27);
Serial.println("BME280 Setup Done.");
}
void loop() {
long temp_raw = BME280_ReadTemperature();
int hum_raw = BME280_ReadHumidity();
long press_raw = BME280_ReadPressure();
Serial.print("Raw Temperature: ");
Serial.print(temp_raw);
Serial.print(" | Raw Humidity: ");
Serial.print(hum_raw);
Serial.print(" | Raw Pressure: ");
Serial.println(press_raw);
}

View File

@@ -0,0 +1,30 @@
#include <Wire.h>
#include <Arduino.h>
uint8_t count = 0;
void setup() {
Wire.begin(); // Master
Serial.begin(9600);
}
void loop() {
// Send count to slave
Wire.beginTransmission(0x42);
Wire.write(count);
Wire.endTransmission();
Serial.print("Sent: ");
Serial.println(count);
// Request response from slave
Wire.requestFrom(0x42, 1);
if (Wire.available()) {
uint8_t response = Wire.read();
Serial.print("Received: ");
Serial.println(response);
}
count++;
}

View File

@@ -0,0 +1,29 @@
#include <Arduino.h>
#include <Wire.h>
volatile uint8_t receivedValue = 0;
void setup() {
Wire.begin(0x42);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}
void loop() {
}
void receiveEvent(int howMany) {
if (howMany > 0) {
receivedValue = Wire.read();
Serial.print("Received from master: ");
Serial.println(receivedValue);
}
}
void requestEvent() {
uint8_t reply = (receivedValue > 100) ? 2 : 4;
Wire.write(reply);
Serial.print("Sent to master: ");
Serial.println(reply);
}

View File

@@ -0,0 +1,85 @@
#include "BME280.h"
// I2C read 1 byte
uint8_t readRegister(uint8_t reg) {
Wire.beginTransmission(BME280_ADDRESS);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(BME280_ADDRESS, 1);
return Wire.read();
}
// I2C write 1 byte
void writeRegister(uint8_t reg, uint8_t value) {
Wire.beginTransmission(BME280_ADDRESS);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
// Public functions
uint8_t BME280_GetID() {
return readRegister(BME280_REG_ID);
}
void BME280_Reset() {
writeRegister(BME280_REG_RESET, BME280_RESET_CMD);
}
uint8_t BME280_CtrlHum() {
return readRegister(BME280_REG_CTRL_HUM);
}
void BME280_CtrlHum(uint8_t bitpattern) {
writeRegister(BME280_REG_CTRL_HUM, bitpattern);
}
uint8_t BME280_CtrlMeas() {
return readRegister(BME280_REG_CTRL_MEAS);
}
void BME280_CtrlMeas(uint8_t bitpattern) {
writeRegister(BME280_REG_CTRL_MEAS, bitpattern);
}
long BME280_ReadTemperature() {
Wire.beginTransmission(BME280_ADDRESS);
Wire.write(BME280_REG_TEMP_MSB);
Wire.endTransmission();
Wire.requestFrom(BME280_ADDRESS, 3);
long msb = Wire.read();
long lsb = Wire.read();
long xlsb = Wire.read();
long adc_T = ((msb << 16) | (lsb << 8) | xlsb) >> 4;
return adc_T; // Raw value (compensation needed)
}
int BME280_ReadHumidity() {
Wire.beginTransmission(BME280_ADDRESS);
Wire.write(BME280_REG_HUM_MSB);
Wire.endTransmission();
Wire.requestFrom(BME280_ADDRESS, 2);
int msb = Wire.read();
int lsb = Wire.read();
int adc_H = (msb << 8) | lsb;
return adc_H; // Raw value
}
long BME280_ReadPressure() {
Wire.beginTransmission(BME280_ADDRESS);
Wire.write(BME280_REG_PRESS_MSB);
Wire.endTransmission();
Wire.requestFrom(BME280_ADDRESS, 3);
long msb = Wire.read();
long lsb = Wire.read();
long xlsb = Wire.read();
long adc_P = ((msb << 16) | (lsb << 8) | xlsb) >> 4;
return adc_P; // Raw value
}

View File

@@ -0,0 +1,43 @@
#ifndef BME280_H
#define BME280_H
#include <Arduino.h>
#include <Wire.h>
// I2C address for BME280 (default)
#define BME280_ADDRESS 0x76
// Register addresses from datasheet
#define BME280_REG_ID 0xD0
#define BME280_REG_RESET 0xE0
#define BME280_REG_CTRL_HUM 0xF2
#define BME280_REG_STATUS 0xF3
#define BME280_REG_CTRL_MEAS 0xF4
#define BME280_REG_CONFIG 0xF5
#define BME280_REG_PRESS_MSB 0xF7
#define BME280_REG_TEMP_MSB 0xFA
#define BME280_REG_HUM_MSB 0xFD
// Reset command
#define BME280_RESET_CMD 0xB6
// Function declarations
uint8_t BME280_GetID();
void BME280_Reset();
uint8_t BME280_CtrlHum();
void BME280_CtrlHum(uint8_t bitpattern);
uint8_t BME280_CtrlMeas();
void BME280_CtrlMeas(uint8_t bitpattern);
long BME280_ReadTemperature();
int BME280_ReadHumidity();
long BME280_ReadPressure();
#endif

View File

@@ -0,0 +1,61 @@
#include <Arduino.h>
#include <Wire.h>
#include "BME280.h"
#define PARALLEL_SLAVE_ADDR 0x77
void writeToRegister(uint8_t deviceAddr, uint8_t reg, uint8_t value) {
Wire.beginTransmission(deviceAddr);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
uint8_t readFromRegister(uint8_t deviceAddr, uint8_t reg) {
Wire.beginTransmission(deviceAddr);
Wire.write(reg);
Wire.endTransmission();
Wire.requestFrom(deviceAddr, 1);
return Wire.available() ? Wire.read() : 0xFF;
}
void setup() {
Serial.begin(9600);
Wire.begin();
// BME280 initialisatie
BME280_Reset();
BME280_CtrlHum(0x01); // x1 oversampling
BME280_CtrlMeas(0x27); // temp/press x1, mode normal
Serial.println("Setup done.");
}
void loop() {
// === Lezen van BME280 ===
long temp = BME280_ReadTemperature();
long press = BME280_ReadPressure();
int hum = BME280_ReadHumidity();
Serial.println("--- BME280 Readings ---");
Serial.print("Raw Temp: "); Serial.println(temp);
Serial.print("Raw Press: "); Serial.println(press);
Serial.print("Raw Hum: "); Serial.println(hum);
// === Testen van parallelle slave ===
uint8_t a = random(0, 100);
uint8_t b = random(0, 100);
writeToRegister(PARALLEL_SLAVE_ADDR, 0x21, a); // INA
writeToRegister(PARALLEL_SLAVE_ADDR, 0x22, b); // INB
uint8_t minVal = readFromRegister(PARALLEL_SLAVE_ADDR, 0x23); // MIN
uint8_t maxVal = readFromRegister(PARALLEL_SLAVE_ADDR, 0x24); // MAX
Serial.println("--- Parallel Slave ---");
Serial.print("a: "); Serial.print(a);
Serial.print(", b: "); Serial.print(b);
Serial.print(" => MIN: "); Serial.print(minVal);
Serial.print(", MAX: "); Serial.println(maxVal);
}

View File

@@ -0,0 +1,65 @@
#include <Arduino.h>
#include <Wire.h>
#define SLAVE_ADDRESS 0x77
uint8_t registers[256] = {0}; // Simuleer registermap
uint8_t regAddr = 0; // Registeradres voor read/write
void setup() {
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(9600);
Serial.println("Slave ready.");
}
void loop() {
}
// Ontvangen van master (write)
void receiveEvent(int howMany) {
if (howMany < 1) return;
regAddr = Wire.read(); // Eerste byte is altijd registeradres
if (howMany > 1) {
// Daarna komen data bytes
int bytesToRead = howMany - 1;
for (int i = 0; i < bytesToRead; i++) {
if (Wire.available()) {
uint8_t val = Wire.read();
registers[regAddr + i] = val;
Serial.print("Reg ");
Serial.print(regAddr + i);
Serial.print(" written with ");
Serial.println(val);
}
}
// Update min/max registers als 0x21 of 0x22 is gewijzigd
if (regAddr == 0x21 || regAddr == 0x22) {
uint8_t a = registers[0x21];
uint8_t b = registers[0x22];
registers[0x23] = (a < b) ? a : b; // MIN
registers[0x24] = (a > b) ? a : b; // MAX
Serial.print("Updated MIN (0x23): ");
Serial.println(registers[0x23]);
Serial.print("Updated MAX (0x24): ");
Serial.println(registers[0x24]);
}
}
// else alleen registeradres ontvangen, niks schrijven (read setup)
}
// Master vraagt data op (read)
void requestEvent() {
uint8_t val = registers[regAddr];
Wire.write(val);
Serial.print("Sent reg ");
Serial.print(regAddr, HEX);
Serial.print(": ");
Serial.println(val);
}

View File

@@ -0,0 +1,60 @@
#include <Arduino.h>
#include <Wire.h>
#define DEVICE_ADDRESS 0x50
// Virtual hardware registers
uint8_t reg_INA = 0;
uint8_t reg_INB = 0;
uint8_t reg_pointer = 0; // acts as address register for reads
// Called when master writes data
void receiveEvent(int howMany) {
if (howMany < 1) return;
// First byte is always register address
reg_pointer = Wire.read();
howMany--;
if (howMany == 1) {
// Second byte is data to write
uint8_t value = Wire.read();
if (reg_pointer == 0x21) {
reg_INA = value;
Serial.println("INA set");
} else if (reg_pointer == 0x22) {
reg_INB = value;
Serial.println("INB set");
} else {
// Writes to read-only registers are ignored
Serial.println("Write to read-only register ignored");
}
}
}
// Called when master reads data
void requestEvent() {
uint8_t value = 0;
switch (reg_pointer) {
case 0x21: value = reg_INA; break;
case 0x22: value = reg_INB; break;
case 0x23: value = min(reg_INA, reg_INB); break;
case 0x24: value = max(reg_INA, reg_INB); break;
default: value = 0xFF; break; // undefined register
}
Wire.write(value);
}
void setup() {
Wire.begin(DEVICE_ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}
void loop() {
}

View File

@@ -0,0 +1,65 @@
#include <Arduino.h>
#include <Wire.h>
#define SLAVE_ADDRESS 0x77
uint8_t registers[256] = {0}; // Simuleer registermap
uint8_t regAddr = 0; // Registeradres voor read/write
void setup() {
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);
Serial.begin(9600);
Serial.println("Slave ready.");
}
void loop() {
}
// Ontvangen van master (write)
void receiveEvent(int howMany) {
if (howMany < 1) return;
regAddr = Wire.read(); // Eerste byte is altijd registeradres
if (howMany > 1) {
// Daarna komen data bytes
int bytesToRead = howMany - 1;
for (int i = 0; i < bytesToRead; i++) {
if (Wire.available()) {
uint8_t val = Wire.read();
registers[regAddr + i] = val;
Serial.print("Reg ");
Serial.print(regAddr + i);
Serial.print(" written with ");
Serial.println(val);
}
}
// Update min/max registers als 0x21 of 0x22 is gewijzigd
if (regAddr == 0x21 || regAddr == 0x22) {
uint8_t a = registers[0x21];
uint8_t b = registers[0x22];
registers[0x23] = (a < b) ? a : b; // MIN
registers[0x24] = (a > b) ? a : b; // MAX
Serial.print("Updated MIN (0x23): ");
Serial.println(registers[0x23]);
Serial.print("Updated MAX (0x24): ");
Serial.println(registers[0x24]);
}
}
// else alleen registeradres ontvangen, niks schrijven (read setup)
}
// Master vraagt data op (read)
void requestEvent() {
uint8_t val = registers[regAddr];
Wire.write(val);
Serial.print("Sent reg ");
Serial.print(regAddr, HEX);
Serial.print(": ");
Serial.println(val);
}

View File

@@ -0,0 +1,37 @@
This directory is intended for project header files.
A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.
```src/main.c
#include "header.h"
int main (void)
{
...
}
```
Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.
In C, the convention is to give header files names that end with `.h'.
Read more about using header files in official GCC documentation:
* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

View File

@@ -0,0 +1,46 @@
This directory is intended for project specific (private) libraries.
PlatformIO will compile them to static libraries and link into the executable file.
The source code of each library should be placed in a separate directory
("lib/your_library_name/[Code]").
For example, see the structure of the following example libraries `Foo` and `Bar`:
|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional. for custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c
Example contents of `src/main.c` using Foo and Bar:
```
#include <Foo.h>
#include <Bar.h>
int main (void)
{
...
}
```
The PlatformIO Library Dependency Finder will find automatically dependent
libraries by scanning project source files.
More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html

View File

@@ -0,0 +1,15 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html
[env:uno]
platform = atmelavr
board = uno
framework = arduino
lib_deps = adafruit/Adafruit BusIO@^1.17.1

View File

@@ -0,0 +1,11 @@
This directory is intended for PlatformIO Test Runner and project tests.
Unit Testing is a software testing method by which individual units of
source code, sets of one or more MCU program modules together with associated
control data, usage procedures, and operating procedures, are tested to
determine whether they are fit for use. Unit testing finds problems early
in the development cycle.
More information about PlatformIO Unit Testing:
- https://docs.platformio.org/en/latest/advanced/unit-testing/index.html