Skip to content

Commit

Permalink
Rewrite Network and UdpClient into a classes (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevMinerTV committed Jul 14, 2023
1 parent fe6c253 commit ed74944
Show file tree
Hide file tree
Showing 29 changed files with 1,010 additions and 985 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
51 changes: 28 additions & 23 deletions src/GlobalVars.h
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
/*
SlimeVR Code is placed under the MIT license
Copyright (c) 2022 TheDevMinerTV
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
SlimeVR Code is placed under the MIT license
Copyright (c) 2022 TheDevMinerTV
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef GLOBALVARS_H
#define GLOBALVARS_H

#include <arduino-timer.h>

#include "LEDManager.h"
#include "status/StatusManager.h"
#include "configuration/Configuration.h"
#include "network/connection.h"
#include "network/manager.h"
#include "sensors/SensorManager.h"
#include <arduino-timer.h>
#include "status/StatusManager.h"

extern Timer<> globalTimer;
extern SlimeVR::LEDManager ledManager;
extern SlimeVR::Status::StatusManager statusManager;
extern SlimeVR::Configuration::Configuration configuration;
extern SlimeVR::Sensors::SensorManager sensorManager;
extern Timer<> globalTimer;
extern SlimeVR::Network::Manager networkManager;
extern SlimeVR::Network::Connection networkConnection;

#endif
8 changes: 4 additions & 4 deletions src/batterymonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ void BatteryMonitor::Loop()
voltage = -1;
#if ESP8266 && (BATTERY_MONITOR == BAT_INTERNAL || BATTERY_MONITOR == BAT_INTERNAL_MCP3021)
// Find out what your max measurement is (voltage_3_3).
// Take the max measurement and check if it was less than 50mV
// Take the max measurement and check if it was less than 50mV
// if yes output 5.0V
// if no output 3.3V - dropvoltage + 0.1V
// if no output 3.3V - dropvoltage + 0.1V
auto ESPmV = ESP.getVcc();
if (ESPmV > voltage_3_3)
{
Expand Down Expand Up @@ -116,7 +116,7 @@ void BatteryMonitor::Loop()
level = 1;
else if (level < 0)
level = 0;
Network::sendBatteryLevel(voltage, level);
networkConnection.sendBatteryLevel(voltage, level);
#ifdef BATTERY_LOW_POWER_VOLTAGE
if (voltage < BATTERY_LOW_POWER_VOLTAGE)
{
Expand All @@ -132,4 +132,4 @@ void BatteryMonitor::Loop()
}
}
#endif
}
}
3 changes: 1 addition & 2 deletions src/batterymonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include <Arduino.h>
#include "globals.h"
#include "network/network.h"
#include <i2cscan.h>
#include <I2Cdev.h>
#include "logging/Logger.h"
Expand Down Expand Up @@ -62,7 +61,7 @@
// Wemos D1 Mini has an internal Voltage Divider with R1=100K and R2=220K > this means, 3.3V analogRead input voltage results in 1023.0
// Wemos D1 Mini with Wemos Battery Shield v1.2.0 or higher: Battery Shield with J2 closed, has an additional 130K resistor. So the resulting Voltage Divider is R1=220K+100K=320K and R2=100K > this means, 4.5V analogRead input voltage results in 1023.0
// ESP32 Boards may have not the internal Voltage Divider. Also ESP32 has a 12bit ADC (0..4095). So R1 and R2 can be changed.
// Diagramm:
// Diagramm:
// (Battery)--- [BATTERY_SHIELD_RESISTANCE] ---(INPUT_BOARD)--- [BATTERY_SHIELD_R2] ---(ESP_INPUT)--- [BATTERY_SHIELD_R1] --- (GND)
// SlimeVR Board can handle max 5V > so analogRead of 5.0V input will result in 1023.0
#define batteryADCMultiplier ADCVoltageMax / ADCResulution * (BATTERY_SHIELD_R1 + BATTERY_SHIELD_R2 + BATTERY_SHIELD_RESISTANCE) / BATTERY_SHIELD_R1
Expand Down
17 changes: 9 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@
#include "Wire.h"
#include "ota.h"
#include "GlobalVars.h"
#include "network/network.h"
#include "globals.h"
#include "credentials.h"
#include <i2cscan.h>
#include "serial/serialcommands.h"
#include "batterymonitor.h"
#include "logging/Logger.h"

Timer<> globalTimer;
SlimeVR::Logging::Logger logger("SlimeVR");
SlimeVR::Sensors::SensorManager sensorManager;
SlimeVR::LEDManager ledManager(LED_PIN);
SlimeVR::Status::StatusManager statusManager;
SlimeVR::Configuration::Configuration configuration;
Timer<> globalTimer;
SlimeVR::Network::Manager networkManager;
SlimeVR::Network::Connection networkConnection;

int sensorToCalibrate = -1;
bool blinking = false;
Expand All @@ -52,7 +53,7 @@ void setup()
Serial.begin(serialBaudRate);
globalTimer = timer_create_default();

#ifdef ESP32C3
#ifdef ESP32C3
// Wait for the Computer to be able to connect.
delay(2000);
#endif
Expand Down Expand Up @@ -82,7 +83,7 @@ void setup()
#endif

// using `static_cast` here seems to be better, because there are 2 similar function signatures
Wire.begin(static_cast<int>(PIN_IMU_SDA), static_cast<int>(PIN_IMU_SCL));
Wire.begin(static_cast<int>(PIN_IMU_SDA), static_cast<int>(PIN_IMU_SCL));

#ifdef ESP8266
Wire.setClockStretchLimit(150000L); // Default stretch limit 150mS
Expand All @@ -94,10 +95,10 @@ void setup()

// Wait for IMU to boot
delay(500);

sensorManager.setup();
Network::setUp();

networkManager.setup();
OTA::otaSetup(otaPassword);
battery.Setup();

Expand All @@ -113,7 +114,7 @@ void loop()
globalTimer.tick();
SerialCommands::update();
OTA::otaUpdate();
Network::update(sensorManager.getFirst(), sensorManager.getSecond());
networkManager.update();
sensorManager.update();
battery.Loop();
ledManager.update();
Expand Down
Loading

0 comments on commit ed74944

Please sign in to comment.