Skip to content

Instantly share code, notes, and snippets.

@quentinchap
Created October 16, 2025 18:15
Show Gist options
  • Select an option

  • Save quentinchap/cecabc8346a70a8dbaf636f71f974010 to your computer and use it in GitHub Desktop.

Select an option

Save quentinchap/cecabc8346a70a8dbaf636f71f974010 to your computer and use it in GitHub Desktop.
#include "TLx493D_inc.hpp"
#include <SimpleKalmanFilter.h>
#include <OneButton.h>
#include <Adafruit_NeoPixel.h>
// How many internal neopixels do we have? some boards have more than one!
#define NUMPIXELS 1
Adafruit_NeoPixel pixels(NUMPIXELS, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
using namespace ifx::tlx493d;
/* Definition of the power pin and sensor objects for Kit2Go XMC1100 boards. */
const uint8_t POWER_PIN = 15; // XMC1100 : LED2
// some board swith multiple I2C need Wire --> Wire1
TLx493D_A1B6 mag(Wire1, TLx493D_IIC_ADDR_A0_e);
SimpleKalmanFilter xFilter(1, 1, 0.2), yFilter(1, 1, 0.2), zFilter(1, 1, 0.2);
// Setup buttons
OneButton button1(27, true);
OneButton button2(24, true);
float xOffset = 0, yOffset = 0, zOffset = 0;
float xCurrent = 0, yCurrent = 0, zCurrent = 0;
int calSamples = 300;
/** Definition of a counter variable. */
uint8_t count = 0;
void calibrate() {
double x, y, z;
Wire1.begin();
// mag sensor init
mag.setPowerPin(POWER_PIN, OUTPUT, INPUT, HIGH, LOW, 0, 250000);
mag.begin();
for (int i = 1; i <= calSamples; i++) {
mag.getMagneticField(&x, &y, &z);
xOffset += x;
yOffset += y;
zOffset += z;
}
xOffset = xOffset / calSamples;
yOffset = yOffset / calSamples;
zOffset = zOffset / calSamples;
}
// the setup routine runs once when you press reset:
void setup() {
// Red LED at start
#if defined(NEOPIXEL_POWER)
pinMode(NEOPIXEL_POWER, OUTPUT);
digitalWrite(NEOPIXEL_POWER, HIGH);
#endif
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
pixels.setBrightness(4); // not so bright
pixels.fill(0xFF0000);
pixels.show();
Serial.begin(115200);
if (Serial) {
Serial.println("Waiting for other USB ports");
}
delay(1000);
Serial.print("You are port 0\n\r\n0> ");
calibrate();
}
void getMagnet() {
double x, y, z;
mag.getMagneticField(&x, &y, &z);
xCurrent = xFilter.updateEstimate(x - xOffset);
yCurrent = yFilter.updateEstimate(y - yOffset);
zCurrent = zFilter.updateEstimate(z - zOffset);
static uint32_t ms = 0;
static uint32_t ms2 = 0;
Serial.print(xCurrent);
Serial.print(",");
Serial.print(yCurrent);
Serial.print(",");
Serial.print(zCurrent);
Serial.println();
}
// the loop routine runs over and over again forever:
void loop() {
pixels.fill(0x33ff33);
pixels.show();
// keep watching the push buttons
//button1.tick();
//button2.tick();
getMagnet();
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment