-
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
1 parent
4c79f35
commit d334620
Showing
40 changed files
with
2,653 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,3 @@ | ||
.idea | ||
cmake-build-debug | ||
build |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 CircuitMess | ||
|
||
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. |
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,11 @@ | ||
name=ByteBoi-Library | ||
version=1.0.0 | ||
author=CircuitMess | ||
maintainer=Filip Budiša <filip@circuitmess.com> | ||
sentence=A library for coding the CircuitMess ByteBoi. | ||
paragraph=An open-source retro game console that you can assemble and code yourself. | ||
category=Device Control | ||
url=https://github.com/CircuitMess/ByteBoi-Library | ||
architectures=esp32 | ||
includes=ByteBoi.h | ||
depends=CircuitOS |
85 changes: 85 additions & 0 deletions
85
libraries/ByteBoi-Library/src/Battery/BatteryPopupService.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,85 @@ | ||
#include "BatteryPopupService.h" | ||
#include <Support/ContextTransition.h> | ||
#include <Support/ModalTransition.h> | ||
#include "ShutdownPopup.h" | ||
#include "WarningPopup.h" | ||
#include "BatteryService.h" | ||
#include "../ByteBoi.h" | ||
#include <Loop/LoopManager.h> | ||
|
||
const uint16_t BatteryPopupService::checkInterval = 5; //in seconds | ||
ShutdownPopup *BatteryPopupService::shutdownPopup = nullptr; | ||
WarningPopup *BatteryPopupService::warningPopup = nullptr; | ||
|
||
void BatteryPopupService::loop(uint time){ | ||
if(!enabled){ | ||
LoopManager::removeListener(this); | ||
return; | ||
} | ||
|
||
checkMicros += time; | ||
//voltage not yet read or charging | ||
if(Battery.getVoltage() == 0 || Battery.chargePinDetected()){ | ||
warningShown = false; | ||
return; | ||
} | ||
uint8_t percentage = Battery.getPercentage(); | ||
|
||
if(checkMicros >= checkInterval * 1000000){ | ||
|
||
if(percentage <= 1){ | ||
if(ContextTransition::isRunning() || | ||
(Modal::getCurrentModal() != nullptr && (Modal::getCurrentModal() == shutdownPopup || Modal::getCurrentModal() == warningPopup)) | ||
|| ModalTransition::isRunning()) return; | ||
|
||
if(Modal::getCurrentModal() != nullptr){ | ||
ModalTransition::setDeleteOnPop(false); | ||
ModalTransition* transition = static_cast<ModalTransition *>((void *)Modal::getCurrentModal()->pop()); | ||
transition->setDoneCallback([](Context *currentContext, Modal *prevModal){ | ||
shutdownPopup = new ShutdownPopup(*currentContext); | ||
shutdownPopup->push(currentContext); | ||
ModalTransition::setDeleteOnPop(true); | ||
}); | ||
}else{ | ||
shutdownPopup = new ShutdownPopup(*Context::getCurrentContext()); | ||
shutdownPopup->push(Context::getCurrentContext()); | ||
} | ||
}else if(percentage <= 15 && !warningShown){ | ||
if(ContextTransition::isRunning() || | ||
(Modal::getCurrentModal() != nullptr && Modal::getCurrentModal() == shutdownPopup) || | ||
ModalTransition::isRunning()) return; | ||
|
||
warningShown = true; | ||
ModalTransition *transition; | ||
|
||
if(Modal::getCurrentModal() != nullptr){ | ||
ModalTransition::setDeleteOnPop(false); | ||
transition = static_cast<ModalTransition *>((void *)Modal::getCurrentModal()->pop()); | ||
transition->setDoneCallback([](Context *currentContext, Modal *prevModal){ | ||
warningPopup = new WarningPopup(*currentContext); | ||
warningPopup->push(currentContext); | ||
warningPopup->returned(prevModal); | ||
ModalTransition::setDeleteOnPop(true); | ||
}); | ||
}else{ | ||
warningPopup = new WarningPopup(*Context::getCurrentContext()); | ||
warningPopup->push(Context::getCurrentContext()); | ||
} | ||
} | ||
checkMicros = 0; | ||
} | ||
} | ||
|
||
void BatteryPopupService::enablePopups(bool enable){ | ||
if(enable){ | ||
Battery.setAutoShutdown(false); | ||
} | ||
|
||
if(enable && !enabled){ | ||
LoopManager::addListener(this); | ||
}else if(!enable && enabled){ | ||
LoopManager::removeListener(this); | ||
} | ||
|
||
enabled = enable; | ||
} |
31 changes: 31 additions & 0 deletions
31
libraries/ByteBoi-Library/src/Battery/BatteryPopupService.h
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,31 @@ | ||
#ifndef BYTEBOI_LIBRARY_BATTERYPOPUPSERVICE_H | ||
#define BYTEBOI_LIBRARY_BATTERYPOPUPSERVICE_H | ||
|
||
#include <Loop/LoopListener.h> | ||
|
||
class WarningPopup; | ||
class ShutdownPopup; | ||
|
||
class BatteryPopupService : public LoopListener{ | ||
public: | ||
void loop(uint time) override; | ||
|
||
/** | ||
* Enable pop-ups. When enabling, this will disable BatteryService auto-shutdown, but won't re-enable | ||
* it when disabling the pop-ups. | ||
* | ||
* Game should be contained within Contexts for this to work. | ||
*/ | ||
void enablePopups(bool enable); | ||
|
||
private: | ||
static const uint16_t checkInterval; | ||
uint checkMicros = 0; | ||
|
||
static ShutdownPopup *shutdownPopup; | ||
static WarningPopup *warningPopup; | ||
bool warningShown = false; | ||
bool enabled = false; | ||
}; | ||
|
||
#endif //BYTEBOI_LIBRARY_BATTERYPOPUPSERVICE_H |
127 changes: 127 additions & 0 deletions
127
libraries/ByteBoi-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,127 @@ | ||
#include "BatteryService.h" | ||
#include "../ByteBoi.h" | ||
#include "../Bitmaps/battery_0.hpp" | ||
#include "../Bitmaps/battery_1.hpp" | ||
#include "../Bitmaps/battery_2.hpp" | ||
#include "../Bitmaps/battery_3.hpp" | ||
#include "../Bitmaps/battery_4.hpp" | ||
#include <SPIFFS.h> | ||
#include <Loop/LoopManager.h> | ||
|
||
const uint16_t BatteryService::measureInterval = 2; //in seconds | ||
const uint16_t BatteryService::measureCount = 10; //in seconds | ||
|
||
void BatteryService::loop(uint micros){ | ||
measureMicros += micros; | ||
if(measureMicros >= (measureInterval * 1000000) / measureCount){ | ||
measureMicros = 0; | ||
measureSum += analogRead(BATTERY_PIN); | ||
measureCounter++; | ||
if(measureCounter == measureCount){ | ||
measureSum = measureSum / measureCount; | ||
voltage = (1.1 * measureSum + 683); | ||
measureCounter = 0; | ||
measureSum = 0; | ||
|
||
if(getLevel() == 0 && autoShutdown && !chargePinDetected()){ | ||
ByteBoi.shutdown(); | ||
return; | ||
} | ||
} | ||
measureMicros = 0; | ||
} | ||
} | ||
|
||
uint8_t BatteryService::getLevel() const{ | ||
uint8_t percentage = getPercentage(); | ||
if(percentage > 80){ | ||
return 4; | ||
}else if(percentage <= 80 && percentage > 40){ | ||
return 3; | ||
}else if(percentage <= 40 && percentage > 15){ | ||
return 2; | ||
}else if(percentage <= 15 && percentage > 0){ | ||
return 1; | ||
}else if(percentage == 0){ | ||
return 0; | ||
} | ||
} | ||
|
||
uint16_t BatteryService::getVoltage() const{ | ||
if(chargePinDetected()){ | ||
return ((float)voltage - (2289.61 - 0.523723*(float)voltage)); | ||
}else{ | ||
return voltage; | ||
} | ||
} | ||
|
||
uint8_t BatteryService::getPercentage() const{ | ||
int16_t percentage = map(getVoltage(), 3650, 4250, 0, 100); | ||
if(percentage < 0){ | ||
return 0; | ||
}else if(percentage > 100){ | ||
return 100; | ||
}else{ | ||
return percentage; | ||
} | ||
} | ||
|
||
void BatteryService::setAutoShutdown(bool enabled){ | ||
autoShutdown = enabled; | ||
} | ||
|
||
void BatteryService::begin(){ | ||
LoopManager::addListener(this); | ||
ByteBoi.getExpander()->pinMode(CHARGE_DETECT_PIN, INPUT_PULLDOWN); | ||
pinMode(BATTERY_PIN, INPUT); | ||
for(int i = 0; i < 5; i++){ | ||
batteryBuffer[i] = static_cast<Color*>(malloc(sizeof(batteryIcon_4))); | ||
} | ||
memcpy_P(batteryBuffer[0],batteryIcon_0,sizeof(batteryIcon_0)); | ||
memcpy_P(batteryBuffer[1],batteryIcon_1,sizeof(batteryIcon_1)); | ||
memcpy_P(batteryBuffer[2],batteryIcon_2,sizeof(batteryIcon_2)); | ||
memcpy_P(batteryBuffer[3],batteryIcon_3,sizeof(batteryIcon_3)); | ||
memcpy_P(batteryBuffer[4],batteryIcon_4,sizeof(batteryIcon_4)); | ||
} | ||
|
||
bool BatteryService::chargePinDetected() const{ | ||
return ByteBoi.getExpander()->getPortState() & (1 << CHARGE_DETECT_PIN); | ||
} | ||
|
||
bool BatteryService::isCharging() const{ | ||
if(getLevel() == 4){ | ||
return false; | ||
}else{ | ||
return chargePinDetected(); | ||
} | ||
} | ||
|
||
void BatteryService::drawIcon(Sprite &sprite, int16_t x, int16_t y, int16_t level){ | ||
if(level != -1){ | ||
if(level > 4 || level < 0) return; | ||
sprite.drawIcon(batteryBuffer[level], x, y, 14, 6, 1, TFT_TRANSPARENT); | ||
return; | ||
} | ||
|
||
Color* buffer = batteryBuffer[getLevel()]; | ||
if(buffer == nullptr) return; | ||
if(!isCharging() && timePassed != 0){ | ||
timePassed = 0; | ||
} | ||
if(isCharging()){ | ||
if(timePassed == 0){ | ||
timePassed = millis(); | ||
pictureIndex = 0; | ||
} | ||
if(millis() - timePassed >= 300){ | ||
timePassed = millis(); | ||
pictureIndex++; | ||
if(pictureIndex > 4){ | ||
pictureIndex = 0; | ||
} | ||
} | ||
sprite.drawIcon(batteryBuffer[pictureIndex], x, y, 14, 6, 1, TFT_TRANSPARENT); | ||
}else{ | ||
sprite.drawIcon(buffer, x, y, 14, 6, 1, TFT_TRANSPARENT); | ||
} | ||
} |
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,39 @@ | ||
#ifndef BYTEBOI_LIBRARY_BATTERYSERVICE_H | ||
#define BYTEBOI_LIBRARY_BATTERYSERVICE_H | ||
|
||
#include <Arduino.h> | ||
#include <Loop/LoopListener.h> | ||
#include <Wire.h> | ||
#include <Display/Sprite.h> | ||
#include <Input/InputListener.h> | ||
|
||
class BatteryService : public LoopListener{ | ||
public: | ||
BatteryService()= default; | ||
|
||
void begin(); | ||
void loop(uint micros) override; | ||
uint16_t getVoltage() const; | ||
uint8_t getLevel() const; | ||
uint8_t getPercentage() const; | ||
void setAutoShutdown(bool enabled); | ||
bool isCharging() const; | ||
bool chargePinDetected() const; | ||
|
||
void drawIcon(Sprite& sprite, int16_t x, int16_t y, int16_t level = -1); | ||
|
||
private: | ||
uint16_t voltage = 0; //in mV | ||
static const uint16_t measureInterval; | ||
static const uint16_t measureCount; | ||
uint measureMicros = 0; | ||
bool autoShutdown = false; | ||
uint8_t level = 0; | ||
Color* batteryBuffer[6] = {nullptr}; | ||
uint32_t timePassed = 0; | ||
uint8_t pictureIndex = 0; | ||
float measureSum = 0; | ||
uint8_t measureCounter = 0; | ||
}; | ||
|
||
#endif //BYTEBOI_LIBRARY_BATTERYSERVICE_H |
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,51 @@ | ||
#include "ShutdownPopup.h" | ||
#include <Loop/LoopManager.h> | ||
#include "../ByteBoi.h" | ||
#include "../Bitmaps/off.hpp" | ||
#include <SPIFFS.h> | ||
|
||
const uint8_t ShutdownPopup::shutdownTime = 5; | ||
|
||
ShutdownPopup::ShutdownPopup(Context &context) : Modal(context, 135, 60){ | ||
screen.getSprite()->setChroma(TFT_TRANSPARENT); | ||
buffer= static_cast<Color*>(malloc(sizeof(off))); | ||
memcpy(buffer,off,sizeof(off)); | ||
|
||
} | ||
ShutdownPopup::~ShutdownPopup(){ | ||
free(buffer); | ||
} | ||
|
||
void ShutdownPopup::draw(){ | ||
Sprite& sprite = *screen.getSprite(); | ||
|
||
sprite.clear(TFT_TRANSPARENT); | ||
sprite.fillRoundRect(0, 0, 135, 60, 10, TFT_BLACK); | ||
sprite.drawIcon(buffer, 5, 15, 30, 30, 1, TFT_TRANSPARENT); | ||
|
||
sprite.setTextColor(TFT_WHITE); | ||
sprite.setTextSize(1); | ||
sprite.setTextFont(2); | ||
sprite.setCursor(screen.getTotalX() + 40, screen.getTotalY() + 12); | ||
sprite.print("Battery empty!"); | ||
sprite.setCursor(screen.getTotalX() + 40, screen.getTotalY() + 32); | ||
sprite.print("Shutting down"); | ||
} | ||
|
||
void ShutdownPopup::start(){ | ||
LoopManager::addListener(this); | ||
draw(); | ||
screen.commit(); | ||
} | ||
|
||
void ShutdownPopup::stop(){ | ||
LoopManager::removeListener(this); | ||
} | ||
|
||
void ShutdownPopup::loop(uint micros){ | ||
shutdownTimer += micros; | ||
if(shutdownTimer >= shutdownTime * 1000000){ | ||
ByteBoi.shutdown(); | ||
} | ||
} | ||
|
Oops, something went wrong.