Add some example and delete repeat file (#211)

* add some example and delete repeat file
This commit is contained in:
Gitshaoxiang
2020-04-10 15:36:53 +08:00
committed by GitHub
parent acdc9deffa
commit f769d9270f
13 changed files with 916 additions and 193 deletions
+81
View File
@@ -0,0 +1,81 @@
#include <M5Stack.h>
#include <ADXL345.h>
ADXL345 accel(ADXL345_ALT);
//https://github.com/jakalada/Arduino-ADXL345
void setup() {
// put your setup code here, to run once:
M5.begin();
Wire.begin();
M5.Lcd.setCursor(140, 10, 4);
M5.Lcd.println("ACC");
M5.Lcd.setCursor(40, 100); M5.Lcd.print(" x ");
M5.Lcd.setCursor(140, 100); M5.Lcd.print(" y ");
M5.Lcd.setCursor(240, 100); M5.Lcd.print(" z ");
byte deviceID = accel.readDeviceID();
if (deviceID != 0) {
Serial.print("0x");
Serial.print(deviceID, HEX);
Serial.println("");
} else {
Serial.println("read device id: failed");
while(1) {
delay(100);
}
}
// Data Rate
// - ADXL345_RATE_3200HZ: 3200 Hz
// - ADXL345_RATE_1600HZ: 1600 Hz
// - ADXL345_RATE_800HZ: 800 Hz
// - ADXL345_RATE_400HZ: 400 Hz
// - ADXL345_RATE_200HZ: 200 Hz
// - ADXL345_RATE_100HZ: 100 Hz
// - ADXL345_RATE_50HZ: 50 Hz
// - ADXL345_RATE_25HZ: 25 Hz
// - ...
if (!accel.writeRate(ADXL345_RATE_200HZ)) {
Serial.println("write rate: failed");
while(1) {
delay(100);
}
}
// Data Range
// - ADXL345_RANGE_2G: +-2 g
// - ADXL345_RANGE_4G: +-4 g
// - ADXL345_RANGE_8G: +-8 g
// - ADXL345_RANGE_16G: +-16 g
if (!accel.writeRange(ADXL345_RANGE_16G)) {
Serial.println("write range: failed");
while(1) {
delay(100);
}
}
if (!accel.start()) {
Serial.println("start: failed");
while(1) {
delay(100);
}
}
}
void loop() {
// put your main code here, to run repeatedly:
if (accel.update()) {
M5.Lcd.fillRect(0, 130, 360, 30, BLACK);
M5.Lcd.setCursor(35, 130); M5.Lcd.print((int)(1000*accel.getX()));
M5.Lcd.setCursor(135, 130); M5.Lcd.print((int)(1000*accel.getY()));
M5.Lcd.setCursor(235, 130); M5.Lcd.print((int)(1000*accel.getZ()));
//M5.Lcd.setCursor(300, 130); M5.Lcd.print("mg");
} else {
Serial.println("update failed");
while(1) {
delay(100);
}
}
delay(100);
}
+52
View File
@@ -0,0 +1,52 @@
/*
Please install FastLED library first.
In arduino library manage search FastLED
*/
#include <M5Stack.h>
#include "FastLED.h"
#define Neopixel_PIN 21
#define NUM_LEDS 118
CRGB leds[NUM_LEDS];
uint8_t gHue = 0;
static TaskHandle_t FastLEDshowTaskHandle = 0;
static TaskHandle_t userTaskHandle = 0;
void setup() {
M5.begin();
M5.Power.begin();
M5.Lcd.clear(BLACK);
M5.Lcd.setTextColor(YELLOW); M5.Lcd.setTextSize(2); M5.Lcd.setCursor(60, 160);
M5.Lcd.println("CatEar Example");
M5.Lcd.setTextColor(WHITE);
// Neopixel initialization
FastLED.addLeds<WS2811,Neopixel_PIN,GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
FastLED.setBrightness(10);
xTaskCreatePinnedToCore(FastLEDshowTask, "FastLEDshowTask", 2048, NULL, 2, NULL, 1);
}
void loop()
{
}
void FastLEDshowESP32()
{
if (userTaskHandle == 0) {
userTaskHandle = xTaskGetCurrentTaskHandle();
xTaskNotifyGive(FastLEDshowTaskHandle);
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 200 );
ulTaskNotifyTake(pdTRUE, xMaxBlockTime);
userTaskHandle = 0;
}
}
void FastLEDshowTask(void *pvParameters)
{
for(;;) {
fill_rainbow(leds, NUM_LEDS, gHue, 7);// rainbow effect
FastLED.show();// must be executed for neopixel becoming effective
EVERY_N_MILLISECONDS( 20 ) { gHue++; }
}
}
+26
View File
@@ -0,0 +1,26 @@
#include <M5Stack.h>
const int motor_pin = 21;
int freq = 10000;
int ledChannel = 0;
int resolution = 10;
void setup() {
// put your setup code here, to run once:
M5.begin();
M5.Lcd.setCursor(120, 110, 4);
M5.Lcd.println("MOTOR");
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(motor_pin, ledChannel);
}
// 0 - 1024
void loop() {
// put your main code here, to run repeatedly:
ledcWrite(ledChannel, 512);//0°
delay(1000);
ledcWrite(ledChannel, 0);//90°
delay(1000);
//ledcWrite(ledChannel, 30);//180°
//delay(1000);
}
+59
View File
@@ -0,0 +1,59 @@
#include <M5Stack.h>
#include <M5StackUpdater.h>
#define WIDTH 320
#define HEIGHT 240
#define BLOCK_SIZE 40
#define UNIT_WIDTH 5
#define UNIT_HEIGHT 5
#define UNIT_SIZE 25
#define GETX(i) ((i) % (5))
#define GETY(i) ((i) / (5))
int world[UNIT_SIZE];
int i;
void setup() {
M5.begin();
Wire.begin();
if(digitalRead(BUTTON_A_PIN) == 0){
Serial.println("Will load menu binary");
updateFromFS(SD);
ESP.restart();
}
Serial2.begin(115200, SERIAL_8N1, 16, 17);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(35, 220);
M5.Lcd.println(" < * >");
for (i = 0; i < UNIT_SIZE; i++) {
world[i] = 0;
}
i = UNIT_SIZE / 2;
}
void loop() {
M5.update();
int x = GETX(i) + 1;
int y = GETY(i);
if (world[i] > 0) M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2, LIGHTGREY);
else M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2, BLUE);
if (M5.BtnC.wasPressed()) {
if (world[i] > 0) M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2, WHITE);
else M5.Lcd.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, BLACK);
++i;
if (i >= UNIT_SIZE) i=0;
}
if (M5.BtnA.wasPressed()) {
if (world[i] > 0) M5.Lcd.fillRect(x * BLOCK_SIZE + 1, y * BLOCK_SIZE + 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2, WHITE);
else M5.Lcd.fillRect(x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE, BLACK);
--i;
if (i < 0 ) i=UNIT_SIZE -1;
}
if (M5.BtnB.wasPressed()) {
if (world[i] > 0) world[i]=0;
else world[i]=1;
Serial2.print(world[i]);
Serial2.print(GETX(i));
Serial2.println(GETY(i));
}
}
+20
View File
@@ -0,0 +1,20 @@
#include <M5Stack.h>
void setup() {
// put your setup code here, to run once:
M5.begin();
//Wire.begin();
M5.Lcd.setCursor(120, 10, 4);
M5.Lcd.println("90/180 OPTICAL");
pinMode(36,INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
M5.Lcd.setCursor(80, 120, 4);
M5.Lcd.printf("ir receive: %d",digitalRead(36));
}
-91
View File
@@ -1,91 +0,0 @@
#include <hidboot.h>
int mou_px, mou_py, mou_button;
class MouseRptParser:public MouseReportParser{
protected:
void OnMouseMove (MOUSEINFO *mi);
void OnLeftButtonUp (MOUSEINFO *mi);
void OnLeftButtonDown (MOUSEINFO *mi);
void OnRightButtonUp (MOUSEINFO *mi);
void OnRightButtonDown (MOUSEINFO *mi);
void OnMiddleButtonUp (MOUSEINFO *mi);
void OnMiddleButtonDown (MOUSEINFO*mi);
};
void SendToBT(MOUSEINFO *mi)
{
byte Button=0;
if (mi->bmLeftButton)
Button |= BIT0;
else
Button & !BIT0;
if (mi->bmRightButton)
Button |= BIT1;
else
Button & !BIT1;
if (mi->bmMiddleButton)
Button |= BIT2;
else
Button & !BIT2;
mou_px = mi->dX;
mou_py = mi->dY;
mou_button = Button;
// /*
Serial.println("L Mouse Move");
Serial.print("dx=");
Serial.print(mi->dX, DEC);
Serial.print(" dy=");
Serial.println(mi->dY, DEC);
Serial.println(Button,DEC);
// */
/*
Serial.write(0x08); //BYTE1
Serial.write(0x00); //BYTE2
Serial.write(0xA1); //BYTE3
Serial.write(0x02); //BYTE4
Serial.write(Button); //BYTE5
Serial.write(mi->dX); //BYTE6
Serial.write(mi->dY); //BYTE7
Serial.write(0); //BYTE8
*/
}
void MouseRptParser::OnMouseMove(MOUSEINFO*mi)
{
SendToBT(mi);
};
void MouseRptParser::OnLeftButtonUp (MOUSEINFO *mi)
{
//Serial.println("L Butt Up");
SendToBT(mi);
};
void MouseRptParser::OnLeftButtonDown (MOUSEINFO *mi)
{
//Serial.println("L Butt Dn");
SendToBT(mi);
};
void MouseRptParser::OnRightButtonUp (MOUSEINFO *mi)
{
//Serial.println("R Butt Up");
SendToBT(mi);
};
void MouseRptParser::OnRightButtonDown (MOUSEINFO *mi)
{
//Serial.println("R Butt Dn");
SendToBT(mi);
};
void MouseRptParser::OnMiddleButtonUp (MOUSEINFO *mi)
{
//Serial.println("M Butt Up");
SendToBT(mi);
};
void MouseRptParser::OnMiddleButtonDown (MOUSEINFO *mi)
{
//Serial.println("M Butt Dn");
SendToBT(mi);
};
-102
View File
@@ -1,102 +0,0 @@
/*
This sample code demonstrates the normal use of a USB_Host_SHield_Library_2.0 object.
USB_Host_SHield_Library_2.0 file in M5stack lib examples -> Unit -> USB -> USB_Host_SHield_Library_2.0
Please uncompress it to xx\xx\Arduino\libraries\.
*/
#include <M5Stack.h>
#include <SPI.h>
#include <Usb.h>
#include <hiduniversal.h>
#include <hidboot.h>
#include <usbhub.h>
#include "M5Mouse.h"
USB Usb;
//HIDUniversal HidMouse(&Usb);
USBHub Hub(&Usb);
HIDBoot<USB_HID_PROTOCOL_MOUSE> HidMouse(&Usb);
MouseRptParser Prs;
int StaPotX = 160, StaPotY = 120;
void Mouse_Pointer(int PotDataX,int PotDataY)
{
static int OldDataX, OldDataY;
if((StaPotX + PotDataX) <= 320 && (StaPotX + PotDataX) > 0 )
StaPotX = (StaPotX + PotDataX);
else if((StaPotX + PotDataX) <= 0)
StaPotX = 0;
else
StaPotX = 319;
if((StaPotY + PotDataY) <= 240 && (StaPotY + PotDataY) > 0 )
StaPotY = (StaPotY + PotDataY);
else if((StaPotY + PotDataY) <= 0)
StaPotY = 0;
else
StaPotY = 239;
// clear draw
if(OldDataX != StaPotX || OldDataY != StaPotY)
{
M5.Lcd.drawLine(OldDataX + 0, OldDataY + 0, OldDataX + 0, OldDataY + 10, BLACK);
M5.Lcd.drawLine(OldDataX + 0, OldDataY + 0, OldDataX + 7, OldDataY + 7, BLACK);
M5.Lcd.drawLine(OldDataX + 4, OldDataY + 7, OldDataX + 7, OldDataY + 7, BLACK);
M5.Lcd.drawLine(OldDataX + 4, OldDataY + 7, OldDataX + 0, OldDataY + 10, BLACK);
M5.Lcd.drawLine(OldDataX + 3, OldDataY + 7, OldDataX + 6, OldDataY + 12, BLACK);
}
// draw
M5.Lcd.drawLine(StaPotX + 0, StaPotY + 0, StaPotX + 0, StaPotY + 10, WHITE);
M5.Lcd.drawLine(StaPotX + 0, StaPotY + 0, StaPotX + 7, StaPotY + 7, WHITE);
M5.Lcd.drawLine(StaPotX + 4, StaPotY + 7, StaPotX + 7, StaPotY + 7, WHITE);
M5.Lcd.drawLine(StaPotX + 4, StaPotY + 7, StaPotX + 0, StaPotY + 10, WHITE);
M5.Lcd.drawLine(StaPotX + 3, StaPotY + 7, StaPotX + 6, StaPotY + 12, WHITE);
OldDataX = StaPotX;
OldDataY = StaPotY;
}
void setup()
{
M5.begin();
M5.Power.begin();
Serial.println("M5USB_Demo Start...");
if (Usb.Init() == -1)
Serial.println("USB Host Init Error");
// HidMouse.SetProtocol(0, HID_RPT_PROTOCOL);
HidMouse.SetReportParser(0,(HIDReportParser*)&Prs);
delay( 200 );
}
void loop()
{
Usb.Task();
if ( Usb.getUsbTaskState() == USB_STATE_RUNNING)
{
// Serial.println("ok");
Mouse_Pointer(mou_px, mou_py);
mou_px = 0;
mou_py = 0;
if(mou_button == 1)
M5.Lcd.drawCircle(StaPotX, StaPotY, 1, WHITE);
if(mou_button == 2)
M5.Lcd.drawCircle(StaPotX, StaPotY, 1, GREEN);
if(mou_button == 4)
M5.Lcd.fillScreen(BLACK);
}
}
Binary file not shown.
+26
View File
@@ -0,0 +1,26 @@
#include <M5Stack.h>
const int motor_pin = 21;
int freq = 10000;
int ledChannel = 0;
int resolution = 10;
void setup() {
// put your setup code here, to run once:
M5.begin();
M5.Lcd.setCursor(120, 110, 4);
M5.Lcd.println("MOTOR");
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(motor_pin, ledChannel);
}
// 0 - 1024
void loop() {
// put your main code here, to run repeatedly:
ledcWrite(ledChannel, 512);//0°
delay(1000);
ledcWrite(ledChannel, 0);//90°
delay(1000);
//ledcWrite(ledChannel, 30);//180°
//delay(1000);
}