Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitshaoxiang committed Mar 26, 2024
0 parents commit 85da12f
Show file tree
Hide file tree
Showing 10 changed files with 448 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 M5Stack

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:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

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.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# M5Hat-BugC

## Overview

M5Stack Hat BugC and BugC2 examples.

## License

- [M5Hat-BugC - MIT](LICENSE)
54 changes: 54 additions & 0 deletions examples/driver/driver.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @file driver.ino
* @author SeanKwok ([email protected])
* @brief
* @version 0.1
* @date 2024-03-26
*
*
* @Hardwares: M5StickC + Hat BugC2
* @Platform Version: Arduino M5Stack Board Manager v2.1.1
* @Dependent Library:
* M5HatBugC: https://github.com/m5stack/M5Hat-BugC
*/

#include "M5HatBugC.h"

M5HatBugC bugc;

void setup() {
Serial.begin(115200);
while (!bugc.begin(&Wire, BUGC_DEFAULT_I2C_ADDR, 0, 26, 400000U)) {
Serial.println("Couldn't find BugC");
delay(1000);
}
bugc.setAllMotorSpeed(0, 0, 0, 0);
}

void loop() {
// Read the battery voltage (only BugC2 support)
uint16_t bat_voltage = bugc.getRawAdc12Bit();
Serial.printf("Battery: %.2fV\r\n",
((float)bat_voltage / 4095.0 * 3.3) * 2.960784);

bugc.setAllLedColor(0xff0000, 0x0000ff);
delay(500);
bugc.setAllLedColor(0x0000ff, 0xff0000);
delay(500);
bugc.setAllMotorSpeed(50, 50, 50, 50);
delay(100);
bugc.setAllMotorSpeed(-50, -50, -50, -50);
delay(100);
bugc.setAllMotorSpeed(0, 0, 0, 0);
delay(100);
bugc.move(MOVE_LEFT, 50);
delay(100);
bugc.move(MOVE_FORWARD, 50);
delay(100);
bugc.move(MOVE_RIGHT, 50);
delay(100);
bugc.move(MOVE_BACKWARD, 50);
delay(100);
bugc.move(MOVE_STOP);
delay(100);
}
70 changes: 70 additions & 0 deletions examples/ir_nec_rx/ir_nec_rx.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* @file ir_nec_rx.ino
* @author SeanKwok ([email protected])
* @brief
* @version 0.1
* @date 2024-03-26
*
*
* @Hardwares: M5StickC + Hat BugC2
* @Platform Version: Arduino M5Stack Board Manager v2.1.1
* @Dependent Library:
* M5HatBugC: https://github.com/m5stack/M5Hat-BugC
* IRremote: https://github.com/Arduino-IRremote/Arduino-IRremote
*/

#include "M5HatBugC.h"
#include <IRremote.hpp>

M5HatBugC bugc;

void setup() {
Serial.begin(115200);
while (!bugc.begin(&Wire, BUGC_DEFAULT_I2C_ADDR, 0, 26, 400000U)) {
Serial.println("Couldn't find BugC");
delay(1000);
}
bugc.setAllMotorSpeed(0, 0, 0, 0);
bugc.setAllLedColor(0xff0000, 0x0000ff);

// only BugC2 support
IrReceiver.begin(BUGC2_IR_RX_PIN, ENABLE_LED_FEEDBACK);
}

void loop() {
/*
* Check if received data is available and if yes, try to decode it.
* Decoded result is in the IrReceiver.decodedIRData structure.
*
* E.g. command is in IrReceiver.decodedIRData.command
* address is in command is in IrReceiver.decodedIRData.address
* and up to 32 bit raw data in IrReceiver.decodedIRData.decodedRawData
*
* At 115200 baud, printing takes 40 ms for NEC protocol and 10 ms for NEC
* repeat
*/
if (IrReceiver.decode()) {
Serial.println();
/*
* !!!Important!!! Enable receiving of the next value,
* since receiving has stopped after the end of the current received
* data packet.
*/
IrReceiver.resume();

/*
* Finally check the received data and perform actions according to the
* received address and commands
*/
Serial.printf("address: %04X\r\n", IrReceiver.decodedIRData.address);
Serial.printf("command: %04X\r\n", IrReceiver.decodedIRData.command);
}

/*
* Your code here
* For all users of the FastLed library, use this code for strip.show() to
* improve receiving performance (which is still not 100%): if
* (IrReceiver.isIdle()) { strip.show();
* }
*/
}
16 changes: 16 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "M5Hat-BugC",
"description": "Library for M5Stack M5Hat-BugC",
"keywords": "M5Stack M5Hat-BugC",
"authors": {
"name": "M5Stack",
"url": "http://www.m5stack.com"
},
"repository": {
"type": "git",
"url": "https://github.com/m5stack/M5Hat-BugC.git"
},
"version": "1.0.0",
"frameworks": "arduino",
"platforms": "espressif32"
}
11 changes: 11 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name=M5Hat-BugC
version=1.0.0
author=M5Stack
maintainer=M5Stack
sentence=Library for M5Stack M5Hat-BugC
paragraph=
category=Device Control
url=https://github.com/m5stack/M5Hat-BugC
architectures=esp32
includes=M5HatBugC.h
depends=IRremote
93 changes: 93 additions & 0 deletions src/I2C_Class.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "I2C_Class.h"

void I2C_Class::begin(TwoWire *wire, uint8_t sda, uint8_t scl, long freq) {
_wire = wire;
_sda = sda;
_scl = scl;
_freq = freq;
_wire->end();
_wire->begin(sda, scl, freq);
}

bool I2C_Class::exist(uint8_t addr) {
int error;
_wire->beginTransmission(addr);
error = _wire->endTransmission();
if (error == 0) {
return true;
}
return false;
}

bool I2C_Class::writeBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
uint8_t length) {
_wire->beginTransmission(addr);
_wire->write(reg);
_wire->write(buffer, length);
if (_wire->endTransmission() == 0) return true;
return false;
}

bool I2C_Class::readBytes(uint8_t addr, uint8_t reg, uint8_t *buffer,
uint8_t length) {
uint8_t index = 0;
_wire->beginTransmission(addr);
_wire->write(reg);
_wire->endTransmission();
if (_wire->requestFrom(addr, length)) {
for (uint8_t i = 0; i < length; i++) {
buffer[index++] = _wire->read();
}
return true;
}
return false;
}

bool I2C_Class::readU16(uint8_t addr, uint8_t reg_addr, uint16_t *value) {
uint8_t read_buf[2] = {0x00, 0x00};
bool result = readBytes(addr, reg_addr, read_buf, 2);
*value = (read_buf[0] << 8) | read_buf[1];
return result;
}

bool I2C_Class::writeU16(uint8_t addr, uint8_t reg_addr, uint16_t value) {
uint8_t write_buf[2];
write_buf[0] = value >> 8;
write_buf[1] = value & 0xff;
return writeBytes(addr, reg_addr, write_buf, 2);
}

bool I2C_Class::writeByte(uint8_t addr, uint8_t reg, uint8_t data) {
_wire->beginTransmission(addr);
_wire->write(reg);
_wire->write(data);
if (_wire->endTransmission() == 0) return true;
return false;
}

uint8_t I2C_Class::readByte(uint8_t addr, uint8_t reg) {
_wire->beginTransmission(addr);
_wire->write(reg);
_wire->endTransmission();

if (_wire->requestFrom(addr, 1)) {
return _wire->read();
}
return 0;
}

bool I2C_Class::writeBitOn(uint8_t addr, uint8_t reg, uint8_t data) {
uint8_t temp;
uint8_t write_back;
temp = readByte(addr, reg);
write_back = (temp | data);
return (writeByte(addr, reg, write_back));
}

bool I2C_Class::writeBitOff(uint8_t addr, uint8_t reg, uint8_t data) {
uint8_t temp;
uint8_t write_back;
temp = readByte(addr, reg);
write_back = (temp & (~data));
return (writeByte(addr, reg, write_back));
}
30 changes: 30 additions & 0 deletions src/I2C_Class.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef _I2C_DEVICE_BUS_
#define _I2C_DEVICE_BUS_

#include "Arduino.h"
#include "Wire.h"

class I2C_Class {
private:
TwoWire* _wire;
uint8_t _scl;
uint8_t _sda;
long _freq;

public:
void begin(TwoWire* wire, uint8_t sda, uint8_t scl, long freq = 100000);
bool exist(uint8_t addr);

bool writeBytes(uint8_t addr, uint8_t reg, uint8_t* buffer, uint8_t length);
bool readBytes(uint8_t addr, uint8_t reg, uint8_t* buffer, uint8_t length);

bool readU16(uint8_t addr, uint8_t reg_addr, uint16_t* value);
bool writeU16(uint8_t addr, uint8_t reg_addr, uint16_t value);

bool writeByte(uint8_t addr, uint8_t reg, uint8_t data);
uint8_t readByte(uint8_t addr, uint8_t reg);
bool writeBitOn(uint8_t addr, uint8_t reg, uint8_t data);
bool writeBitOff(uint8_t addr, uint8_t reg, uint8_t data);
};

#endif
Loading

0 comments on commit 85da12f

Please sign in to comment.