1 /****************************************************************************
4 * WS2812 Chainable leds example using Adafruit les strip
6 * Copyright 2013-2015 Nathael Pajani <nathael.pajani@ed3l.fr>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 *************************************************************************** */
25 #include "lib/stdint.h"
26 #include "core/system.h"
27 #include "core/systick.h"
29 #include "lib/stdio.h"
30 #include "drivers/serial.h"
31 #include "drivers/gpio.h"
32 #include "drivers/adc.h"
33 #include "drivers/i2c.h"
34 #include "drivers/timers.h"
36 #include "extdrv/ws2812.h"
37 #include "extdrv/tmp101_temp_sensor.h"
40 #define MODULE_VERSION 0x01
41 #define MODULE_NAME "E-Xanh Gardener"
44 #define SELECTED_FREQ FREQ_SEL_36MHz
48 /***************************************************************************** */
49 /* Initial Pins configuration */
50 const struct pio_config common_pins[] = {
52 { LPC_GPIO_0_0, LPC_UART0_RX, 0 },
53 { LPC_GPIO_0_4, LPC_UART0_TX, 0 },
55 { LPC_I2C0_SCL_PIO_0_10, LPC_FIXED, 0 },
56 { LPC_I2C0_SDA_PIO_0_11, LPC_FIXED, 0 },
58 { LPC_ADC_AD2_PIO_0_14, LPC_FIXED, 0 }, /* Debug */
59 { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
60 { LPC_ADC_AD10_PIO_0_13, LPC_FIXED, 0 },
62 { LPC_GPIO_0_8, LPC_SCT_POUT0, 0 },
63 { LPC_GPIO_0_9, LPC_SCT_POUT1, 0 },
64 { LPC_GPIO_0_3, LPC_SCT_POUT2, 0 }, /* Debug */
66 { LPC_GPIO_0_2, LPC_GPIO, 0 },
67 { LPC_GPIO_0_12, LPC_GPIO, 0 },
71 /* Configure pins as used for capacitance sensing */
72 const struct pio_config capacitance_pins[] = {
73 { LPC_GPIO_0_9, LPC_SCT_POUT1, 0 },
74 { LPC_ADC_AD9_PIO_0_17, LPC_FIXED, 0 },
78 #define TMP101_ADDR 0x94 /* Pin Addr0 (pin5 of tmp101) connected to VCC */
79 struct tmp101_sensor_config tmp101_sensor = {
82 .resolution = TMP_RES_ELEVEN_BITS,
85 const struct pio button = LPC_GPIO_0_12; /* ISP button */
86 const struct pio ws2812_data_out_pin = LPC_GPIO_0_2; /* Led control data pin */
88 const struct pio cap_trig_gpio = LPC_GPIO_0_9;
89 const struct pio cap_adc_gpio = LPC_GPIO_0_17;
91 /***************************************************************************** */
93 /* The Temperature Alert pin is on GPIO Port 0, pin 7 (PIO0_7) */
94 /* The I2C Temperature sensor is at address 0x94 */
95 void temp_config(int uart_num)
100 ret = tmp101_sensor_config(&tmp101_sensor);
102 serial_write(uart_num, "Temp config error\r\n", 19);
106 void temp_display(int uart_num)
109 int deci_degrees = 0;
112 tmp101_sensor_start_conversion(&tmp101_sensor);
113 msleep(50); /* Wait for the end of the conversion : 40ms */
114 len = tmp101_sensor_read(&tmp101_sensor, &raw, &deci_degrees);
116 serial_write(uart_num, "Temp read error\r\n", 19);
118 uprintf(uart_num, "Temp read: %d,%d - raw: 0x%04x.\r\n",
119 (deci_degrees/10), (deci_degrees%10), raw);
124 /***************************************************************************** */
127 system_set_default_power_state();
128 clock_config(SELECTED_FREQ);
129 set_pins(common_pins);
131 /* System tick timer MUST be configured and running in order to use the sleeping
133 systick_timer_on(1); /* 1ms */
137 /* Define our fault handler. This one is not mandatory, the dummy fault handler
138 * will be used when it's not overridden here.
139 * Note : The default one does a simple infinite loop. If the watchdog is deactivated
140 * the system will hang.
142 void fault_info(const char* name, uint32_t len)
144 uprintf(UART0, name);
150 /* This mode reads values from ADC2, ADC9 and ADC10 every 150ms and uses the values to set the leds.
151 * Pixels are updated when all pixel are set from ADC input.
153 void set_led_color(void)
155 uint16_t red = 0, green = 0, blue = 0;
156 static uint16_t tmp = 0;
159 adc_get_value(&tmp, LPC_ADC_NUM(9));
161 adc_get_value(&tmp, LPC_ADC_NUM(10));
165 ws2812_set_pixel(0, (((red >> 6) & 0xFF) - 30), green, (((blue >> 6) & 0xFF) - 30));
166 ws2812_send_frame(0);
168 uprintf(UART0, "ADC9: %d, ADC10: %d\n", red, blue);
172 uint8_t message_request = 0;
174 void trigger_message(uint32_t int_num) {
175 message_request = int_num + 1;
178 const struct lpc_timer_pwm_config pwm_conf = {
181 .outputs_initial_state = 0x07,
182 .match_values = { 15, 10, 15, },
183 .outputs = { 0, 1, 2, },
186 /***************************************************************************** */
190 uart_on(UART0, 115200, NULL);
192 uprintf(UART0, "Sarting\n");
194 i2c_on(I2C0, I2C_CLK_100KHz, I2C_MASTER);
196 uprintf(UART0, "I2C config done\n");
198 /* ADC for potentiometer color settings */
199 config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0);
200 config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0);
202 adc_start_burst_conversion(LPC_ADC_CHANNEL(2) | LPC_ADC_CHANNEL(9) | LPC_ADC_CHANNEL(10), LPC_ADC_SEQA);
203 // adc_start_burst_conversion(LPC_ADC_CHANNEL(9) | LPC_ADC_CHANNEL(10), LPC_ADC_SEQA);
204 uprintf(UART0, "ADC config done\n");
206 /* Led strip configuration */
207 ws2812_config(&ws2812_data_out_pin);
208 uprintf(UART0, "Led config done\n");
210 /* Activate the message request on Rising edge (button release) */
211 set_gpio_callback(trigger_message, &button, EDGE_RISING);
212 uprintf(UART0, "Button config done\n");
215 timer_on(LPC_SCT, (10 * 1000 * 1000), NULL);
216 timer_pwm_config(LPC_SCT, &pwm_conf);
217 uprintf(UART0, "Timer config done\n");
221 msleep(250); /* Wait for the end of the conversion : 40ms */
223 if (message_request != 0) {
224 uprintf(UART0, "Button pressed: %d\n", message_request);
227 /* Configure timer out and ADC in */
228 set_pins(capacitance_pins);
229 timer_start(LPC_SCT);
230 uprintf(UART0, "Timer started\n");
234 /* Turn timer output and ADC input as inputs to discharge all the capacitances. */
235 config_gpio(&cap_trig_gpio, 0, GPIO_DIR_OUT, 0);
236 config_gpio(&cap_adc_gpio, 0, GPIO_DIR_OUT, 0);
237 uprintf(UART0, "Timer stoped\n");