8000 Tue Feb 28 09:02:22 CST 2017 · programmer131/esp32-snippets@996b2df · GitHub
[go: up one dir, main page]

Skip to content

Commit 996b2df

Browse files
author
kolban
committed
Tue Feb 28 09:02:22 CST 2017
1 parent d95258e commit 996b2df

File tree

266 files changed

+17404
-11
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

266 files changed

+17404
-11
lines changed

cpp_utils/Doxyfile

Lines changed: 2479 additions & 0 deletions
Large diffs are not rendered by default.

cpp_utils/FreeRTOS.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* FreeRTOS.cpp
3+
*
4+
* Created on: Feb 24, 2017
5+
* Author: kolban
6+
*/
7+
#include <freertos/FreeRTOS.h>
8+
#include <freertos/task.h>
9+
10+
#include "FreeRTOS.h"
11+
#include "sdkconfig.h"
12+
13+
FreeRTOS::FreeRTOS() {
14+
// TODO Auto-generated constructor stub
15+
16+
}
17+
18+
FreeRTOS::~FreeRTOS() {
19+
// TODO Auto-generated destructor stub
20+
}
21+
22+
/**
23+
* Sleep for the specified number of milliseconds.
24+
* @param[in] ms The period in milliseconds for which to sleep.
25+
*/
26+
void FreeRTOS::sleep(uint32_t ms) {
27+
::vTaskDelay(ms/portTICK_PERIOD_MS);
28+
}
29+
30+
/**
31+
* Start a new task.
32+
* @param[in] task The function pointer to the function to be run in the task.
33+
* @param[in] taskName A string identifier for the task.
34+
* @param[in] param An optional parameter to be passed to the started task.
35+
* @param[in] stackSize An optional paremeter supplying the size of the stack in which to run the task.
36+
*/
37+
void FreeRTOS::startTask(void task(void*), std::string taskName, void *param, int stackSize) {
38+
::xTaskCreate(task, taskName.data(), stackSize, param, 5, NULL);
39+
}
40+
41+
/**
42+
* Delete the task.
43+
* @param[in] pTask An optional handle to the task to be deleted. If not supplied the calling task will be deleted.
44+
*/
45+
void FreeRTOS::deleteTask(TaskHandle_t pTask) {
46+
::vTaskDelete(pTask);
47+
}
48+
49+
/**
50+
* Get the time in milliseconds since the FreeRTOS scheduler started.
51+
* @return The time in milliseconds since the FreeRTOS scheduler started.
52+
*/
53+
uint32_t FreeRTOS::getTimeSinceStart() {
54+
return (uint32_t)(xTaskGetTickCount()*portTICK_PERIOD_MS);
55+
}

cpp_utils/FreeRTOS.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* FreeRTOS.h
3+
*
4+
* Created on: Feb 24, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef MAIN_FREERTOS_H_
9+
#define MAIN_FREERTOS_H_
10+
#include <stdint.h>
11+
#include <string>
12+
13+
#include <freertos/FreeRTOS.h>
14+
#include <freertos/task.h>
15+
16+
class FreeRTOS {
17+
public:
18+
FreeRTOS();
19+
virtual ~FreeRTOS();
20+
static void sleep(uint32_t ms);
21+
static void startTask(void task(void *), std::string taskName, void *param=nullptr, int stackSize = 2048);
22+
static void deleteTask(TaskHandle_t pTask = nullptr);
23+
24+
static uint32_t getTimeSinceStart();
25+
};
26+
27+
#endif /* MAIN_FREERTOS_H_ */

cpp_utils/I2C.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
/*
2+
* I2C.cpp
3+
*
4+
* Created on: Feb 24, 2017
5+
* Author: kolban
6+
*/
7+
#include <driver/gpio.h>
8+
#include <driver/i2c.h>
9+
#include <esp_err.h>
10+
#include <stdint.h>
11+
#include <sys/types.h>
12+
#include "I2C.h"
13+
#include "sdkconfig.h"
14+
15+
16+
/**
17+
* Create an instance of an I2C object.
18+
* @param[in] sdaPin The pin number used for the SDA line.
19+
* @param[in] sclPin The pin number used for the SCL line.
20+
*/
21+
I2C::I2C(int sdaPin, int sclPin) {
22+
i2c_config_t conf;
23+
conf.mode = I2C_MODE_MASTER;
24+
conf.sda_io_num = (gpio_num_t)sdaPin;
25+
conf.scl_io_num = (gpio_num_t)sclPin;
26+
conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
27+
conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
28+
conf.master.clk_speed = 100000;
29+
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &conf));
30+
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0));
31+
directionKnown = false;
32+
address = 0;
33+
cmd = 0;
34+
}
35+
36+
37+
/**
38+
* Begin a new I2C transaction.
39+
*/
40+
void I2C::beginTransaction() {
41+
cmd = i2c_cmd_link_create();
42+
ESP_ERROR_CHECK(i2c_master_start(cmd));
43+
directionKnown = false;
44+
}
45+
46+
/**
47+
* End an I2C transaction.
48+
* This call will execute the I2C requests that have been queued up since the preceding call to the
49+
* `beginTransaction()` function.
50+
*/
51+
void I2C::endTransaction() {
52+
ESP_ERROR_CHECK(i2c_master_stop(cmd));
53+
i2c_master_cmd_begin(I2C_NUM_0, cmd, 1000/portTICK_PERIOD_MS);
54+
i2c_cmd_link_delete(cmd);
55+
}
56+
57+
58+
/**
59+
* Write a single byte to the I2C slave.
60+
* @param[in] byte The byte to write to the slave.
61+
* @param[in] ack Whether or not an acknowledgement is expected from the slave.
62+
*/
63+
void I2C::write(uint8_t byte, bool ack) {
64+
if (directionKnown == false) {
65+
directionKnown = true;
66+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, 1));
67+
}
68+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, byte, ack));
69+
}
70+
71+
/**
72+
* Write a sequence of byte to the I2C slave.
73+
* @param[in] bytes The sequence of bytes to write to the I2C slave.
74+
* @param[in] length The number of bytes to write.
75+
* @param[in] ack Whether or not an acknowledgement is expected from the slave.
76+
*/
77+
void I2C::write(uint8_t *bytes, size_t length, bool ack) {
78+
if (directionKnown == false) {
79+
directionKnown = true;
80+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, ack));
81+
}
82+
ESP_ERROR_CHECK(i2c_master_write(cmd, bytes, length, 1));
83+
}
84+
85+
86+
/**
87+
* Add an I2C start request to the command stream.
88+
*/
89+
void I2C::start() {
90+
ESP_ERROR_CHECK(i2c_master_start(cmd));
91+
}
92+
93+
/**
94+
* Read a single byte from the slave.
95+
* @param[out] byte The address into which the read byte will be stored.
96+
* @param[in] ack Whether or not we should send an ACK to the slave after reading a byte.
97+
*/
98+
void I2C::readByte(uint8_t* byte, bool ack) {
99+
if (directionKnown == false) {
100+
directionKnown = true;
101+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_READ, 1));
102+
}
103+
ESP_ERROR_CHECK(i2c_master_read_byte(cmd, byte, !ack));
104+
}
105+
106+
/**
107+
* Read a sequence of bytes from the slave.
108+
* @param[out] byte The address into which the read bytes will be stored.
109+
* @param[in] length The number of expected bytes to read.
110+
* @param[in] ack Whether or not we should send an ACK to the slave after reading a byte.
111+
*/
112+
void I2C::read(uint8_t* bytes, size_t length, bool ack) {
113+
if (directionKnown == false) {
114+
directionKnown = true;
115+
ESP_ERROR_CHECK(i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_READ, 1));
116+
}
117+
ESP_ERROR_CHECK(i2c_master_read(cmd, bytes, length, !ack));
118+
}
119+
120+
121+
/**
122+
* Add an I2C stop request to the command stream.
123+
*/
124+
void I2C::stop() {
125+
ESP_ERROR_CHECK(i2c_master_stop(cmd));
126+
}

cpp_utils/I2C.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* I2C.h
3+
*
4+
* Created on: Feb 24, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef MAIN_I2C_H_
9+
#define MAIN_I2C_H_
10+
#include <stdint.h>
11+
#include <sys/types.h>
12+
#include <driver/i2c.h>
13+
14+
class I2C {
15+
private:
16+
uint8_t address;
17+
i2c_cmd_handle_t cmd;
18+
bool directionKnown;
19+
20+
public:
21+
I2C(int sdaPin, int sclPin);
22+
void beginTransaction();
23+
void endTransaction();
24+
void write(uint8_t byte, bool ack=true);
25+
void write(uint8_t *bytes, size_t length, bool ack=true);
26+
void readByte(uint8_t *byte, bool ack=true);
27+
void read(uint8_t *bytes, size_t length, bool ack=true);
28+
void start();
29+
void stop();
30+
31+
/**
32+
* Get the address of the I2C slave against which we are working.
33+
* @return The address of the I2C slave.
34+
*/
35+
uint8_t getAddress() const
36+
{
37+
return address;
38+
}
39+
40+
/**
41+
* Set the address of the I2C slave against which we will be working.
42+
* @param[in] address The address of the I2C slave.
43+
*/
44+
void setAddress(uint8_t address)
45+
{
46+
this->address = address;
47+
}
48+
};
49+
50+
#endif /* MAIN_I2C_H_ */

cpp_utils/MPU6050.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Encapsulate the MPU6050.
3+
*/
4+
5+
#include "MPU6050.h"
6+
#define PIN_SDA 25
7+
#define PIN_CLK 26
8+
#define I2C_ADDRESS 0x68 // I2C address of MPU6050
9+
10+
#define MPU6050_ACCEL_XOUT_H 0x3B
11+
#define MPU6050_GYRO_XOUT_H 0x43
12+
#define MPU6050_PWR_MGMT_1 0x6B
13+
14+
/**
15+
* Construct an MPU6050 handler.
16+
*/
17+
MPU6050::M 10000 PU6050() {
18+
i2c = new I2C(PIN_SDA, PIN_CLK);
19+
// Dummy call
20+
i2c->setAddress(I2C_ADDRESS);
21+
i2c->beginTransaction();
22+
i2c->write(MPU6050_ACCEL_XOUT_H);
23+
i2c->endTransaction();
24+
25+
i2c->beginTransaction();
26+
i2c->write(MPU6050_PWR_MGMT_1);
27+
i2c->write(0);
28+
i2c->endTransaction();
29+
accel_x = accel_y = accel_z = 0;
30+
gyro_x = gyro_y = gyro_z = 0;
31+
}
32+
33+
34+
MPU6050::~MPU6050() {
35+
delete i2c;
36+
}
37+
38+
39+
/**
40+
* Read the acceleration value from the device.
41+
*/
42+
void MPU6050::readAccel() {
43+
i2c->beginTransaction();
44+
i2c->write(MPU6050_ACCEL_XOUT_H);
45+
i2c->endTransaction();
46+
47+
uint8_t data[6];
48+
i2c->beginTransaction();
49+
i2c->read(data, 5, 1);
50+
i2c->readByte(data+5, 0);
51+
i2c->endTransaction();
52+
53+
accel_x = (data[0] << 8) | data[1];
54+
accel_y = (data[2] << 8) | data[3];
55+
accel_z = (data[4] << 8) | data[5];
56+
} // readAccel
57+
58+
/**
59+
* Read the gyroscopic values from the device.
60+
*/
61+
void MPU6050::readGyro() {
62+
i2c->beginTransaction();
63+
i2c->write(MPU6050_GYRO_XOUT_H);
64+
i2c->endTransaction();
65+
66+
uint8_t data[6];
67+
i2c->beginTransaction();
68+
i2c->read(data, 5, 1);
69+
i2c->readByte(data+5, 0);
70+
i2c->endTransaction();
71+
72+
gyro_x = (data[0] << 8) | data[1];
73+
gyro_y = (data[2] << 8) | data[3];
74+
gyro_z = (data[4] << 8) | data[5];
75+
} // readGyro

0 commit comments

Comments
 (0)
0