Skip to content

Commit

Permalink
first code
Browse files Browse the repository at this point in the history
  • Loading branch information
BjoernLuig committed Nov 28, 2024
1 parent 05778e5 commit aeac2a3
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/serial-test/serial-test.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <SolarCharger.h>
#include <Wire.h>

SolarCharger solar;

void setup() {
Wire.begin();
Serial.begin(9600);
}

void loop() {
solar.update();

if (solar.isConnected()) {
Serial.print("Solar Panel Voltage: ");
Serial.println(solar.getSolarPanelVoltage());
Serial.print("Battery Voltage: ");
Serial.println(solar.getBatteryVoltage());
Serial.print("Battery Level: ");
Serial.println(solar.getBatteryLevel());
Serial.print("Charging: ");
Serial.println(solar.isCharging() ? "Yes" : "No");
Serial.print("Fast Charging: ");
Serial.println(solar.isFastCharging() ? "Yes" : "No");
Serial.print("Battery Temperature: ");
Serial.println(solar.getBatteryTemperature());
} else {
Serial.println("Solar charger not connected.");
}

delay(1000);
}
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=SolarChargerSB041
version=1.0.0
author=Dein Name
maintainer=Dein Name <[email protected]>
sentence=Eine Arduino-Bibliothek für das Auslesen des Solarladereglers SB-041.
paragraph=Diese Bibliothek ermöglicht das einfache Auslesen von Daten wie Batterie- und Solarpanel-Spannung, Ladezustand und Temperatur.
category=Sensors
url=https://github.com/dein-repo
architectures=*
64 changes: 64 additions & 0 deletions src/SolarChargerSB041.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "SolarChargerSB041.h"

SolarChargerSB041::SolarChargerSB041(uint8_t address)
: address(address),
connected(false),
solarPanelVoltage(-1),
batteryVoltage(-1),
batteryLevel(-1),
charging(false),
fastCharging(false),
batteryTemperature(-1) {}

void SolarChargerSB041::update() {
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0 || error == 2) {
connected = (error == 0);
Wire.requestFrom((uint8_t)address, (uint8_t)4);

uint8_t vbat_raw = Wire.read();
batteryVoltage = 0.02 * vbat_raw;

uint8_t vin_raw = Wire.read();
solarPanelVoltage = 0.1 * vin_raw;

uint8_t flags = Wire.read();
charging = flags & 1;
if (flags & 32)
batteryLevel = 4;
else if (flags & 16)
batteryLevel = 3;
else if (flags & 8)
batteryLevel = 2;
else if (flags & 4)
batteryLevel = 1;
else
batteryLevel = 0;

fastCharging = flags & 64;
charging = flags & 128;

batteryTemperature = (int8_t)(Wire.read());
} else {
connected = false;
fastCharging = false;
charging = false;
batteryVoltage = -1;
solarPanelVoltage = -1;
batteryLevel = -1;
batteryTemperature = -1;
}
}

bool SolarChargerSB041::isConnected() const { return connected; }
float SolarChargerSB041::getSolarPanelVoltage() const {
return solarPanelVoltage;
}
float SolarChargerSB041::getBatteryVoltage() const { return batteryVoltage; }
int SolarChargerSB041::getBatteryLevel() const { return batteryLevel; }
bool SolarChargerSB041::isCharging() const { return charging; }
bool SolarChargerSB041::isFastCharging() const { return fastCharging; }
float SolarChargerSB041::getBatteryTemperature() const {
return batteryTemperature;
}
60 changes: 60 additions & 0 deletions src/SolarChargerSB041.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#ifndef SOLARCHARGER_H
#define SOLARCHARGER_H

#include <Wire.h>

class SolarCharger {
public:
SolarCharger(uint8_t address = 0x32);
#ifndef SOLARCHARGER_H
#define SOLARCHARGER_H

#include <Wire.h>

class SolarCharger {
public:
SolarCharger(uint8_t address = 0x32);

void update();
bool isConnected() const;
float getSolarPanelVoltage() const;
float getBatteryVoltage() const;
int getBatteryLevel() const;
bool isCharging() const;
bool isFastCharging() const;
float getBatteryTemperature() const;

private:
uint8_t address;
bool connected;
float solarPanelVoltage;
float batteryVoltage;
int batteryLevel;
bool charging;
bool fastCharging;
float batteryTemperature;
};

#endif // SOLARCHARGER_H

void update();
bool isConnected() const;
float getSolarPanelVoltage() const;
float getBatteryVoltage() const;
int getBatteryLevel() const;
bool isCharging() const;
bool isFastCharging() const;
float getBatteryTemperature() const;

private:
uint8_t address;
bool connected;
float solarPanelVoltage;
float batteryVoltage;
int batteryLevel;
bool charging;
bool fastCharging;
float batteryTemperature;
};

#endif // SOLARCHARGER_H

0 comments on commit aeac2a3

Please sign in to comment.