8000 Tue Mar 14 08:30:46 CDT 2017 · mwerschy/esp32-snippets@c65d370 · GitHub
[go: up one dir, main page]

Skip to content

Commit c65d370

Browse files
author
kolban
committed
Tue Mar 14 08:30:46 CDT 2017
1 parent 82e5710 commit c65d370

21 files changed

+603
-58
lines changed

cpp_utils/FreeRTOS.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77
#include <freertos/FreeRTOS.h>
88
#include <freertos/task.h>
9-
9+
#include <string>
1010
#include "FreeRTOS.h"
1111
#include "sdkconfig.h"
1212

cpp_utils/FreeRTOSTimer.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* FreeRTOSTimer.cpp
3+
*
4+
* Created on: Mar 8, 2017
5+
* Author: kolban
6+
*/
7+
8+
#include <assert.h>
9+
#include <map>
10+
11+
#include "FreeRTOSTimer.h"
12+
13+
14+
static std::map<void *, FreeRTOSTimer *> timersMap;
15+
16+
void FreeRTOSTimer::internalCallback(TimerHandle_t xTimer) {
17+
FreeRTOSTimer *timer = timersMap.at(xTimer);
18+
timer->callback(timer);
19+
}
20+
21+
/**
22+
* @brief Construct a timer.
23+
*
24+
* We construct a timer that will fire after the given period has elapsed. The period is measured
25+
* in ticks. When the timer fires, the callback function is invoked within the scope/context of
26+
* the timer demon thread. As such it must **not** block. Once the timer has fired, if the reload
27+
* flag is true, then the timer will be automatically restarted.
28+
*
29+
* Note that the timer does *not* start immediately. It starts ticking after the start() method
30+
* has been called.
31+
*
32+
* The signature of the callback function is:
33+
*
34+
* @code{.cpp}
35+
* void callback(FreeRTOSTimer *pTimer) {
36+
* // Callback code here ...
37+
* }
38+
* @endcode
< B422 code>39+
*
40+
* @param [in] name The name of the timer.
41+
* @param [in] period The period of the timer in ticks.
42+
* @param [in] reload True if the timer is to restart once fired.
43+
* @param [in] data Data to be passed to the callback.
44+
* @param [in] callback Callback function to be fired when the timer expires.
45+
*/
46+
FreeRTOSTimer::FreeRTOSTimer(
47+
char *name,
48+
TickType_t period,
49+
UBaseType_t reload,
50+
void *data,
51+
void (*callback)(FreeRTOSTimer *pTimer)) {
52+
/*
53+
* The callback function to actually be called is saved as member data in the object and
54+
* a static callback function is called. This will be passed the FreeRTOS timer handle
55+
* which is used as a key in a map to lookup the saved user supplied callback which is then
56+
* actually called.
57+
*/
58+
59+
assert(callback != nullptr);
60+
this->period = period;
61+
this->callback = callback;
62+
timerHandle = ::xTimerCreate(name, period, reload, data, internalCallback);
63+
64+
// Add the association between the timer handle and this class instance into the map.
65+
timersMap.insert(std::make_pair(timerHandle, this));
66+
} // FreeRTOSTimer
67+
68+
/**
69+
* @brief Destroy a class instance.
70+
*
71+
* The timer is deleted.
72+
*/
73+
FreeRTOSTimer::~FreeRTOSTimer() {
74+
::xTimerDelete(timerHandle, portMAX_DELAY);
75+
timersMap.erase(timerHandle);
76+
}
77+
78+
/**
79+
* @brief Start the timer ticking.
80+
*/
81+
void FreeRTOSTimer::start(TickType_t blockTime) {
82+
::xTimerStart(timerHandle, blockTime);
83+
} // start
84+
85+
/**
86+
* @brief Stop the timer from ticking.
87+
*/
88+
void FreeRTOSTimer::stop(TickType_t blockTime) {
89+
::xTimerStop(timerHandle, blockTime);
90+
} // stop
91+
92+
/**
93+
* @brief Reset the timer to the period and start it ticking.
94+
*/
95+
void FreeRTOSTimer::reset(TickType_t blockTime) {
96+
::xTimerReset(timerHandle, blockTime);
97+
} // reset
98+
99+
100+
/**
101+
* @brief Return the period of the timer.
102+
*
103+
* @return The period of the timer.
104+
*/
105+
TickType_t FreeRTOSTimer::getPeriod() {
106+
return period;
107+
} // getPeriod
108+
109+
110+
/**
111+
* @brief Change the period of the timer.
112+
*
113+
* @param [in] newPeriod The new period of the timer in ticks.
114+
*/
115+
void FreeRTOSTimer::changePeriod(TickType_t newPeriod, TickType_t blockTime) {
116+
if (::xTimerChangePeriod(timerHandle, newPeriod, blockTime) == pdPASS) {
117+
period = newPeriod;
118+
}
119+
} // changePeriod
120+
121+
122+
/**
123+
* @brief Get the name of the timer.
124+
*
125+
* @return The name of the timer.
126+
*/
127+
const char *FreeRTOSTimer::getName() {
128+
return ::pcTimerGetTimerName(timerHandle);
129+
} // getName
130+
131+
132+
/**
133+
* @brief Get the user supplied data associated with the timer.
134+
*
135+
* @return The user supplied data associated with the timer.
136+
*/
137+
void *FreeRTOSTimer::getData() {
138+
return ::pvTimerGetTimerID(timerHandle);
139+
} // getData

cpp_utils/FreeRTOSTimer.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* FreeRTOSTimer.h
3+
*
4+
* Created on: Mar 8, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef COMPONENTS_CPP_UTILS_FREERTOSTIMER_H_
9+
#define COMPONENTS_CPP_UTILS_FREERTOSTIMER_H_
10+
#include <freertos/FreeRTOS.h>
11+
#include <freertos/task.h>
12+
#include <freertos/timers.h>
13+
/**
14+
* @brief Wrapper around the FreeRTOS timer functions.
15+
*/
16+
class FreeRTOSTimer {
17+
public:
18+
FreeRTOSTimer(char *name, TickType_t period, UBaseType_t reload, void *data, void (*callback)(FreeRTOSTimer *pTimer));
19+
virtual ~FreeRTOSTimer();
20+
void changePeriod(TickType_t newPeriod, TickType_t blockTime=portMAX_DELAY);
21+
void *getData();
22+
const char *getName();
23+
TickType_t getPeriod();
24+
void reset(TickType_t blockTime=portMAX_DELAY);
25+
void start(TickType_t blockTime=portMAX_DELAY);
26+
void stop(TickType_t blockTime=portMAX_DELAY);
27+
28+
private:
29+
TimerHandle_t timerHandle;
30+
TickType_t period;
31+
void (*callback)(FreeRTOSTimer *pTimer);
32+
static void internalCallback(TimerHandle_t xTimer);
33+
};
34+
35+
#endif /* COMPONENTS_CPP_UTILS_FREERTOSTIMER_H_ */

cpp_utils/MPU6050.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
* @brief Construct an %MPU6050 handler.
1414
*/
1515
MPU6050::MPU6050() {
16-
1716
accel_x = accel_y = accel_z = 0;
1817
gyro_x = gyro_y = gyro_z = 0;
18+
i2c=nullptr;
19+
inited=false;
1920
}
2021

2122

@@ -34,6 +35,7 @@ MPU6050::~MPU6050() {
3435
* acceleration data that is then stored in the class instance ready for retrieval.
3536
*/
3637
void MPU6050::readAccel() {
38+
assert(inited);
3739
i2c->beginTransaction();
3840
i2c->write(MPU6050_ACCEL_XOUT_H);
3941
i2c->endTransaction();
@@ -56,6 +58,7 @@ void MPU6050::readAccel() {
5658
* gyroscopic data that is then stored in the class instance ready for retrieval.
5759
*/
5860
void MPU6050::readGyro() {
61+
assert(inited);
5962
i2c->beginTransaction();
6063
i2c->write(MPU6050_GYRO_XOUT_H);
6164
i2c->endTransaction();
@@ -91,4 +94,5 @@ void MPU6050::init(gpio_num_t sdaPin, gpio_num_t clkPin) {
9194
i2c->write(MPU6050_PWR_MGMT_1);
9295
i2c->write(0);
9396
i2c->endTransaction();
97+
inited=true;
9498
}

cpp_utils/MPU6050.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class MPU6050 {
2020
I2C *i2c;
2121
short accel_x, accel_y, accel_z;
2222
short gyro_x, gyro_y, gyro_z;
23+
bool inited;
2324
public:
2425
MPU6050();
2526
virtual ~MPU6050();

cpp_utils/NeoPixelWiFiEventHandler.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,53 @@
77
#include <stdio.h>
88
#include "NeoPixelWiFiEventHandler.h"
99

10-
NeoPixelWiFiEventHandler::NeoPixelWiFiEventHandler() {
10+
NeoPixelWiFiEventHandler::NeoPixelWiFiEventHandler(gpio_num_t gpioPin) {
11+
this->gpioPin = gpioPin;
12+
ws2812 = new WS2812(gpioPin, 8);
1113
}
1214

1315
NeoPixelWiFiEventHandler::~NeoPixelWiFiEventHandler() {
16+
delete ws2812;
1417
}
1518

1619
esp_err_t NeoPixelWiFiEventHandler::apStart() {
1720
printf("XXX apStart\n");
18-
ws2812.setPixel(0, 0, 00, 64);
19-
ws2812.show();
21+
ws2812->setPixel(0, 0, 00, 64);
22+
ws2812->show();
2023
return ESP_OK;
2124
}
2225

2326
esp_err_t NeoPixelWiFiEventHandler::staConnected() {
2427
printf("XXX staConnected\n");
25-
ws2812.setPixel(0, 57, 89, 66);
26-
ws2812.show();
28+
ws2812->setPixel(0, 57, 89, 66);
29+
ws2812->show();
2730
return ESP_OK;
2831
}
2932

3033
esp_err_t NeoPixelWiFiEventHandler::staDisconnected() {
3134
printf("XXX staDisconnected\n");
32-
ws2812.setPixel(0, 64, 0, 0);
33-
ws2812.show();
35+
ws2812->setPixel(0, 64, 0, 0);
36+
ws2812->show();
3437
return ESP_OK;
3538
}
3639

3740
esp_err_t NeoPixelWiFiEventHandler::staStart() {
3841
printf("XXX staStart\n");
39-
ws2812.setPixel(0, 64, 64, 0);
40-
ws2812.show();
42+
ws2812->setPixel(0, 64, 64, 0);
43+
ws2812->show();
4144
return ESP_OK;
4245
}
4346

4447
esp_err_t NeoPixelWiFiEventHandler::staGotIp(system_event_sta_got_ip_t event_sta_got_ip) {
4548
printf("XXX staGotIp\n");
46-
ws2812.setPixel(0, 0, 64, 0);
47-
ws2812.show();
49+
ws2812->setPixel(0, 0, 64, 0);
50+
ws2812->show();
4851
return ESP_OK;
4952
}
5053

5154
esp_err_t NeoPixelWiFiEventHandler::wifiReady() {
5255
printf("XXX wifiReady\n");
53-
ws2812.setPixel(0, 64, 64, 0);
54-
ws2812.show();
56+
ws2812->setPixel(0, 64, 64, 0);
57+
ws2812->show();
5558
return ESP_OK;
5659
}

cpp_utils/NeoPixelWiFiEventHandler.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
class NeoPixelWiFiEventHandler: public WiFiEventHandler {
2121
public:
22-
NeoPixelWiFiEventHandler();
22+
NeoPixelWiFiEventHandler(gpio_num_t gpioPin);
2323
virtual ~NeoPixelWiFiEventHandler();
2424

2525
esp_err_t apStart() override;
@@ -29,7 +29,8 @@ class NeoPixelWiFiEventHandler: public WiFiEventHandler {
2929
esp_err_t wifiReady() override;
3030
esp_err_t staStart() override;
3131
private:
32-
WS2812 ws2812 = WS2812(GPIO_NUM_16, 8);
32+
gpio_num_t gpioPin;
33+
WS2812 *ws2812;
3334
};
3435

3536
#endif /* MAIN_NEOPIXELWIFIEVENTHANDLER_H_ */

cpp_utils/PWM.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* PWM.cpp
3+
*
4+
* Created on: Mar 9, 2017
5+
* Author: kolban
6+
*/
7+
8+
#include "PWM.h"
9+
10+
/**
11+
* @brief Construct an instance.
12+
*
13+
* @param [in] bitSize The size in bits of the timer. Allowed values are LEDC_TIMER_10_BIT,
14+
* LEDC_TIMER_11_BIT, LEDC_TIMER_12_BIT, LEDC_TIMER_13_BIT, LEDC_TIMER_14_BIT, LEDC_TIMER_15_BIT.
15+
* @param [in] timer The timer to use. A value of LEDC_TIMER0, LEDC_TIMER1, LEDC_TIMER2 or LEDC_TIMER3.
16+
* @param [in] channel The channel to use. A value from LEDC_CHANNEL0 to LEDC_CHANNEL1.
17+
*/
18+ 10000
PWM::PWM(ledc_timer_bit_t bitSize, ledc_timer_t timer, ledc_channel_t channel,
19+
int gpioNum) {
20+
ledc_timer_config_t timer_conf;
21+
timer_conf.bit_num = bitSize;
22+
timer_conf.freq_hz = 100;
23+
timer_conf.speed_mode = LEDC_HIGH_SPEED_MODE;
24+
timer_conf.timer_num = timer;
25+
::ledc_timer_config(&timer_conf);
26+
27+
ledc_channel_config_t ledc_conf;
28+
ledc_conf.channel = channel;
29+
ledc_conf.duty = 0;
30+
ledc_conf.gpio_num = gpioNum;
31+
ledc_conf.intr_type = LEDC_INTR_DISABLE;
32+
ledc_conf.speed_mode = LEDC_HIGH_SPEED_MODE;
33+
ledc_conf.timer_sel = timer;
34+
::ledc_channel_config(&ledc_conf);
35+
36+
this->channel = channel;
37+
this->timer = timer;
38+
}
39+
40+
41+
/**
42+
* @brief Get the duty cycle value.
43+
*
44+
* @return The duty cycle value.
45+
*/
46+
uint32_t PWM::getDuty() {
47+
return ::ledc_get_duty(LEDC_HIGH_SPEED_MODE, channel);
48+
} // getDuty
49+
50+
51+
/**
52+
* @brief Get the frequency/period in Hz.
53+
*
54+
* @return The frequency/period in Hz.
55+
*/
56+
uint32_t PWM::getFrequency() {
57+
return ::ledc_get_freq(LEDC_HIGH_SPEED_MODE, timer);
58+
} // getFrequency
59+
60+
61+
/**
62+
* @brief Set the duty cycle value.
63+
*/
64+
void PWM::setDuty(uint32_t duty) {
65+
::ledc_set_duty(LEDC_HIGH_SPEED_MODE, channel, duty);
66+
::ledc_update_duty(LEDC_HIGH_SPEED_MODE, channel);
67+
} // setDuty
68+
69+
70+
/**
71+
* @brief Set the frequency/period in Hz.
72+
*/
73+
void PWM::setFrequency(uint32_t freq) {
74+
::ledc_set_freq(LEDC_HIGH_SPEED_MODE, timer, freq);
75+
} // setFrequency
76+
77+
/**
78+
* @brief Stop the %PWM.
79+
*/
80+
void PWM::stop(bool idleLevel) {
81+
::ledc_stop(LEDC_HIGH_SPEED_MODE, channel, idleLevel);
82+
} // stop

0 commit comments

Comments
 (0)
0