Skip to content

Commit

Permalink
Merge pull request #138 from Tinyu-Zhao/master
Browse files Browse the repository at this point in the history
add some Ex&explain
  • Loading branch information
Tinyu-Zhao authored Aug 20, 2021
2 parents b4f281b + de98e69 commit 2915857
Show file tree
Hide file tree
Showing 76 changed files with 6,213 additions and 1,979 deletions.
73 changes: 34 additions & 39 deletions examples/Advanced/Display/HZK16/HZK16.ino
Original file line number Diff line number Diff line change
@@ -1,50 +1,45 @@
/*
在 src.h 文件中 GbkStr 显示为乱码为正常现象
HZK16为GB2312中文编码格式字库, 需要显示中文建议使用notepad++或其它软件(Arduino IDE 为UTF-8)通过编码GB2312打开str.h
即可修改为个人想要输出中文字符
*/
/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
* Equipped with M5Stick-C sample source code
* 配套 M5Stick-C 示例源代码
* Visit the website for more information:https://docs.m5stack.com/en/core/m5stickc
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/m5stickc
*
* describe:Character library. 字库
* date:2021/7/28
*******************************************************************************
-----PLEASE SEE THE README----
------请在使用前看README文件----*/
#include <M5StickC.h>
#include "str.h"

void setup() {

M5.begin();

M5.Lcd.loadHzk16();

M5.Lcd.setTextColor(WHITE, BLACK);

M5.begin(); //Init M5StickC. 初始化M5StickC
M5.Lcd.setRotation(3); //Rotate the screen. 旋转屏幕
M5.Lcd.loadHzk16(); //Load the Chinese character library (be sure to load before using the Chinese character library). 加载汉字库(务必在使用汉字库前加载)
M5.Lcd.setTextColor(WHITE, BLACK);//Set the text color to white and the text background color to black (mandatory). 设置文字颜色为白色,文字背景颜色为黑色(必加)
// Set text with red highlight color
M5.Lcd.setHighlightColor(RED);

// Set text size to 1
M5.Lcd.setTextSize(1);
M5.Lcd.setCursor(0,0,1);
M5.Lcd.writeHzk(AscStr);
M5.Lcd.setHighlightColor(RED);//Set the text highlight color to red. 设置文字高亮颜色为红色
M5.Lcd.setTextSize(1);//Set text size to 1. 设置字号大小为1 Set text size to 1(必加)
}

void loop() {
M5.Lcd.setCursor(0,0,1); //Set the cursor at (0,0) and the size to 1(mandatory). 将光标设置在(0,0)处,并设置字号为1(必加)
M5.Lcd.writeHzk(AscStr); //Display the contents of AscStr here (which can be changed in str.h). 在此处显示AscStr中的内容(可在str.h中更改)
// Print some ASCII text on the screen
delay(1000);
// Highlight the text
M5.Lcd.highlight(true);
M5.Lcd.setCursor(0,32);
M5.Lcd.writeHzk(AscStr);
delay(1000);
// Clear screen
//M5.Lcd.clear();
// Turn highlight off
M5.Lcd.highlight(false);
M5.Lcd.setCursor(0,70);
// print some GBK text on the screen
M5.Lcd.writeHzk(GbkStr);

M5.Lcd.setCursor(0,40);
M5.Lcd.writeHzk(GbkStr); //Display the contents of GbkStr here (which can be changed in str.h). 在此处显示GbkStr中的内容(可在str.h中更改)
delay(1000);
M5.Lcd.highlight(true);
M5.Lcd.setCursor(0,90);
//Highlight the text. 高亮显示文本
M5.Lcd.highlight(true); //Turn on highlight. 开启高亮显示
M5.Lcd.setCursor(0,60);
M5.Lcd.writeHzk(GbkStr);


}

void loop() {
// put your main code here, to run repeatedly:

delay(1000);
M5.Lcd.fillScreen(BLACK); //Fill the screen with black color, equivalent to empty the screen. 填充屏幕颜色为黑色,等效于清空屏幕
M5.Lcd.highlight(false); //Turn off highlight. 关闭高亮显示
delay(500);
}
7 changes: 7 additions & 0 deletions examples/Advanced/Display/HZK16/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
HZK16 is the GB2312 Chinese encoding format font, so it is normal that GbkStr is displayed as garbled in the src.h file
To display Chinese normally, notepad++ or other software (UTF-8 for Arduino IDE) should be used to open str.h with encoding GB2312
Modify the content in GbkStr to replace it with the character you want to display

HZK16为GB2312中文编码格式字库, 故在 src.h 文件中 GbkStr 显示为乱码为正常现象
若要正常显示中文应使用notepad++或其它软件(Arduino IDE 为UTF-8)使用编码GB2312打开str.h
修改GbkStr里的内容即可更换为想要显示的字符
2 changes: 1 addition & 1 deletion examples/Advanced/Display/HZK16/str.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
#define _STR_H_

char* AscStr="ASCII: \nABCDEFG1234567";
char* GbkStr="²É¾Õ¶«ÀéÏÂ";
char* GbkStr="²É¾Õ¶«ÀéÏ ¤³¤ó¤Ë¤Á¤Ï";

#endif
43 changes: 43 additions & 0 deletions examples/Advanced/Display/TFT_Ellipse/TFT_Ellipse.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
* Equipped with M5Stick-C sample source code
* 配套 M5Stick-C 示例源代码
* Visit the website for more information:https://docs.m5stack.com/en/core/m5stickc
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/m5stickc
*
* describe:Ellipse drawing. 椭圆绘制
* date:2021/7/27
*******************************************************************************/

#include <M5StickC.h>

void setup(void) {
M5.begin(); //Init M5Stick-C. 初始化M5Stick-C
}

void loop() {
// Draw some random ellipses. 绘制一些随机形状的椭圆
for (int i = 0; i < 40; i++)
{
int rx = random(40);
int ry = random(40);
int x = rx + random(80 - rx - rx);
int y = ry + random(160 - ry - ry);
M5.Lcd.fillEllipse(x, y, rx, ry, random(0xFFFF)); //At (x, y), draw a filled ellipse with random width and height of rx, ry with random color random (0xFFFF). 在(x,y)处以随机颜色random(0xFFFF),绘制一个随机宽高为rx,ry的填充椭圆
}

delay(2000);
M5.Lcd.fillScreen(BLACK); //清空屏幕

for (int i = 0; i < 40; i++)
{
int rx = random(40);
int ry = random(40);
int x = rx + random(80 - rx - rx);
int y = ry + random(160 - ry - ry);
M5.Lcd.drawEllipse(x, y, rx, ry, random(0xFFFF));//At (x, y), draw an elliptical wire frame with a random width and height of rx, ry in a random color (0xFFFF). 在(x,y)处以随机颜色random(0xFFFF),绘制一个随机宽高为rx,ry的椭圆线框
}

delay(2000);
}
85 changes: 85 additions & 0 deletions examples/Advanced/HallSensor/HallSensor.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
* Equipped with M5Stick-C sample source code
* 配套 M5Stick-C 示例源代码
* Visit the website for more information:https://docs.m5stack.com/en/core/m5stickc
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/m5stickc
*
* describe:Hall sensor. 霍尔传感器
* date:2021/7/27
*******************************************************************************
The ESP32 MCU has build in Hall sensor. ESP32 MCU内置霍尔传感器
It is not very suscescible but if you bring a magnet close to the
upper screen you will see the influence on the measurement.
In this example we use some low pass filters to get rid of the noise.
*/
#include <M5StickC.h>

#define HORIZONTAL_RESOLUTION 80 //屏幕水平分辨率
#define VERTICAL_RESOLUTION 160 //屏幕竖直分辨率
#define POSITION_OFFSET_Y 20

uint16_t oldSignal[HORIZONTAL_RESOLUTION];

float ESP32_hallRead() //ESP32 hall value read. ESP32霍尔值读取
{
float value = 0;
int count = 400;
// mean value filter. 数据低通滤波器
for (int n = 0; n < count; n++) value += hallRead();
return value / count * 10;
}

float HallOffset = 0; ////Store the initial value of magnetic force. 存储磁力的初值

void setup()
{
M5.begin(); //Init M5Stick-C. 初始化M5Stick-C
M5.Lcd.printf("\nESP32 Hall sensor:"); //Screen printout. 屏幕打印输出
HallOffset = ESP32_hallRead(); // callibrate the output value to the magnetic field at start up. 在启动时将输出值校准到磁场
}

float LowPassFilteredValue=0;
void showSignal()
{
int n;

int oldx;
int oldy;
int oldSig;
int x, y;

for (n = 0; n < HORIZONTAL_RESOLUTION; n++) //在水平分辨率内,每个像素点根据计算得到的磁力大小绘制
{ //Within the horizontal resolution, each pixel is drawn according to the calculated magnetic force
x = n;
float value = ESP32_hallRead()- HallOffset; //Reduce the influence of own magnetism. 减少自身磁力的影响
LowPassFilteredValue+=(value-LowPassFilteredValue)*0.05;
M5.Lcd.setCursor(30, 17);
M5.Lcd.print((int)LowPassFilteredValue);
M5.Lcd.print(" ");
M5.Lcd.setCursor(0, 40);
y = map(value , -127, 127, VERTICAL_RESOLUTION, POSITION_OFFSET_Y); //将value映射至-30~30的范围内,以免绘制超出屏幕

if (n > 0)
{
// delete old line element. 删除上次画的线
M5.Lcd.drawLine(oldx , oldSig, x, oldSignal[n], BLACK );

// draw new line element. 画新的线
if (n < HORIZONTAL_RESOLUTION - 1) // don't draw last element because it would generate artifacts. 不能绘制最后一个像素
{
M5.Lcd.drawLine(oldx,oldy,x,y, GREEN);
}
}
oldx = x;
oldy = y;
oldSig = oldSignal[n];
oldSignal[n] = y;
}
}

void loop(void)
{
showSignal();
}
73 changes: 36 additions & 37 deletions examples/Advanced/I2C_Tester/I2C_Tester.ino
Original file line number Diff line number Diff line change
@@ -1,53 +1,51 @@
/************************************************************************
M5StackFire I2C Scanner
The M5StackFire has a connector for I2C devices.
This program scans the addresses 1-127 continuosly and shows
the devices found on the TFT.
The M5Stack fire has two internal I2C devices at address 0x68 and 0x75.
If they do not appear on the TFT it could mean you made a short cut on
the I2C bus.
October 2018, ChrisMicro
************************************************************************/
/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
* Equipped with M5Stick-C sample source code
* 配套 M5Stick-C 示例源代码
* Visit the website for more information:https://docs.m5stack.com/en/core/m5stickc
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/m5stickc
*
* describe:I2C Scanner. I2C探测
* date:2021/7/27
*******************************************************************************
*/
/*
This program scans the addresses 1-127 continuosly and shows the devices found on the TFT.
该程序连续扫描地址 1-127 并显示在外部(内部)I2C发现的设备。
*/
#include <M5StickC.h>

void setup()
{
M5.Lcd.begin();
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen( BLACK );
M5.Lcd.setCursor(0, 0);
M5.Lcd.setTextColor(YELLOW);
M5.Lcd.setTextSize(1);

M5.Lcd.fillScreen( BLACK );
M5.Lcd.setCursor(0, 0);
M5.Lcd.println("M5StickC I2C Tester");

Wire.begin();
M5.begin(); //Init M5Stick-C(Init of external I2C is also included ). 初始化M5Stick-C(初始化外部I2C也包含在内)
Wire.begin(); //Init wire and join the I2C network. 初始化wire,并且加入到I2C网络
//Wire.begin(21, 22); //Detect internal I2C, if this sentence is not added, it will detect external I2C. 检测内部I2C,若不加此句为检测外部I2C
M5.Lcd.setTextColor(YELLOW); //Set the font color to yellow. 设置字体颜色为黄色
M5.Lcd.getTextDatum(3); //Rotate the screen. 旋转屏幕
M5.Lcd.println("M5StickC I2C Tester"); //Print a string on the screen. 在屏幕上打印字符串

delay(3000);
M5.Lcd.fillScreen( BLACK );
M5.Lcd.fillScreen( BLACK );//Make the screen full of black (equivalent to clear() to clear the screen). 使屏幕充满黑色(等效clear()清屏)
}

int textColor=YELLOW;

void loop()
void loop()
{
int address;
int error;
M5.Lcd.setCursor(0, 0);
M5.Lcd.println("scanning Address [HEX]");

for(address = 1; address < 127; address++ )
for(address = 1; address < 127; address++ )
{
Wire.beginTransmission(address);
error = Wire.endTransmission();
Wire.beginTransmission(address); //Data transmission to the specified device address starts. 开始向指定的设备地址进行传输数据
error = Wire.endTransmission();/*Stop data transmission with the slave. 停止与从机的数据传输
0: success. 成功
1: The amount of data exceeds the transmission buffer capacity limit. 数据量超过传送缓存容纳限制
return value: 2: Received NACK when sending address. 传送地址时收到 NACK
3: Received NACK when transmitting data. 传送数据时收到 NACK
4: Other errors. 其它错误 */
if(error==0)
{
M5.Lcd.print(address,HEX);M5.Lcd.print(" ");
Expand All @@ -57,7 +55,8 @@ void loop()
delay(10);
}

if(textColor==YELLOW) textColor=GREEN;
else textColor=YELLOW;
M5.Lcd.setTextColor(textColor,BLACK);
if(textColor==YELLOW) {
textColor=CYAN;
}else textColor=YELLOW;
M5.Lcd.setTextColor(textColor,BLACK); //Set the foreground color of the text to textColor and the background color to BLACK. 设置文字的前景色为textColor背景色为BLACK
}
31 changes: 31 additions & 0 deletions examples/Advanced/MultSerial/MultSerial.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
*******************************************************************************
* Copyright (c) 2021 by M5Stack
* Equipped with M5Stick-C sample source code
* 配套 M5Stick-C 示例源代码
* Visit the website for more information:https://docs.m5stack.com/en/core/m5stickc
* 获取更多资料请访问:https://docs.m5stack.com/zh_CN/core/m5stickc
*
* describe:MultSerial. 多串口
* date:2021/8/5
******************************************************************************
*/
#include <M5StickC.h>

void setup() {
M5.begin(); //Init M5StickC. 初始化 M5StickC
// Serial2.begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t txPin, bool invert)
Serial2.begin(115200, SERIAL_8N1, 16, 17); //Init serial port 2. 初始化串口2
}

void loop() {
if(Serial.available()) { //If the serial port reads data. 如果串口读到数据
int ch = Serial.read(); // Copy the data read from the serial port to the CH. 把串口读取到的数据复制给ch
Serial2.write(ch); //Serial port 2 Outputs the CH content. 串口2输出ch的内容
}

if(Serial2.available()) {
int ch = Serial2.read();
Serial.write(ch);
}
}
Loading

0 comments on commit 2915857

Please sign in to comment.