Skip to content

Commit

Permalink
リファクタリング
Browse files Browse the repository at this point in the history
  • Loading branch information
yushin-ito committed Nov 4, 2023
1 parent ab4040f commit 12c1b57
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 65 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
<br>

<h2>📝 Overview</h2>
An M5Stack library for scrolling text.
An M5Stack library for displaying battery charge.

<br>
<br>

<h2>🚀 Features</h2>
<ul>
<li>Scroll from left to right on the lcd</li>
<li>Customize color and speed</li>
<li>Display battery charge</li>
<li>Customize size and rotation</li>
</ul>
<br>
<div align="center">
<img src="https://github.com/yushin-ito/M5Battery/assets/75526539/7a1514ee-3172-422f-889b-651a5821e1dd" width="80%"/>
<img src="https://github.com/yushin-ito/M5Battery/assets/75526539/b1e6bdbb-6da7-4f03-a09a-3ff254dc0549" width="80%"/>
</div>

<br>
Expand Down Expand Up @@ -52,17 +52,18 @@ An M5Stack library for scrolling text.
#include <M5Unified.h>
#include <M5Battery.h>

M5Battery ts;
M5Battery bat;

void setup() {
auto cfg = M5.config();
M5.begin(cfg);
ts.setCusor(0, M5.Display.height() / 2, 1);
ts.setText("Hello World!");
auto cfg = M5.config();
M5.begin(cfg);
bat.setCusor(M5.Display.width() / 2, M5.Display.height() / 2);
bat.setSize(3);
}

void loop() {
ts.showBattery();
bat.showBattery();
bat.updateBattery();
}
```

Expand Down
91 changes: 49 additions & 42 deletions doc/BASIC.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,31 @@
- [Instance Basics](#instance-basics)
- [Create a instance](#create-a-instance)
- [Functions Basic](#functions-basic)
- [ts.Cusor(x, y, scroll_pixel) => void](#tssetcusorx-y-scroll_pixel--void)
- [ts.setTextFont(font) => void](#tssettextfontfont--void)
- [ts.setBgColor(color) => void](#tssetbgcolorcolor--void)
- [ts.setTextColor(color) => void](#tssettextcolorcolor--void)
- [ts.setText(text) => void](#tssettexttext--void)
- [ts.showTextScroll() => void](#tsshowtextscroll--void)
- [ts.getScrollCount() => void](#tsgetscrollcount--uint8_t)
- [bat.setCusor(x, y) => void](#batsetcusorx-y--void)
- [bat.setSize(size) => void](#batsetsizesize--void)
- [bat.setBgColor(color) => void](#batsetbgcolorcolor--void)
- [bat.setBorderColor(color) => void](#batsetbordercolorcolor--void)
- [bat.flip() => void](#batflip--void)
- [bat.showBattery() => void](#batshowbattery--void)
- [bat.deleteBattery() => void](#batdeletebattery--void)
- [bat.updateBattery() => void](#batupdatebattery--void)
- [bat.isLowBattery() => bool](#batislowbattery--bool)

## Instance Basics

### Create a instance

Include the `M5TextScroll.h` and `M5Unified`
Include the `M5Battery.h` and `M5Unified`

```c
#include <M5Unified.h>
#include <M5TextScroll.h>
#include <M5Battery.h>
```

Then Instanciate

```c
M5TextScroll ts;
M5Battery bat;
```

Your instance is now ready.
Expand All @@ -35,59 +37,64 @@ Your instance is now ready.

Call the fuctions using the created instance.

### ts.setCusor(x, y, scroll_pixel) => void
### bat.setCusor(x, y) => void

Set position and speed of text scroll box.
Kind: instance method of `M5TextScroll`
Set position of battery box.
Kind: instance method of `M5Battery`

| Param | Type |
| :----------: | :-----: |
| x | int32_t |
| y | int32_t |
| scroll_pixel | uint8_t |
| Param | Type |
| :---: | :-----: |
| x | int32_t |
| y | int32_t |

### ts.setTextFont(font) => void
### bat.setSize(size) => void

Set font of text.
Kind: instance method of `M5TextScroll`
Set size(1~7) of battery box.
Kind: instance method of `M5Battery`

| Param | Type |
| :---: | :-----------: |
| font | lgfx::IFont\* |
| Param | Type |
| :---: | :----: |
| size | size_t |

### ts.setBgColor(color) => void
### bat.setBgColor(color) => void

Set bg color of text scroll box.
Kind: instance method of `M5TextScroll`
Set bg color of battery box.
Kind: instance method of `M5Battery`

| Param | Type |
| :---: | :------: |
| color | uint32_t |

### ts.setTextColor(color) => void
### bat.setBorderColor(color) => void

Set bg color of text.
Kind: instance method of `M5TextScroll`
Set border color of battery box.
Kind: instance method of `M5Battery`

| Param | Type |
| :---: | :------: |
| color | uint32_t |

### ts.setText(text) => void
### bat.flip() => void

Set scrolling text.
Kind: instance method of `M5TextScroll`
Flip battery box.
Kind: instance method of `M5Battery`

| Param | Type |
| :---: | :----------: |
| text | const chat\* |
### bat.showBattery() => void

### ts.showTextScroll() => void
Show battery.
Kind: instance method of `M5Battery`

Show text scroll.
Kind: instance method of `M5TextScroll`
### bat.deleteBattery() => void

### ts.getScrollCount() => uint8_t
Delete battery.
Kind: instance method of `M5Battery`

Get scrolling count.
Kind: instance method of `M5TextScroll`
### bat.updateBattery() => void

Update battery.
Kind: instance method of `M5Battery`

### bat.isLowBattery() => bool

Get if the battery is low.
Kind: instance method of `M5Battery`
1 change: 0 additions & 1 deletion examples/M5Stack/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ M5Battery bat;
void setup() {
auto cfg = M5.config();
M5.begin(cfg);
M5.Power.begin();
bat.setCusor(M5.Display.width() / 2, M5.Display.height() / 2);
bat.setSize(3);
}
Expand Down
1 change: 0 additions & 1 deletion examples/M5StickC/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ M5Battery bat;
void setup() {
auto cfg = M5.config();
M5.begin(cfg);
M5.Power.begin();
bat.setCusor(M5.Display.width() / 2, M5.Display.height() / 2);
bat.setSize(3);
}
Expand Down
36 changes: 27 additions & 9 deletions src/M5Battery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ M5Battery::M5Battery() {
_x = 0;
_y = 0;
_size = 3;
_flip = false;
_width = 12 * _size;
_height = 5 * _size;
_border_width = 2;
_top_width = 1 * _size;
_bg_color = canvas->color888(0, 0, 0);
_border_color = canvas->color888(255, 255, 255);
Expand Down Expand Up @@ -37,11 +39,13 @@ void M5Battery::setBgColor(uint32_t color) { _bg_color = color; }

void M5Battery::setBorderColor(uint32_t color) { _border_color = color; }

void M5Battery::flip() { _flip = true; }

void M5Battery::showBattery() {
_flag = true;
drawBatteryShape();
canvas->setColorDepth(16);
canvas->createSprite(_width - 4, _height - 4);
canvas->createSprite(_width - _border_width * 2, _height - _border_width * 2);
}

void M5Battery::deleteBattery() {
Expand All @@ -60,23 +64,37 @@ void M5Battery::updateBattery() {

int32_t percent = M5.Power.getBatteryLevel();

canvas->fillRect(0, 0, _width - 4, _height - 4, _bg_color);
canvas->fillRect(0, 0, _width - _border_width * 2, _height - _border_width * 2, _bg_color);

int32_t fill_width = int32_t((_width - _border_width * 2) * (percent / 100.0f));

int32_t fill_width = int32_t((_width - 4) * (percent / 100.0f));
canvas->fillRect(0, 0, fill_width, _height - 4, getBatteryColor());
canvas->fillRect(0, 0, fill_width, _height - _border_width * 2, getBatteryColor());

canvas->pushSprite(_x + 2, _y + 2);
if (!_flip) {
canvas->pushSprite(_x + _border_width + _top_width, _y + _border_width);
}
else {
canvas->pushSprite(_x + _border_width, _y + _border_width);
}
}

void M5Battery::drawBatteryShape() {
canvas->setColorDepth(1);
canvas->createSprite(_width + _top_width, _height);
canvas->fillSprite(0);

canvas->fillRect(0, 0, _width, _height, _border_color);
canvas->fillRect(2, 2, _width - 4, _height - 4, 0);
canvas->fillRect(_width, _top_width, _top_width, _height - (_top_width * 2),
_border_color);
if (!_flip) {
canvas->fillRect(_top_width, 0, _width, _height, _border_color);
canvas->fillRect(_top_width + _border_width, _border_width, _width - _border_width * 2, _height - _border_width * 2, 0);
canvas->fillRect(0, _top_width, _top_width, _height - (_top_width * 2),
_border_color);
}
else {
canvas->fillRect(0, 0, _width, _height, _border_color);
canvas->fillRect(_border_width, _border_width, _width - _border_width * 2, _height - _border_width * 2, 0);
canvas->fillRect(_width, _top_width, _top_width, _height - (_top_width * 2),
_border_color);
}

canvas->setBitmapColor(_border_color, 0);
canvas->pushSprite(_x, _y, 0);
Expand Down
3 changes: 3 additions & 0 deletions src/M5Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ class M5Battery {

M5Canvas* canvas;
bool _flag = true;
bool _flip;
int32_t _x;
int32_t _y;
size_t _size;
int32_t _width;
int32_t _height;
int32_t _border_width;
int32_t _top_width;
uint32_t _bg_color;
uint32_t _border_color;
Expand All @@ -25,6 +27,7 @@ class M5Battery {
void setSize(size_t size);
void setBgColor(uint32_t color);
void setBorderColor(uint32_t color);
void flip();
void showBattery();
void deleteBattery();
void updateBattery();
Expand Down
2 changes: 0 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ M5Battery bat;
void setup() {
auto cfg = M5.config();
M5.begin(cfg);
M5.Power.begin();
bat.setCusor(M5.Display.width() / 2, M5.Display.height() / 2);
bat.setSize(3);
}

void loop() {
M5.update();
bat.showBattery();
bat.updateBattery();
}

0 comments on commit 12c1b57

Please sign in to comment.