-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
994 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
cmake-build-debug | ||
build | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Source directories | ||
file(GLOB_RECURSE SOURCES "src/**.cpp" "src/**.c" "src/**.hpp" "src/**.h") | ||
|
||
idf_component_register(SRCS ${SOURCES} INCLUDE_DIRS "src") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#include <Arduino.h> | ||
#include <CircuitOS.h> | ||
#include "src/Clockstar.h" | ||
#include "src/Battery/BatteryService.h" | ||
#include <Loop/LoopManager.h> | ||
#include <unordered_map> | ||
Display* display; | ||
|
||
std::unordered_map<int, const char*> btnMap = { | ||
{ BTN_SELECT, "Select" }, | ||
{ BTN_DOWN, "Down" }, | ||
{ BTN_UP, "Up" }, | ||
{ BTN_B, "Alt" } | ||
}; | ||
|
||
class InputTest : public InputListener, public LoopListener { | ||
public: | ||
InputTest(){ | ||
LoopManager::addListener(this); | ||
Input::getInstance()->addListener(this); | ||
|
||
pinMode(PIN_CHARGE, INPUT); | ||
imu = Clockstar.getIMU(); | ||
} | ||
|
||
private: | ||
|
||
LSM6DS3* imu; | ||
float ax = 0; | ||
float ay = 0; | ||
float az = 0; | ||
|
||
void buttonPressed(uint i) override{ | ||
button = i; | ||
} | ||
|
||
void buttonReleased(uint i) override{ | ||
button = -1; | ||
} | ||
|
||
void draw(){ | ||
auto btn = btnMap.find(button); | ||
|
||
Sprite* canvas = display->getBaseSprite(); | ||
canvas->clear(TFT_RED); | ||
|
||
if(btn != btnMap.end()){ | ||
canvas->setCursor(50, 90); | ||
canvas->print(btn->second); | ||
} | ||
|
||
canvas->setCursor(4, 105); | ||
canvas->printf("%.1f, %.1f, %.1f", ax, ay, az); | ||
|
||
display->commit(); | ||
} | ||
|
||
void loop(uint micros) override{ | ||
|
||
ax = imu->readFloatAccelX(); | ||
ay = imu->readFloatAccelY(); | ||
az = imu->readFloatAccelZ(); | ||
|
||
draw(); | ||
} | ||
|
||
int button = -1; | ||
}; | ||
|
||
void setup(){ | ||
Clockstar.begin(); | ||
display = Clockstar.getDisplay(); | ||
|
||
RGB.setSolid(Pixel::Red); | ||
new InputTest(); | ||
} | ||
|
||
void loop(){ | ||
LoopManager::loop(); | ||
// Clockstar.getDisplay()->clear(TFT_RED); | ||
// const auto sprite = Clockstar.getDisplay()->getBaseSprite(); | ||
// | ||
// sprite->setCursor(0,0); | ||
// sprite->printf("bat: %d, charge: %d\n", Battery.getPercentage(), Battery.charging()); | ||
// Clockstar.getDisplay()->commit(); | ||
// delay(100); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name=Clockstar-Library | ||
version=1.0 | ||
author=CircuitMess | ||
maintainer=Filip Budiša <filip@circuitmess.com> | ||
sentence=An Arduino library to program Clockstar, a DIY smartwatch. | ||
paragraph=See more on https://circuitmess.com/clockstar/ | ||
category=Device Control | ||
url=https://circuitmess.com/clockstar/ | ||
architectures=esp32 | ||
includes=Clockstar.h |
94 changes: 94 additions & 0 deletions
94
libraries/Clockstar-Library/src/Battery/BatteryService.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#include "BatteryService.h" | ||
#include <Loop/LoopManager.h> | ||
#include <soc/efuse_reg.h> | ||
#include "../Pins.hpp" | ||
#include "../Clockstar.h" | ||
|
||
BatteryService Battery; | ||
|
||
uint16_t BatteryService::mapReading(uint16_t reading){ | ||
int mapped = map(reading, MIN_READ, MAX_READ, MIN_VOLT, MAX_VOLT); | ||
return mapped; | ||
} | ||
|
||
void BatteryService::begin(){ | ||
LoopManager::addListener(this); | ||
pinMode(PIN_CHARGE, INPUT_PULLDOWN); | ||
pinMode(PIN_BATT, INPUT); | ||
analogSetPinAttenuation(PIN_BATT, ADC_0db); | ||
|
||
for(int i = 0; i < MeasureCount; i++){ | ||
measureVoltage += analogRead(PIN_BATT); | ||
} | ||
|
||
voltage = mapReading(measureVoltage / MeasureCount); | ||
|
||
measureVoltage = 0; | ||
measureCount = 0; | ||
measureTime = millis(); | ||
|
||
if(getVoltage() < MIN_VOLT && !charging()){ | ||
Clockstar.shutdown(); | ||
return; | ||
} | ||
} | ||
|
||
void BatteryService::loop(uint micros){ | ||
if(millis() - measureTime <= 1000.0f * MeasureInverval / MeasureCount) return; | ||
|
||
measureVoltage += analogRead(PIN_BATT); | ||
measureTime = millis(); | ||
|
||
if(++measureCount < MeasureCount) return; | ||
|
||
voltage = mapReading(measureVoltage / MeasureCount); | ||
measureVoltage = 0; | ||
measureCount = 0; | ||
|
||
if(getVoltage() < MIN_VOLT && !charging()){ | ||
Clockstar.shutdown(); | ||
return; | ||
} | ||
} | ||
|
||
uint8_t BatteryService::getLevel() const{ | ||
uint8_t percentage = getPercentage(); | ||
if(percentage > 80){ | ||
return 5; | ||
}else if(percentage > 60){ | ||
return 4; | ||
}else if(percentage > 40){ | ||
return 3; | ||
}else if(percentage > 20){ | ||
return 2; | ||
}else if(percentage >= 5){ | ||
return 1; | ||
}else if(percentage < 5){ | ||
return 0; | ||
} | ||
} | ||
|
||
uint8_t BatteryService::getPercentage() const{ | ||
int16_t percentage = map(getVoltage(), MIN_VOLT, MAX_VOLT, 0, 100); | ||
if(percentage < 0){ | ||
return 0; | ||
}else if(percentage > 100){ | ||
return 100; | ||
}else{ | ||
return percentage; | ||
} | ||
} | ||
|
||
uint16_t BatteryService::getVoltage() const{ | ||
return voltage + getVoltOffset(); | ||
} | ||
|
||
int16_t BatteryService::getVoltOffset(){ | ||
uint32_t upper = REG_GET_FIELD(EFUSE_BLK3_RDATA3_REG, EFUSE_RD_ADC1_TP_HIGH); | ||
uint32_t lower = REG_GET_FIELD(EFUSE_BLK3_RDATA3_REG, EFUSE_RD_ADC1_TP_LOW); | ||
return (upper << 7) | lower; | ||
} | ||
|
||
bool BatteryService::charging() const{ | ||
return digitalRead(PIN_CHARGE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#ifndef CLOCKSTAR_LIBRARY_BATTERYSERVICE_H | ||
#define CLOCKSTAR_LIBRARY_BATTERYSERVICE_H | ||
|
||
#include <Arduino.h> | ||
#include <Loop/LoopListener.h> | ||
|
||
#define MAX_VOLT 4200 | ||
#define MIN_VOLT 3600 | ||
#define MAX_READ 3550 // 4.2V | ||
#define MIN_READ 3050 // 3.6V | ||
|
||
|
||
class BatteryService : private LoopListener { | ||
public: | ||
BatteryService() = default; | ||
|
||
void loop(uint micros) override; | ||
void begin(); | ||
uint8_t getLevel() const; | ||
uint8_t getPercentage() const; | ||
uint16_t getVoltage() const; | ||
|
||
static uint16_t mapReading(uint16_t reading); | ||
|
||
static int16_t getVoltOffset(); | ||
|
||
bool charging() const; | ||
|
||
private: | ||
static constexpr float MeasureInverval = 2; | ||
static constexpr uint8_t MeasureCount = 30; | ||
|
||
uint32_t measureVoltage = 0; | ||
uint8_t measureCount = 0; | ||
uint32_t measureTime = 0; | ||
|
||
uint16_t voltage = 0; | ||
|
||
}; | ||
|
||
extern BatteryService Battery; | ||
|
||
#endif //CLOCKSTAR_LIBRARY_BATTERYSERVICE_H |
Oops, something went wrong.