add compass

This commit is contained in:
Egor
2025-01-11 20:43:24 -08:00
parent 4df5d99aaa
commit e140a172d2
3 changed files with 64 additions and 1 deletions

View File

@@ -25,6 +25,8 @@
#ifdef LILYGO
#define BUZZER_PIN 45
#define COMPASS_SCL 42
#define COMPASS_SDA 46
#endif
#ifdef T3_V1_6_SX1276
#define BUZZER_PIN 35

View File

@@ -232,6 +232,7 @@ lib_deps =
U8g2
XPowersLib
ESP Async WebServer
https://github.com/Genaker/HMC5883L_Simple
build_flags =
-DLILYGO
-DT3_S3_V1_2_LR1121
@@ -247,6 +248,7 @@ build_flags =
-DARDUINO_USB_CDC_ON_BOOT=1
-DARDUINO_LILYGO_T3_S3_V1_X
-DARDUINO_USB_MODE=1
-DCOMPASS_ENABLED
[env:lilygo-T3S3-v1-2-sx1280]
platform = espressif32

View File

@@ -29,6 +29,7 @@
#include <ESPAsyncWebServer.h>
#include <File.h>
#include <LittleFS.h>
#include <Wire.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <unordered_map>
@@ -112,13 +113,21 @@ unsigned int osdCyclesCount = 0;
#define SIDEBAR_DB_LEVEL 80 // Absolute value without minus
#define SIDEBAR_DB_DELTA 2 // detect changes <> threshold
#ifdef LILYGO
#define OSD_SCK 38
#define OSD_CS 39
#define OSD_MISO 40
#define OSD_MOSI 41
#else
// SPI pins
#define OSD_SCK 26
#define OSD_CS 47
#define OSD_MISO 33
#define OSD_MOSI 34
#define OSD_SCK 26
#endif
#endif // End OSD_ENABLED
#define OSD_WIDTH 30
#define OSD_HEIGHT 16
#define OSD_CHART_WIDTH 15
@@ -152,6 +161,10 @@ DFRobot_OSD osd(OSD_CS);
#include "global_config.h"
#include "ui.h"
#ifdef COMPASS_ENABLED
#include <HMC5883L_Simple.h>
HMC5883L_Simple Compass;
#endif
// -----------------------------------------------------------------
// CONFIGURATION OPTIONS
// -----------------------------------------------------------------
@@ -1462,6 +1475,39 @@ void setup(void)
r.addEventListener(ALL_EVENTS, eventListenerForReport, NULL);
#ifdef COMPASS_ENABLED
Wire1.end();
Wire1.begin(46, 42);
// Wire1.begin();
Serial.println("Scanning...");
for (uint8_t addr = 1; addr < 127; addr++)
{
Wire1.beginTransmission(addr);
if (Wire1.endTransmission() == 0)
{
Serial.print("I2C device found at address 0x");
Serial.println(addr, HEX);
}
}
Serial.println("Done.");
Compass.SetDeclination(23, 35, 'E');
Compass.SetSamplingMode(COMPASS_SINGLE);
Compass.SetScale(COMPASS_SCALE_130);
// To allow you to mount the compass in different ways you can specify the
// orientation:
// COMPASS_HORIZONTAL_X_NORTH (default), the compass is oriented horizontally,
// top-side up. when pointing North the X silkscreen arrow will point North
// COMPASS_HORIZONTAL_Y_NORTH, top-side up, Y is the needle,when pointing North the
// Y silkscreen arrow will point North COMPASS_VERTICAL_X_EAST, vertically
// mounted (tall) looking at the top side, when facing North the X silkscreen arrow
// will point East COMPASS_VERTICAL_Y_WEST, vertically mounted (wide) looking at
// the top side, when facing North the Y silkscreen arrow will point West
Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
Serial.println("QMC5883L Compass Init Done");
Serial.println("Turn compass in all directions to calibrate....");
#endif
#ifdef UPTIME_CLOCK
uptime = new UptimeClock(display, millis());
#endif
@@ -1970,6 +2016,19 @@ void loop(void)
doScan();
reportScan();
}
#ifdef COMPASS_ENABLED
float heading = Compass.GetHeadingDegrees();
if (heading == 0)
{
/* Still calibrating, so measure but don't print */
}
else
{
Serial.println("Compass Heading: " + String(heading));
display.drawString(40, 0, String(heading));
display.display();
}
#endif
}
void doScan()