adding files

This commit is contained in:
2026-03-21 23:51:53 +01:00
commit 8cb0240ca2
19 changed files with 2162 additions and 0 deletions

28
i2c_encoder/encoder.cpp Normal file
View File

@@ -0,0 +1,28 @@
#include "encoder.h"
// public:
// constructor : sets pins as inputs and turns on pullup resistors
RotaryEncoder::RotaryEncoder( const String& argName, int argClkPin, int argDtPin, int argPinMode) : name(argName), clkPin ( argClkPin), dtPin( argDtPin )
{
// set pin a and b to be input with pull up enabled
pinMode(clkPin, argPinMode);
pinMode(dtPin, argPinMode);
position = 0;
}
void RotaryEncoder::update(void)
{
digitalRead(dtPin) ? position++ : position--;
}
int RotaryEncoder::getPosition ()
{
return position;
}
void RotaryEncoder::clearPosition()
{
position = 0;
}

64
i2c_encoder/encoder.h Normal file
View File

@@ -0,0 +1,64 @@
#ifndef __C_ROTARY_ENCODER_H__
#define __C_ROTARY_ENCODER_H__
#include <Arduino.h>
class RotaryEncoder {
/*
wraps encoder setup and update functions in a class
!!! NOTE : user must call the encoders update method from an
interrupt function himself! i.e. user must attach an interrupt to the
encoder pin A and call the encoder update method from within the
interrupt
uses Arduino pull-ups on A & B channel outputs
turning on the pull-ups saves having to hook up resistors
to the A & B channel outputs
// ------------------------------------------------------------------------------------------------
// Example usage :
// ------------------------------------------------------------------------------------------------
#include "Encoder.h"
Encoder encoder(2, 4);
void setup() {
attachInterrupt(0, doEncoder, CHANGE);
Serial.begin (115200);
Serial.println("start");
}
void loop(){
// do some stuff here - the joy of interrupts is that they take care of themselves
}
void doEncoder(){
encoder.update();
Serial.println( encoder.getPosition() );
}
// ------------------------------------------------------------------------------------------------
// Example usage end
// ------------------------------------------------------------------------------------------------
*/
public:
// constructor : sets pins as inputs and turns on pullup resistors
RotaryEncoder( const String& argName, int argClkPin, int argDtPin, int argPinMode = INPUT_PULLUP);
~RotaryEncoder() {};
// call this from your interrupt function
void update();
int getPosition();
void clearPosition();
private:
String name;
volatile int position;
int clkPin; // clock pin
int dtPin; // direction pin
};
#endif // __C_ROTARY_ENCODER_H__

View File

@@ -0,0 +1,58 @@
#include <Wire.h>
#include "encoder.h"
constexpr auto kSlaveAddress = 0x08; // I2C address for Arduino
constexpr auto kXEncoderClockPin = 2;
constexpr auto kXEncoderDirectionPin = 8;
constexpr auto kZEncoderClockPin = 3;
constexpr auto kZEncoderDirectionPin = 9;
static void x_isr(void);
static void z_isr(void);
static void sendData(void);
static RotaryEncoder x_encoder{"X", kXEncoderClockPin, kXEncoderDirectionPin, INPUT};
static RotaryEncoder z_encoder{"Z", kZEncoderClockPin, kZEncoderDirectionPin, INPUT_PULLUP};
static char data[4] = {0, 0, 0, 0};
void setup(){
//Serial.begin(38400);
//Serial.setTimeout(500);
//Serial.println("setup()");
Wire.begin(kSlaveAddress);
Wire.onRequest(sendData);
attachInterrupt(digitalPinToInterrupt(kXEncoderClockPin), x_isr, RISING);
attachInterrupt(digitalPinToInterrupt(kZEncoderClockPin), z_isr, RISING);
}
void loop() {
/*Serial.println(x_encoder.getPosition());
Serial.println(z_encoder.getPosition());
delay(250);*/
}
// Handle request to send I2C data
void sendData() {
int x_pos = x_encoder.getPosition();
int z_pos = z_encoder.getPosition();
data[0] = 0x00ff & x_pos;
data[1] = (0xff00 & x_pos) >> 8;
data[2] = 0x00ff & z_pos;
data[3] = (0xff00 & z_pos) >> 8;
Wire.write(data, 4);
}
static void x_isr(void)
{
x_encoder.update();
}
static void z_isr(void)
{
z_encoder.update();
}