adding the files ig
This commit is contained in:
commit
939a70aba5
11 files changed
+356
No files matched your search
@@ -0,0 +1,6 @@
|
||||
#ifndef I2C_COMMANDS_H
|
||||
#define I2C_COMMANDS_H
|
||||
|
||||
#define CMD_CHANGE_ADDR 0xAA // Use the same value as in your slave
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "I2CMasterUtils.h"
|
||||
#include "I2CCommands.h"
|
||||
|
||||
void I2CMasterUtils::scanI2C(Stream &output) {
|
||||
output.println("Scanning for I2C devices...");
|
||||
for (uint8_t addr = 1; addr < 127; addr++) {
|
||||
Wire.beginTransmission(addr);
|
||||
if (Wire.endTransmission() == 0) {
|
||||
output.print("Found device at 0x");
|
||||
output.println(addr, HEX);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void I2CMasterUtils::changeSlaveAddress(uint8_t oldAddr, uint8_t newAddr, Stream &output) {
|
||||
Wire.beginTransmission(oldAddr);
|
||||
Wire.write(0xAA); // Command code for address change
|
||||
Wire.write(newAddr);
|
||||
Wire.endTransmission();
|
||||
output.print("Sent address change command to 0x");
|
||||
output.print(oldAddr, HEX);
|
||||
output.print(" -> 0x");
|
||||
output.println(newAddr, HEX);
|
||||
}
|
||||
|
||||
bool I2CMasterUtils::parseChangeAddressCommand(const String &input, uint8_t &oldAddr, uint8_t &newAddr) {
|
||||
if (!input.startsWith("ch")) return false;
|
||||
int o, n;
|
||||
int matched = sscanf(input.c_str(), "ch %x %x", &o, &n);
|
||||
if (matched == 2) {
|
||||
oldAddr = (uint8_t)o;
|
||||
newAddr = (uint8_t)n;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef I2CMASTERUTILS_H
|
||||
#define I2CMASTERUTILS_H
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
|
||||
class I2CMasterUtils {
|
||||
public:
|
||||
static void scanI2C(Stream &output = Serial);
|
||||
static void changeSlaveAddress(uint8_t oldAddr, uint8_t newAddr, Stream &output = Serial);
|
||||
static bool parseChangeAddressCommand(const String &input, uint8_t &oldAddr, uint8_t &newAddr);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,100 @@
|
||||
#include "I2CStepper.h"
|
||||
#include "I2CCommands.h"
|
||||
|
||||
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
|
||||
#define IS_ARDUINO
|
||||
#endif
|
||||
|
||||
// Define your stepper pins here for the slave
|
||||
#define EN_PIN 8
|
||||
#define DIR_PIN 5
|
||||
#define STEP_PIN 2
|
||||
|
||||
static uint8_t currentAddress = 0x55; // Default address
|
||||
|
||||
I2CStepper::I2CStepper(uint8_t address) : _address(address) {}
|
||||
|
||||
void I2CStepper::begin() {
|
||||
Wire.begin(_address);
|
||||
#ifdef IS_ARDUINO
|
||||
pinMode(EN_PIN, OUTPUT);
|
||||
pinMode(DIR_PIN, OUTPUT);
|
||||
pinMode(STEP_PIN, OUTPUT);
|
||||
digitalWrite(EN_PIN, LOW); // Enable driver
|
||||
// Register receive handler for slave
|
||||
Wire.onReceive([this](int numBytes){ this->handleI2CCommand(); });
|
||||
#endif
|
||||
}
|
||||
|
||||
void I2CStepper::setSpeed(uint8_t speed, uint8_t direction) {
|
||||
// Send speed/direction to slave
|
||||
if (_address == 0) return;
|
||||
if (speed > 100) speed = 100;
|
||||
direction = (direction == 1) ? 1 : 0;
|
||||
Wire.beginTransmission(_address);
|
||||
Wire.write(speed); // 0-100 (percent)
|
||||
Wire.write(direction); // 0 or 1
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
void I2CStepper::stop() {
|
||||
setSpeed(0, 0);
|
||||
}
|
||||
|
||||
uint8_t I2CStepper::scanDevices(uint8_t* foundAddresses, uint8_t maxDevices) {
|
||||
uint8_t count = 0;
|
||||
for (uint8_t addr = 1; addr < 127; addr++) {
|
||||
Wire.beginTransmission(addr);
|
||||
if (Wire.endTransmission() == 0) {
|
||||
if (foundAddresses && count < maxDevices) {
|
||||
foundAddresses[count] = addr;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void I2CStepper::setAddress(uint8_t newAddress) {
|
||||
_address = newAddress;
|
||||
}
|
||||
|
||||
// ========== SLAVE: I2C Command Handler ==========
|
||||
|
||||
void I2CStepper::handleI2CCommand() {
|
||||
if (Wire.available() < 1) return;
|
||||
uint8_t cmd = Wire.read();
|
||||
|
||||
if (cmd == CMD_CHANGE_ADDR) {
|
||||
// Change I2C address
|
||||
if (Wire.available() >= 1) {
|
||||
uint8_t newAddr = Wire.read();
|
||||
currentAddress = newAddr;
|
||||
Wire.end(); // End current I2C
|
||||
delay(10);
|
||||
Wire.begin(currentAddress); // Restart with new address
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// If not a command, treat as speed/direction
|
||||
uint8_t speed = cmd;
|
||||
if (Wire.available() < 1) return;
|
||||
uint8_t direction = Wire.read();
|
||||
|
||||
// Map speed (0-100) to step delay (us): 100 = fast, 1 = slow
|
||||
uint16_t speedDelay = map(speed, 1, 100, 2000, 200); // Tune these values
|
||||
|
||||
// Move a fixed number of steps for demonstration (or make this customizable)
|
||||
moveSteps(200, direction, speedDelay); // 200 steps = 1 rev for 1.8° stepper
|
||||
}
|
||||
|
||||
void I2CStepper::moveSteps(uint16_t steps, uint8_t direction, uint16_t speedDelay) {
|
||||
digitalWrite(DIR_PIN, direction);
|
||||
for (uint16_t i = 0; i < steps; i++) {
|
||||
digitalWrite(STEP_PIN, HIGH);
|
||||
delayMicroseconds(speedDelay);
|
||||
digitalWrite(STEP_PIN, LOW);
|
||||
delayMicroseconds(speedDelay);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef I2CStepper_h
|
||||
#define I2CStepper_h
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
|
||||
// ========== I2CStepper Class ==========
|
||||
|
||||
class I2CStepper {
|
||||
public:
|
||||
I2CStepper(uint8_t address = 0x55);
|
||||
void begin();
|
||||
void setSpeed(uint8_t speed, uint8_t direction);
|
||||
void stop();
|
||||
uint8_t scanDevices(uint8_t* foundAddresses, uint8_t maxDevices);
|
||||
void setAddress(uint8_t newAddress);
|
||||
void handleI2CCommand(); // Only used on the slave
|
||||
|
||||
private:
|
||||
uint8_t _address;
|
||||
void moveSteps(uint16_t steps, uint8_t direction, uint16_t speedDelay);
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user