Skip to content

Commit 7748dba

Browse files
committed
apds example integration
1 parent bd79c31 commit 7748dba

15 files changed

+1835
-0
lines changed

apds9960/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
server_certs
3+
sdkconfig
4+
CMakeFiles

apds9960/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The following four lines of boilerplate have to be in your project's CMakeLists
2+
# in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.5)
4+
5+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
6+
7+
project(rgb_led)

apds9960/Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
3+
# project subdirectory.
4+
#
5+
PROJECT_NAME := apds9960
6+
7+
EXTRA_COMPONENT_DIRS := $(shell pwd)/../components/ArduinoJson
8+
9+
include $(IDF_PATH)/make/project.mk
10+
IDF_PATH := $(IOT_SOLUTION_PATH)/submodule/esp-idf/
11+
include $(IOT_SOLUTION_PATH)/components/component_conf.mk

apds9960/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# ESP-MQTT sample application
2+
(See the README.md file in the upper level 'examples' directory for more information about examples.)
3+
4+
This example connects to the broker URI selected using `make menuconfig` (using mqtt tcp transport) and as a demonstration subscribes/unsubscribes and send a message on certain topic.
5+
6+
It uses ESP-MQTT library which implements mqtt client to connect to mqtt broker.
7+
8+
## How to use example
9+
10+
### Hardware Required
11+
12+
This example can be executed on any ESP32 board, the only required interface is WiFi and connection to internet.
13+
14+
### Configure the project
15+
16+
```
17+
make menuconfig
18+
```
19+
20+
* Set serial port under Serial Flasher Options.
21+
22+
* Set ssid and password for the board to connect to AP.
23+
24+
### Build and Flash
25+
26+
Build the project and flash it to the board, then run monitor tool to view serial output:
27+
28+
```
29+
make -j4 flash monitor
30+
```
31+
32+
(To exit the serial monitor, type ``Ctrl-]``.)
33+
34+
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
35+
36+
## Example Output
37+
38+
```
39+
I (3714) event: sta ip: 192.168.0.139, mask: 255.255.255.0, gw: 192.168.0.2
40+
I (3714) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
41+
I (3964) MQTT_CLIENT: Sending MQTT CONNECT message, type: 1, id: 0000
42+
I (4164) MQTT_EXAMPLE: MQTT_EVENT_CONNECTED
43+
I (4174) MQTT_EXAMPLE: sent publish successful, msg_id=41464
44+
I (4174) MQTT_EXAMPLE: sent subscribe successful, msg_id=17886
45+
I (4174) MQTT_EXAMPLE: sent subscribe successful, msg_id=42970
46+
I (4184) MQTT_EXAMPLE: sent unsubscribe successful, msg_id=50241
47+
I (4314) MQTT_EXAMPLE: MQTT_EVENT_PUBLISHED, msg_id=41464
48+
I (4484) MQTT_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=17886
49+
I (4484) MQTT_EXAMPLE: sent publish successful, msg_id=0
50+
I (4684) MQTT_EXAMPLE: MQTT_EVENT_SUBSCRIBED, msg_id=42970
51+
I (4684) MQTT_EXAMPLE: sent publish successful, msg_id=0
52+
I (4884) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19
53+
I (4884) MQTT_EXAMPLE: MQTT_EVENT_DATA
54+
TOPIC=/topic/qos0
55+
DATA=data
56+
I (5194) MQTT_CLIENT: deliver_publish, message_length_read=19, message_length=19
57+
I (5194) MQTT_EXAMPLE: MQTT_EVENT_DATA
58+
TOPIC=/topic/qos0
59+
DATA=data
60+
```

apds9960/main/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
set(COMPONENT_SRCS "app_main.cpp")
2+
set(COMPONENT_ADD_INCLUDEDIRS "../ArduinoJson"
3+
".")
4+
5+
register_component()

apds9960/main/GPIO.cpp

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
/*
2+
* GPIO.cpp
3+
*
4+
* Created on: Feb 28, 2017
5+
* Author: kolban
6+
*/
7+
8+
#include "GPIO.h"
9+
#include <driver/gpio.h>
10+
#include "sdkconfig.h"
11+
#include <esp_log.h>
12+
#include <esp_err.h>
13+
#include "GeneralUtils.h"
14+
15+
static const char* LOG_TAG = "GPIO";
16+
17+
static bool g_isrServiceInstalled = false;
18+
19+
/**
20+
* @brief Add an ISR handler to the pin.
21+
* @param [in] pin The pin to have the ISR associated with it.
22+
* @param [in] handler The function to be invoked when the interrupt is detected.
23+
* @param [in] pArgs Optional arguments to pass to the handler.
24+
*/
25+
void ESP32CPP::GPIO::addISRHandler(gpio_num_t pin, gpio_isr_t handler, void* pArgs) {
26+
ESP_LOGD(LOG_TAG, ">> addISRHandler: pin=%d", pin);
27+
28+
// If we have not yet installed the ISR service handler, install it now.
29+
if (!g_isrServiceInstalled) {
30+
ESP_LOGD(LOG_TAG, "Installing the global ISR service");
31+
esp_err_t errRc = ::gpio_install_isr_service(0);
32+
if (errRc != ESP_OK) {
33+
ESP_LOGE(LOG_TAG, "<< gpio_install_isr_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
34+
return;
35+
}
36+
g_isrServiceInstalled = true;
37+
}
38+
39+
esp_err_t errRc = ::gpio_isr_handler_add(pin, handler, pArgs);
40+
if (errRc != ESP_OK) {
41+
ESP_LOGE(LOG_TAG, "<< gpio_isr_handler_add: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
42+
return;
43+
}
44+
45+
ESP_LOGD(LOG_TAG, "<< addISRHandler");
46+
} // addISRHandler
47+
48+
49+
/**
50+
* @brief Set the pin high.
51+
*
52+
* Ensure that the pin is set to be output prior to calling this method.
53+
*
54+
* @param [in] pin The pin to be set high.
55+
* @return N/A.
56+
*/
57+
void ESP32CPP::GPIO::high(gpio_num_t pin) {
58+
write(pin, true);
59+
} // high
60+
61+
62+
/**
63+
* @brief Determine if the pin is a valid pin for an ESP32 (i.e. is it in range).
64+
*
65+
* @param [in] pin The pin number to validate.
66+
* @return The value of true if the pin is valid and false otherwise.
67+
*/
68+
bool ESP32CPP::GPIO::inRange(gpio_num_t pin) {
69+
return (pin >= 0 && pin <= 39);
70+
} // inRange
71+
72+
73+
/**
74+
* @brief Disable interrupts on the named pin.
75+
* @param [in] pin The pin to disable interrupts upon.
76+
* @return N/A.
77+
*/
78+
void ESP32CPP::GPIO::interruptDisable(gpio_num_t pin) {
79+
esp_err_t rc = ::gpio_intr_disable(pin);
80+
if (rc != ESP_OK) {
81+
ESP_LOGE(LOG_TAG, "interruptDisable: %d", rc);
82+
}
83+
} // interruptDisable
84+
85+
86+
/**
87+
* @brief Enable interrupts on the named pin.
88+
* @param [in] pin The pin to enable interrupts upon.
89+
* @return N/A.
90+
*/
91+
void ESP32CPP::GPIO::interruptEnable(gpio_num_t pin) {
92+
esp_err_t rc = ::gpio_intr_enable(pin);
93+
if (rc != ESP_OK) {
94+
ESP_LOGE(LOG_TAG, "interruptEnable: %d", rc);
95+
}
96+
} // interruptEnable
97+
98+
99+
/**
100+
* @brief Set the pin low.
101+
*
102+
* Ensure that the pin is set to be output prior to calling this method.
103+
*
104+
* @param [in] pin The pin to be set low.
105+
* @return N/A.
106+
*/
107+
void ESP32CPP::GPIO::low(gpio_num_t pin) {
108+
write(pin, false);
109+
} // low
110+
111+
112+
/**
113+
* @brief Read a value from the given pin.
114+
*
115+
* Ensure the pin is set as input before calling this method.
116+
* @param [in] pin The pin to read from.
117+
* @return True if the pin is high, false if the pin is low.
118+
*/
119+
bool ESP32CPP::GPIO::read(gpio_num_t pin) {
120+
return ::gpio_get_level(pin) == 1;
121+
} // read
122+
123+
124+
/**
125+
* @brief Set the pin as input.
126+
*
127+
* Set the direction of the pin as input.
128+
* @param [in] pin The pin to set as input.
129+
* @return N/A.
130+
*/
131+
void ESP32CPP::GPIO::setInput(gpio_num_t pin) {
132+
::gpio_set_direction(pin, GPIO_MODE_INPUT);
133+
} // setInput
134+
135+
136+
/**
137+
* @brief Set the interrupt type.
138+
* The type of interrupt can be one of:
139+
*
140+
* * GPIO_INTR_ANYEDGE
141+
* * GPIO_INTR_DISABLE
142+
* * GPIO_INTR_NEGEDGE
143+
* * GPIO_INTR_POSEDGE
144+
* * GPIO_INTR_LOW_LEVEL
145+
* * GPIO_INTR_HIGH_LEVEL
146+
*
147+
* @param [in] pin The pin to set the interrupt upon.
148+
* @param [in] intrType The type of interrupt.
149+
* @return N/A.
150+
*/
151+
void ESP32CPP::GPIO::setInterruptType(gpio_num_t pin, gpio_int_type_t intrType) {
152+
esp_err_t rc = ::gpio_set_intr_type(pin, intrType);
153+
if (rc != ESP_OK) {
154+
ESP_LOGE(LOG_TAG, "setInterruptType: %d", rc);
155+
}
156+
} // setInterruptType
157+
158+
159+
/**
160+
* @brief Set the pin as output.
161+
*
162+
* Set the direction of the pin as output. Note that pins 34 through 39 are input only and can **not** be
163+
* set as output.
164+
* @param [in] pin The pin to set as output.
165+
* @return N/A.
166+
*/
167+
void ESP32CPP::GPIO::setOutput(gpio_num_t pin) {
168+
::gpio_set_direction(pin, GPIO_MODE_OUTPUT);
169+
} // setOutput
170+
171+
172+
/**
173+
* @brief Write a value to the given pin.
174+
*
175+
* Ensure that the pin is set as output before calling this method.
176+
* @param [in] pin The gpio pin to change.
177+
* @param [out] value The value to be written to the pin.
178+
* @return N/A.
179+
*/
180+
void ESP32CPP::GPIO::write(gpio_num_t pin, bool value) {
181+
//ESP_LOGD(LOG_TAG, ">> write: pin: %d, value: %d", pin, value);
182+
esp_err_t errRc = ::gpio_set_level(pin, value ? 1 : 0);
183+
if (errRc != ESP_OK) {
184+
ESP_LOGE(LOG_TAG, "<< gpio_set_level: pin=%d, rc=%d %s", pin, errRc, GeneralUtils::errorToString(errRc));
185+
}
186+
} // write
187+
188+
189+
/**
190+
* @brief Write up to 8 bits of data to a set of pins.
191+
* @param [in] pins An array of pins to set their values.
192+
* @param [in] value The data value to write.
193+
* @param [in] bits The number of bits to write.
194+
*/
195+
void ESP32CPP::GPIO::writeByte(gpio_num_t pins[], uint8_t value, int bits) {
196+
ESP_LOGD(LOG_TAG, ">> writeByte: value: %.2x, bits: %d", value, bits);
197+
for (int i = 0; i < bits; i++) {
198+
//ESP_LOGD(LOG_TAG, "i=%d, bits=%d", i, bits);
199+
write(pins[i], (value & (1 << i)) != 0);
200+
}
201+
ESP_LOGD(LOG_TAG, "<< writeByte");
202+
} // writeByte

apds9960/main/GPIO.h

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* GPIO.h
3+
*
4+
* Created on: Feb 28, 2017
5+
* Author: kolban
6+
*/
7+
8+
#ifndef COMPONENTS_CPP_UTILS_GPIO_H_
9+
#define COMPONENTS_CPP_UTILS_GPIO_H_
10+
#include <driver/gpio.h>
11+
namespace ESP32CPP {
12+
/**
13+
* @brief Interface to %GPIO functions.
14+
*
15+
* The %GPIO functions encapsulate the %GPIO access. The GPIOs available to us are
16+
* 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,21,23,25,26,27,32,33,34,35,36,37,38,39.
17+
*
18+
* The GPIOs of 34,35,36,37,38 and 39 are input only.
19+
*
20+
* Note that we must not use `int` values for the pin numbers but instead use the `gpio_num_t`. There
21+
* are constants defined for these of the form `GPIO_NUM_xx`.
22+
*
23+
* To toggle a pin we might code:
24+
*
25+
* @code{.cpp}
26+
* ESP32CPP::GPIO::setOutput(pin);
27+
* ESP32CPP::GPIO::high(pin);
28+
* ESP32CPP::GPIO::low(pin);
29+
* @endcode
30+
*/
31+
class GPIO {
32+
public:
33+
static void addISRHandler(gpio_num_t pin, gpio_isr_t handler, void* pArgs);
34+
static void high(gpio_num_t pin);
35+
static void interruptDisable(gpio_num_t pin);
36+
static void interruptEnable(gpio_num_t pin);
37+
static bool inRange(gpio_num_t pin);
38+
static void low(gpio_num_t pin);
39+
static bool read(gpio_num_t pin);
40+
static void setInput(gpio_num_t pin);
41+
static void setInterruptType(gpio_num_t pin, gpio_int_type_t intrType);
42+
static void setOutput(gpio_num_t pin);
43+
static void write(gpio_num_t pin, bool value);
44+
static void writeByte(gpio_num_t pins[], uint8_t value, int bits);
45+
}; // End GPIO
46+
} // End ESP32CPP namespace
47+
#endif /* COMPONENTS_CPP_UTILS_GPIO_H_ */

0 commit comments

Comments
 (0)