Skip to content

Commit f427dbf

Browse files
committed
Added Clockstar library
1 parent 032c48c commit f427dbf

16 files changed

Lines changed: 994 additions & 0 deletions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
cmake-build-debug
3+
build
4+
.vscode
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Source directories
2+
file(GLOB_RECURSE SOURCES "src/**.cpp" "src/**.c" "src/**.hpp" "src/**.h")
3+
4+
idf_component_register(SRCS ${SOURCES} INCLUDE_DIRS "src")
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <Arduino.h>
2+
#include <CircuitOS.h>
3+
#include "src/Clockstar.h"
4+
#include "src/Battery/BatteryService.h"
5+
#include <Loop/LoopManager.h>
6+
#include <unordered_map>
7+
Display* display;
8+
9+
std::unordered_map<int, const char*> btnMap = {
10+
{ BTN_SELECT, "Select" },
11+
{ BTN_DOWN, "Down" },
12+
{ BTN_UP, "Up" },
13+
{ BTN_B, "Alt" }
14+
};
15+
16+
class InputTest : public InputListener, public LoopListener {
17+
public:
18+
InputTest(){
19+
LoopManager::addListener(this);
20+
Input::getInstance()->addListener(this);
21+
22+
pinMode(PIN_CHARGE, INPUT);
23+
imu = Clockstar.getIMU();
24+
}
25+
26+
private:
27+
28+
LSM6DS3* imu;
29+
float ax = 0;
30+
float ay = 0;
31+
float az = 0;
32+
33+
void buttonPressed(uint i) override{
34+
button = i;
35+
}
36+
37+
void buttonReleased(uint i) override{
38+
button = -1;
39+
}
40+
41+
void draw(){
42+
auto btn = btnMap.find(button);
43+
44+
Sprite* canvas = display->getBaseSprite();
45+
canvas->clear(TFT_RED);
46+
47+
if(btn != btnMap.end()){
48+
canvas->setCursor(50, 90);
49+
canvas->print(btn->second);
50+
}
51+
52+
canvas->setCursor(4, 105);
53+
canvas->printf("%.1f, %.1f, %.1f", ax, ay, az);
54+
55+
display->commit();
56+
}
57+
58+
void loop(uint micros) override{
59+
60+
ax = imu->readFloatAccelX();
61+
ay = imu->readFloatAccelY();
62+
az = imu->readFloatAccelZ();
63+
64+
draw();
65+
}
66+
67+
int button = -1;
68+
};
69+
70+
void setup(){
71+
Clockstar.begin();
72+
display = Clockstar.getDisplay();
73+
74+
RGB.setSolid(Pixel::Red);
75+
new InputTest();
76+
}
77+
78+
void loop(){
79+
LoopManager::loop();
80+
// Clockstar.getDisplay()->clear(TFT_RED);
81+
// const auto sprite = Clockstar.getDisplay()->getBaseSprite();
82+
//
83+
// sprite->setCursor(0,0);
84+
// sprite->printf("bat: %d, charge: %d\n", Battery.getPercentage(), Battery.charging());
85+
// Clockstar.getDisplay()->commit();
86+
// delay(100);
87+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Clockstar-Library
2+
version=1.0
3+
author=CircuitMess
4+
maintainer=Filip Budiša <filip@circuitmess.com>
5+
sentence=An Arduino library to program Clockstar, a DIY smartwatch.
6+
paragraph=See more on https://circuitmess.com/clockstar/
7+
category=Device Control
8+
url=https://circuitmess.com/clockstar/
9+
architectures=esp32
10+
includes=Clockstar.h
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "BatteryService.h"
2+
#include <Loop/LoopManager.h>
3+
#include <soc/efuse_reg.h>
4+
#include "../Pins.hpp"
5+
#include "../Clockstar.h"
6+
7+
BatteryService Battery;
8+
9+
uint16_t BatteryService::mapReading(uint16_t reading){
10+
int mapped = map(reading, MIN_READ, MAX_READ, MIN_VOLT, MAX_VOLT);
11+
return mapped;
12+
}
13+
14+
void BatteryService::begin(){
15+
LoopManager::addListener(this);
16+
pinMode(PIN_CHARGE, INPUT_PULLDOWN);
17+
pinMode(PIN_BATT, INPUT);
18+
analogSetPinAttenuation(PIN_BATT, ADC_0db);
19+
20+
for(int i = 0; i < MeasureCount; i++){
21+
measureVoltage += analogRead(PIN_BATT);
22+
}
23+
24+
voltage = mapReading(measureVoltage / MeasureCount);
25+
26+
measureVoltage = 0;
27+
measureCount = 0;
28+
measureTime = millis();
29+
30+
if(getVoltage() < MIN_VOLT && !charging()){
31+
Clockstar.shutdown();
32+
return;
33+
}
34+
}
35+
36+
void BatteryService::loop(uint micros){
37+
if(millis() - measureTime <= 1000.0f * MeasureInverval / MeasureCount) return;
38+
39+
measureVoltage += analogRead(PIN_BATT);
40+
measureTime = millis();
41+
42+
if(++measureCount < MeasureCount) return;
43+
44+
voltage = mapReading(measureVoltage / MeasureCount);
45+
measureVoltage = 0;
46+
measureCount = 0;
47+
48+
if(getVoltage() < MIN_VOLT && !charging()){
49+
Clockstar.shutdown();
50+
return;
51+
}
52+
}
53+
54+
uint8_t BatteryService::getLevel() const{
55+
uint8_t percentage = getPercentage();
56+
if(percentage > 80){
57+
return 5;
58+
}else if(percentage > 60){
59+
return 4;
60+
}else if(percentage > 40){
61+
return 3;
62+
}else if(percentage > 20){
63+
return 2;
64+
}else if(percentage >= 5){
65+
return 1;
66+
}else if(percentage < 5){
67+
return 0;
68+
}
69+
}
70+
71+
uint8_t BatteryService::getPercentage() const{
72+
int16_t percentage = map(getVoltage(), MIN_VOLT, MAX_VOLT, 0, 100);
73+
if(percentage < 0){
74+
return 0;
75+
}else if(percentage > 100){
76+
return 100;
77+
}else{
78+
return percentage;
79+
}
80+
}
81+
82+
uint16_t BatteryService::getVoltage() const{
83+
return voltage + getVoltOffset();
84+
}
85+
86+
int16_t BatteryService::getVoltOffset(){
87+
uint32_t upper = REG_GET_FIELD(EFUSE_BLK3_RDATA3_REG, EFUSE_RD_ADC1_TP_HIGH);
88+
uint32_t lower = REG_GET_FIELD(EFUSE_BLK3_RDATA3_REG, EFUSE_RD_ADC1_TP_LOW);
89+
return (upper << 7) | lower;
90+
}
91+
92+
bool BatteryService::charging() const{
93+
return digitalRead(PIN_CHARGE);
94+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#ifndef CLOCKSTAR_LIBRARY_BATTERYSERVICE_H
2+
#define CLOCKSTAR_LIBRARY_BATTERYSERVICE_H
3+
4+
#include <Arduino.h>
5+
#include <Loop/LoopListener.h>
6+
7+
#define MAX_VOLT 4200
8+
#define MIN_VOLT 3600
9+
#define MAX_READ 3550 // 4.2V
10+
#define MIN_READ 3050 // 3.6V
11+
12+
13+
class BatteryService : private LoopListener {
14+
public:
15+
BatteryService() = default;
16+
17+
void loop(uint micros) override;
18+
void begin();
19+
uint8_t getLevel() const;
20+
uint8_t getPercentage() const;
21+
uint16_t getVoltage() const;
22+
23+
static uint16_t mapReading(uint16_t reading);
24+
25+
static int16_t getVoltOffset();
26+
27+
bool charging() const;
28+
29+
private:
30+
static constexpr float MeasureInverval = 2;
31+
static constexpr uint8_t MeasureCount = 30;
32+
33+
uint32_t measureVoltage = 0;
34+
uint8_t measureCount = 0;
35+
uint32_t measureTime = 0;
36+
37+
uint16_t voltage = 0;
38+
39+
};
40+
41+
extern BatteryService Battery;
42+
43+
#endif //CLOCKSTAR_LIBRARY_BATTERYSERVICE_H

0 commit comments

Comments
 (0)