Skip to content

Commit

Permalink
Reinstatement of the Adafruit_MCP4725 library
Browse files Browse the repository at this point in the history
  • Loading branch information
m1cr0lab committed Oct 24, 2021
1 parent a132a90 commit e718f55
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ However, note that this project is experimental and therefore is likely to evolv
At the stage where I am today, I use the following libraries to simplify the use of the various peripherals of the microcontroller:

- [Adafruit MCP23017 Arduino Library][mcp23017]
- [Adafruit MCP4725 Arduino Library][mcp4725]
- [LovyanGFX][lovyangfx]

I noticed that most of the developments around the ESPboy adopted Bodmer's [TFT_eSPI][tftespi] library, but I personally preferred to use the [LovyanGFX][lovyangfx] library, which provides impressive optimizations. Its author, lovyan03, has kindly agreed to [add support for ESP8266][lovyan8266] to make our life with ESPboy easier. You can thank him by starring :star: his library.
Expand All @@ -27,6 +28,7 @@ You can try out the applications without compiling them by uploading the precomp
[espboy]: https://www.espboy.com/
[platformio]: https://platformio.org/
[mcp23017]: https://github.com/adafruit/Adafruit-MCP23017-Arduino-Library
[mcp4725]: https://github.com/adafruit/Adafruit_MCP4725
[lovyangfx]: https://github.com/lovyan03/LovyanGFX
[tftespi]: https://github.com/Bodmer/TFT_eSPI
[lovyan8266]: https://github.com/lovyan03/LovyanGFX/issues/130
Expand Down
34 changes: 28 additions & 6 deletions lib/ESPboy/ESPboy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@ void ESPboy::begin(const bool show_logo) {
_frame_count = _fps = 0;

_initTFT();
_initMCP4725();
_initMCP23017();

if (show_logo) {

tft.setBrightness(0);
_dac.setVoltage(0, false);
_drawLogo();
fadeIn(); while (_fading.active) _fade();
fadeOut(); while (_fading.active) _fade();
tft.fillScreen(0);
fadeIn();

} else tft.setBrightness(0xff);
} else _dac.setVoltage(4095, false);

}

Expand Down Expand Up @@ -58,6 +59,12 @@ void ESPboy::_initTFT() {

}

void ESPboy::_initMCP4725() {

_dac.begin(0x60);

}

void ESPboy::_initMCP23017() {

_mcp.begin_I2C();
Expand Down Expand Up @@ -125,7 +132,7 @@ bool ESPboy::fading() const { return _fading.active; }
void ESPboy::fadeIn() {

_fading.last_us = micros();
_fading.level = _TFT_BRIGHTNESS_MIN;
_fading.level = _DAC_MIN;
_fading.inc = true;
_fading.active = true;

Expand All @@ -134,7 +141,7 @@ void ESPboy::fadeIn() {
void ESPboy::fadeOut() {

_fading.last_us = micros();
_fading.level = _TFT_BRIGHTNESS_MAX;
_fading.level = _DAC_MAX;
_fading.inc = false;
_fading.active = true;

Expand All @@ -146,10 +153,25 @@ void ESPboy::_fade() {

if (now - _fading.last_us > 9999) {

if ( _fading.inc && _fading.level < _TFT_BRIGHTNESS_MAX) tft.setBrightness(++_fading.level);
else if (!_fading.inc && _fading.level > _TFT_BRIGHTNESS_MIN) tft.setBrightness(--_fading.level);
if ( _fading.inc && _fading.level < _DAC_MAX) _fading.level += 5;
else if (!_fading.inc && _fading.level > _DAC_MIN) _fading.level -= 5;
else _fading.active = false;

switch (_fading.level) {

case _DAC_MIN:
_dac.setVoltage(0, false);
break;

case _DAC_MAX:
_dac.setVoltage(4095, false);
break;

default:
_dac.setVoltage(_fading.level, false);

}

_fading.last_us = now;

}
Expand Down
9 changes: 6 additions & 3 deletions lib/ESPboy/ESPboy.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#pragma once

#include <Adafruit_MCP23X17.h>
#include <Adafruit_MCP4725.h>
#include "Button.h"
#include "NeoPixel.h"
#include "lgfx.h"
Expand All @@ -19,25 +20,27 @@ class ESPboy {

private:

static constexpr uint8_t _TFT_BRIGHTNESS_MIN = 32;
static constexpr uint8_t _TFT_BRIGHTNESS_MAX = 128;
static constexpr uint16_t _DAC_MIN = 650;
static constexpr uint16_t _DAC_MAX = 1000;

struct Fading {

uint32_t last_us;
uint8_t level;
uint16_t level;
bool inc;
bool active;

};

Adafruit_MCP4725 _dac;
Adafruit_MCP23X17 _mcp;

uint32_t _frame_count;
uint32_t _fps;
Fading _fading;

void _initTFT();
void _initMCP4725();
void _initMCP23017();
void _updateFPS();

Expand Down
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ board_build.f_cpu = 160000000L
framework = arduino
monitor_speed = 115200
upload_speed = 1500000
lib_deps = adafruit/Adafruit MCP23017 Arduino Library @ ^2.0.2
lib_deps = adafruit/Adafruit MCP4725 @ ^2.0.0
adafruit/Adafruit MCP23017 Arduino Library @ ^2.0.2
https://github.com/lovyan03/LovyanGFX.git#develop

; You can use these flags to overclock the SPI bus to 40 MHz
Expand Down

0 comments on commit e718f55

Please sign in to comment.