generated from m5stack/M5Template-C-CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f5a70b
commit cccb883
Showing
8 changed files
with
296 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,15 @@ | ||
# Product Name | ||
# Hat CBack Driver | ||
|
||
## Overview | ||
|
||
### SKU:xxx | ||
### SKU:A100 | ||
|
||
Description of the product | ||
Hat CBack Driver is a steering gear drive board compatible with M5StickC. It adopts STM32F030F4P6 control scheme, uses I2C communication interface to communicate with M5StickC, and provides 4 sets of PWM steering gear drive interfaces (the drive power of the steering gear is directly connected to the M5StickC The internal battery can drive general specifications of the steering gear, such as: SG90, etc.). This module leads the I2C bus at the top of StickC and provides an additional GPIO interface through STM32 expansion. It can be used for general logic level and ADC analog signal input reading. With the LEGO compatible hole design on the back, users can easily integrate this driver board into the LEGO building block structure, which can be used to build controllable structures such as steering gear manipulators. | ||
|
||
## Related Link | ||
|
||
- [Document & Datasheet](https://docs.m5stack.com/en/unit/product_Link) | ||
|
||
## Required Libraries: | ||
|
||
- [Adafruit_BMP280_Library](https://github.com/adafruit/Required_Libraries_Link) | ||
|
||
## License | ||
|
||
- [Product Name- MIT](LICENSE) | ||
|
||
## Remaining steps(Editorial Staff Look,After following the steps, remember to delete all the content below) | ||
|
||
1. Change [clang format check path](./.github/workflows/clang-format-check.yml#L9-L15). | ||
2. Add License content to [LICENSE](/LICENSE). | ||
3. Change link on line 78 of [bug-report.yml](./.github/ISSUE_TEMPLATE/bug-report.yml#L78). | ||
|
||
```cpp | ||
Example | ||
# M5Unit-ENV | ||
|
||
## Overview | ||
|
||
### SKU:U001 & U001-B & U001-C | ||
|
||
Contains M5Stack-**UNIT ENV** series related case programs.ENV is an environmental sensor with integrated SHT30 and QMP6988 internally to detect temperature, humidity, and atmospheric pressure data. | ||
|
||
## Related Link | ||
|
||
- [Document & Datasheet](https://docs.m5stack.com/en/unit/envIII) | ||
|
||
## Required Libraries: | ||
|
||
- [Adafruit_BMP280_Library](https://github.com/adafruit/Adafruit_BMP280_Library) | ||
- [Document & Datasheet](https://docs.m5stack.com/en/products/sku/A100) | ||
|
||
## License | ||
|
||
- [M5Unit-ENV - MIT](LICENSE) | ||
``` | ||
- [Hat CBack Driver - MIT](LICENSE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
/* | ||
* @Hardwares: StickC/StickC-Plus/StickC-Plus2 + Hat CBack Driver | ||
* @Dependent Library: | ||
* M5GFX: https://github.com/m5stack/M5GFX | ||
* M5Unified: https://github.com/m5stack/M5Unified | ||
* M5Hat-CBack-Driver:https://github.com/m5stack/M5Hat-CBack-Driver | ||
*/ | ||
|
||
#include <M5Unified.h> | ||
#include "M5HatCBackDriver.h" | ||
|
||
M5HatCBackDriver driver; | ||
|
||
void setup() | ||
{ | ||
M5.begin(); | ||
Serial.begin(115200); | ||
Wire.begin(0, 26); | ||
M5.Display.setFont(&fonts::FreeMonoBold9pt7b); | ||
M5.Display.setRotation(1); | ||
while (!driver.begin(&Wire, M5_HAT_CBACK_DRIVER_I2C_ADDR)) { | ||
M5.Display.fillScreen(YELLOW); | ||
Serial.println("Hat CBack Driver Init Fail"); | ||
M5.Display.setCursor(0, 0); | ||
M5.Display.println("Hat CBack Driver Init Fail"); | ||
delay(1000); | ||
}; | ||
M5.Display.setCursor(0, 0); | ||
M5.Display.fillScreen(GREEN); | ||
Serial.println("Hat CBack Driver Init OK"); | ||
M5.Display.println("Hat CBack Driver Init OK"); | ||
M5.Display.println("Start Control Servo"); | ||
} | ||
|
||
void loop() | ||
{ | ||
for (int i = 0; i <= 3; i++) { // channel 0-3 | ||
driver.setServoAngle(i, 0); // 0-180 degree | ||
// driver.setServoPulse(i,500); // 500-2500us | ||
delay(200); | ||
} | ||
for (int i = 0; i <= 3; i++) { // channel 0-3 | ||
driver.setServoAngle(i, 180); // 0-180 degree | ||
// driver.setServoPulse(i,2500); // 500-2500us | ||
delay(200); | ||
} | ||
driver.digitalWritePortB(1); | ||
delay(1000); | ||
driver.digitalWritePortB(0); | ||
Serial.print("PortB Analog Read: "); | ||
Serial.println(driver.analogReadPortB()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"name": "M5Hat-CBack-Driver", | ||
"description": "Library for M5Stack M5Hat-CBack-Driver", | ||
"keywords": "M5Stack M5Hat-CBack-Driver", | ||
"authors": { | ||
"name": "M5Stack", | ||
"url": "http://www.m5stack.com" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/m5stack/M5Hat-CBack-Driver.git" | ||
}, | ||
"version": "1.0.0", | ||
"frameworks": "arduino", | ||
"platforms": "espressif32" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
name=M5Hat-CBack-Driver | ||
version=1.0.0 | ||
author=M5Stack | ||
maintainer=M5Stack | ||
sentence=Library for M5Stack M5Hat-CBack-Driver | ||
paragraph= | ||
category=Device Control | ||
url=https://github.com/m5stack/M5Hat-CBack-Driver | ||
architectures=esp32 | ||
includes=M5HatCBackDriver.h | ||
depends=M5Unified |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
*SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD | ||
* | ||
*SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
#include "M5HatCBackDriver.h" | ||
|
||
bool M5HatCBackDriver::begin(TwoWire* wire, uint8_t addr) | ||
{ | ||
_wire = wire; | ||
_addr = addr; | ||
int error; | ||
_wire->beginTransmission(addr); | ||
error = _wire->endTransmission(); | ||
if (error == 0) { | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool M5HatCBackDriver::write1Byte(uint8_t address, uint8_t reg, uint8_t data) | ||
{ | ||
_wire->beginTransmission(address); | ||
_wire->write(reg); | ||
_wire->write(data); | ||
_wire->endTransmission(); | ||
if (_wire->endTransmission() == 0) return true; | ||
return false; | ||
} | ||
|
||
bool M5HatCBackDriver::write2Byte(uint8_t address, uint8_t reg, uint16_t data) | ||
{ | ||
_wire->beginTransmission(address); | ||
_wire->write(reg); | ||
_wire->write(data >> 8); // MSB | ||
_wire->write(data & 0xFF); // LSB | ||
if (_wire->endTransmission() == 0) return true; | ||
return false; | ||
} | ||
|
||
bool M5HatCBackDriver::readBytes(uint8_t address, uint8_t reg, uint8_t count, uint8_t* dest) | ||
{ | ||
_wire->beginTransmission(address); // Initialize the Tx buffer | ||
_wire->write(reg); // Put slave register address in Tx buffer | ||
uint8_t i = 0; | ||
if (_wire->endTransmission(false) == 0 && _wire->requestFrom(address, (uint8_t)count)) { | ||
while (_wire->available()) { | ||
dest[i++] = _wire->read(); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
/*******************************************************************************/ | ||
|
||
bool M5HatCBackDriver::setServoAngle(uint8_t ch, uint8_t angle) | ||
{ | ||
if (ch > 3) return false; | ||
uint8_t reg = ch; | ||
return write1Byte(_addr, reg, angle); | ||
} | ||
|
||
bool M5HatCBackDriver::setServoPulse(uint8_t ch, uint16_t width) | ||
{ | ||
if (ch > 3) return false; | ||
uint8_t reg = (2 * ch + 16); | ||
if (reg % 2 == 1 || reg > 32) return 1; | ||
return write2Byte(_addr, reg, width); | ||
} | ||
|
||
int M5HatCBackDriver::getServoAngle(uint8_t ch) | ||
{ | ||
uint8_t data = 0; | ||
uint8_t reg = ch; | ||
if (readBytes(_addr, reg, 1, &data)) { | ||
return data; | ||
}; | ||
return -1; | ||
} | ||
|
||
int M5HatCBackDriver::getServoPulse(uint8_t ch) | ||
{ | ||
uint8_t data[2]; | ||
uint8_t reg = (2 * ch | 0x10); | ||
if (readBytes(_addr, reg, 2, data)) { | ||
return (data[0] << 8) + data[1]; | ||
}; | ||
return -1; | ||
} | ||
|
||
int M5HatCBackDriver::analogReadPortB() | ||
{ | ||
uint8_t data[2]; | ||
if (readBytes(_addr, 0x20, 2, data)) { | ||
return (data[0] << 8) + data[1]; | ||
}; | ||
return -1; | ||
} | ||
|
||
bool M5HatCBackDriver::digitalWritePortB(uint8_t status) | ||
{ | ||
return write1Byte(_addr, 0x30, status); | ||
} | ||
|
||
int M5HatCBackDriver::digitalReadPortB() | ||
{ | ||
uint8_t data; | ||
if (readBytes(_addr, 0x31, 1, &data)) { | ||
return data; | ||
} | ||
return -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
*SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD | ||
* | ||
*SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
#ifndef _M5_HAT_CBACK_DRIVER_H_ | ||
#define _M5_HAT_CBACK_DRIVER_H_ | ||
|
||
#include "Arduino.h" | ||
#include "Wire.h" | ||
|
||
#define M5_HAT_CBACK_DRIVER_I2C_ADDR 0x38 | ||
|
||
class M5HatCBackDriver { | ||
private: | ||
private: | ||
TwoWire* _wire; | ||
uint8_t _addr; | ||
bool write1Byte(uint8_t address, uint8_t reg, uint8_t data); | ||
bool write2Byte(uint8_t address, uint8_t reg, uint16_t data); | ||
bool readBytes(uint8_t address, uint8_t reg, uint8_t count, uint8_t* dest); | ||
|
||
public: | ||
/** | ||
* @brief Initializes the Hat Back Driver with I2C Bus communication parameters. | ||
* | ||
* @param wire wire Pointer to a TwoWire object, defaults to TwoWire(0) | ||
* @param addr device i2c addr, defaults to 0x38 | ||
* @return bool True if the device was successfully initialized, otherwise False. | ||
*/ | ||
bool begin(TwoWire* wire = &Wire, uint8_t addr = M5_HAT_CBACK_DRIVER_I2C_ADDR); | ||
|
||
/** | ||
* @brief Set the Servo Angle | ||
* | ||
* @param ch 0-3 | ||
* @param angle 0-180 degree | ||
* @return bool True write success, otherwise False | ||
*/ | ||
bool setServoAngle(uint8_t ch, uint8_t angle); | ||
|
||
/** | ||
* @brief Set the Servo Pulse | ||
* | ||
* @param ch 0-3 | ||
* @param width 500-2500us | ||
* @return bool True write success, otherwise False | ||
*/ | ||
bool setServoPulse(uint8_t ch, uint16_t width); | ||
|
||
/** | ||
* @brief Get the Servo Angle | ||
* | ||
* @param ch 0-3 | ||
* @return int error: -1, angle: 0-180 degree | ||
*/ | ||
int getServoAngle(uint8_t ch); | ||
|
||
/** | ||
* @brief Get the Servo Pulse | ||
* | ||
* @param ch 0-3 | ||
* @return int error: -1, pulse: 500-2500us | ||
*/ | ||
int getServoPulse(uint8_t ch); | ||
|
||
/** | ||
* @brief Read the Grove Port B Analog Input Value | ||
* | ||
* @return int error: -1, analog value: 0-4095 | ||
*/ | ||
int analogReadPortB(); | ||
|
||
/** | ||
* @brief Write the Grove Port B Digital Output Value | ||
* | ||
* @param status | ||
* @return bool True write success, otherwise False | ||
*/ | ||
bool digitalWritePortB(uint8_t status); | ||
|
||
/** | ||
* @brief Read the Grove Port B Digital Input Value | ||
* | ||
* @return int error: -1, low: 0, high: 1 | ||
*/ | ||
int digitalReadPortB(); | ||
}; | ||
#endif |