Skip to content

Commit 3d3d9da

Browse files
authored
Merge pull request #1 from Forairaaaaa/main
Add code.
2 parents f87df1b + ab5d13b commit 3d3d9da

30 files changed

Lines changed: 6184 additions & 0 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.pio
2+
.vscode

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
# M5StickCPlus2-UserDemo
22
M5StickCPlus2 user demo for hardware evaluation.
3+
4+
## Tool Chains
5+
6+
[PlatformIO](https://platformio.org/)
7+
8+
## Build
9+
10+
- Upload firmware
11+
- Upload filesystem image

include/README

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
This directory is intended for project header files.
3+
4+
A header file is a file containing C declarations and macro definitions
5+
to be shared between several project source files. You request the use of a
6+
header file in your project source file (C, C++, etc) located in `src` folder
7+
by including it, with the C preprocessing directive `#include'.
8+
9+
```src/main.c
10+
11+
#include "header.h"
12+
13+
int main (void)
14+
{
15+
...
16+
}
17+
```
18+
19+
Including a header file produces the same results as copying the header file
20+
into each source file that needs it. Such copying would be time-consuming
21+
and error-prone. With a header file, the related declarations appear
22+
in only one place. If they need to be changed, they can be changed in one
23+
place, and programs that include the header file will automatically use the
24+
new version when next recompiled. The header file eliminates the labor of
25+
finding and changing all the copies as well as the risk that a failure to
26+
find one copy will result in inconsistencies within a program.
27+
28+
In C, the usual convention is to give header files names that end with `.h'.
29+
It is most portable to use only letters, digits, dashes, and underscores in
30+
header file names, and at most one dot.
31+
32+
Read more about using header files in official GCC documentation:
33+
34+
* Include Syntax
35+
* Include Operation
36+
* Once-Only Headers
37+
* Computed Includes
38+
39+
https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html

lib/Button/Button.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
Button - a small library for Arduino to handle button debouncing
3+
4+
MIT licensed.
5+
*/
6+
7+
#include "Button.h"
8+
#include <Arduino.h>
9+
10+
Button::Button(uint8_t pin, uint16_t debounce_ms)
11+
: _pin(pin)
12+
, _delay(debounce_ms)
13+
, _state(HIGH)
14+
, _ignore_until(0)
15+
, _has_changed(false)
16+
{
17+
}
18+
19+
void Button::begin()
20+
{
21+
pinMode(_pin, INPUT_PULLUP);
22+
}
23+
24+
//
25+
// public methods
26+
//
27+
28+
bool Button::read()
29+
{
30+
// ignore pin changes until after this delay time
31+
if (_ignore_until > millis())
32+
{
33+
// ignore any changes during this period
34+
}
35+
36+
// pin has changed
37+
else if (digitalRead(_pin) != _state)
38+
{
39+
_ignore_until = millis() + _delay;
40+
_state = !_state;
41+
_has_changed = true;
42+
}
43+
44+
return _state;
45+
}
46+
47+
// has the button been toggled from on -> off, or vice versa
48+
bool Button::toggled()
49+
{
50+
read();
51+
return has_changed();
52+
}
53+
54+
// mostly internal, tells you if a button has changed after calling the read() function
55+
bool Button::has_changed()
56+
{
57+
if (_has_changed)
58+
{
59+
_has_changed = false;
60+
return true;
61+
}
62+
return false;
63+
}
64+
65+
// has the button gone from off -> on
66+
bool Button::pressed()
67+
{
68+
return (read() == PRESSED && has_changed());
69+
}
70+
71+
// has the button gone from on -> off
72+
bool Button::released()
73+
{
74+
return (read() == RELEASED && has_changed());
75+
}

lib/Button/Button.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
Button - a small library for Arduino to handle button debouncing
3+
4+
MIT licensed.
5+
*/
6+
7+
#ifndef Button_h
8+
#define Button_h
9+
#include "Arduino.h"
10+
11+
class Button
12+
{
13+
public:
14+
Button(uint8_t pin, uint16_t debounce_ms = 100);
15+
void begin();
16+
bool read();
17+
bool toggled();
18+
bool pressed();
19+
bool released();
20+
bool has_changed();
21+
22+
const static bool PRESSED = LOW;
23+
const static bool RELEASED = HIGH;
24+
25+
private:
26+
uint8_t _pin;
27+
uint16_t _delay;
28+
bool _state;
29+
uint32_t _ignore_until;
30+
bool _has_changed;
31+
};
32+
33+
#endif

lib/Button/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Michael D K Adams
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

lib/Button/README.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
Button
2+
======
3+
4+
* Author: Michael Adams (<http://www.michael.net.nz>)
5+
* Copyright (C) 2016 Michael D K Adams.
6+
* Released under the MIT license.
7+
8+
Button is a tiny library to make reading buttons very simple. It handles debouncing automatically, and monitoring of state.
9+
10+
Motivation
11+
----------
12+
Ahh buttons. Ahh debouncing! Sometimes you really wish you could ignore all the mechanics of debouncing, reading inputs, monitoring state, and just say... `if (button.pressed())` and know that it'll only run once each time you press a button.
13+
14+
Or how about `if (button.released())`, or `button.toggled()`. It is such a simple concept, but in practice you need to set up timers, monitor input puts, set pullups, etc etc. On anything more than the most simple example, that can become quite a headache.
15+
16+
So fed up with all that I figured there had to be a better way. This library is the result.
17+
18+
Features
19+
--------
20+
* Super simple API.
21+
* Handles debouncing.
22+
* Sets the pin mode automatically.
23+
* Lets you write code that triggers:
24+
** based on the pin state (high or low)
25+
** when a button is pressed
26+
** when a button is released
27+
** or when a button changes (i.e. pressing or releasing)
28+
29+
Requirements
30+
------------
31+
* An Arduino — http://arduino.cc/
32+
* A button
33+
34+
Installation
35+
------------
36+
Download the ZIP archive (https://github.com/madleech/Button/zipball/master), then open the Arduino IDE and choose Sketch > Include Library > Add .ZIP Library... and select your downloaded file.
37+
38+
You should now see in File > Examples > Button entires for the basic\_usage example.
39+
40+
Code Examples
41+
-------------
42+
Here is the 'basic\_usage' example program, included in the download:
43+
44+
```C++
45+
#include <Button.h>
46+
47+
Button button1(2); // Connect your button between pin 2 and GND
48+
Button button2(3); // Connect your button between pin 3 and GND
49+
Button button3(4); // Connect your button between pin 4 and GND
50+
51+
void setup() {
52+
button1.begin();
53+
button2.begin();
54+
button3.begin();
55+
56+
while (!Serial) { }; // for Leos
57+
Serial.begin(9600);
58+
}
59+
60+
void loop() {
61+
if (button1.pressed())
62+
Serial.println("Button 1 pressed");
63+
64+
if (button2.released())
65+
Serial.println("Button 2 released");
66+
67+
if (button3.toggled()) {
68+
if (button3.read() == Button::PRESSED)
69+
Serial.println("Button 3 has been pressed");
70+
else
71+
Serial.println("Button 3 has been released");
72+
}
73+
}
74+
```
75+
76+
Documentation
77+
-------------
78+
**Button(int pin)**
79+
Creates a new Button.
80+
81+
**void begin()**
82+
Call this in your `setup` method to setup the button. All it does is set the correct pin mode.
83+
84+
**bool pressed()**
85+
Returns true when _and only when_ the button is pressed. Until the button is released (in the debounced-sense of the word) this function won't return true again. So in effect, it returns true only while you are pressing the button, or to put it another way, it fires on a rising edge.
86+
87+
**bool released()**
88+
Like `pressed()`, but round the other way. So if you hold down a button, and then release it... that is when it fires.
89+
90+
**bool toggled()**
91+
Returns true whenever the button is pressed or released, i.e., its position is toggled. To find out what the position actually is, you can use the `read()` function.
92+
93+
**bool read()**
94+
Returns the current debounced state of the button, i.e. Button::PRESSED or Button::RELEASED.
95+
96+
**bool has_changed()**
97+
Returns whether the position/state of the button has changed after calling the previous read() function. Unlikely to be used except by Super Gurus.
98+
99+
Quirks and Things to Keep in Mind
100+
---------------------------------
101+
**Highs and lows, lows and highs**
102+
The easiest way to connect a switch on an Arduino is to connect it between an input pin and ground, and use the internal pullup resistor to make sure it doesn't float. This is fine and dandy, but it can get a bit confusing, as a "pressed" button is logic level: low, while a "released" button is logic level: high.
103+
104+
So to make it a bit more obvious what you're talking about, you can use a couple of handy shortcuts: `Button::PRESSED` and `Button::RELEASED` which map to the expected values.
105+
106+
License
107+
-------
108+
Copyright (c) 2016 Michael D K Adams. http://www.michael.net.nz/
109+
110+
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:
111+
112+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
113+
114+
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.
115+

lib/Button/library.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Button
2+
version=1.0.0
3+
author=Michael Adams <arduino@michaeladams.org>
4+
maintainer=Michael Adams <arduino@michaeladams.org>
5+
sentence=Button is a tiny library to make reading buttons very simple.
6+
paragraph=It handles debouncing automatically, and monitoring of state.
7+
category=Signal Input/Output
8+
url=http://utrainia.com/
9+
architectures=*

lib/README

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
This directory is intended for project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link into executable file.
4+
5+
The source code of each library should be placed in a an own separate directory
6+
("lib/your_library_name/[here are source files]").
7+
8+
For example, see a structure of the following two libraries `Foo` and `Bar`:
9+
10+
|--lib
11+
| |
12+
| |--Bar
13+
| | |--docs
14+
| | |--examples
15+
| | |--src
16+
| | |- Bar.c
17+
| | |- Bar.h
18+
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
19+
| |
20+
| |--Foo
21+
| | |- Foo.c
22+
| | |- Foo.h
23+
| |
24+
| |- README --> THIS FILE
25+
|
26+
|- platformio.ini
27+
|--src
28+
|- main.c
29+
30+
and a contents of `src/main.c`:
31+
```
32+
#include <Foo.h>
33+
#include <Bar.h>
34+
35+
int main (void)
36+
{
37+
...
38+
}
39+
40+
```
41+
42+
PlatformIO Library Dependency Finder will find automatically dependent
43+
libraries scanning project source files.
44+
45+
More information about PlatformIO Library Dependency Finder
46+
- https://docs.platformio.org/page/librarymanager/ldf.html

0 commit comments

Comments
 (0)