Contiki-NG
Loading...
Searching...
No Matches
led-strip.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Zolertia
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30/*---------------------------------------------------------------------------*/
31/**
32 * \addtogroup zoul-led-strip
33 * @{
34 *
35 * Driver to control a bright LED strip powered at 3VDC, drawing power directly
36 * from the battery power supply. An example on how to adapt 12VDC LED strips
37 * to 3VDC is provided at http://www.hackster.io/zolertia
38 * @{
39 *
40 * \file
41 * Driver for a bright LED strip
42 */
43/*---------------------------------------------------------------------------*/
44#include "contiki.h"
45#include "dev/gpio.h"
46#include "led-strip.h"
47
48#include <stdint.h>
49/*---------------------------------------------------------------------------*/
50#ifndef LED_STRIP_PORT
51#define LED_STRIP_PORT GPIO_A_NUM
52#endif
53#ifndef LED_STRIP_PIN
54#define LED_STRIP_PIN 6
55#endif
56#define LED_STRIP_PORT_BASE GPIO_PORT_TO_BASE(LED_STRIP_PORT)
57#define LED_STRIP_PIN_MASK GPIO_PIN_MASK(LED_STRIP_PIN)
58/*---------------------------------------------------------------------------*/
59static uint8_t initialized = 0;
60/*---------------------------------------------------------------------------*/
61void
63{
64 /* Software controlled */
65 GPIO_SOFTWARE_CONTROL(LED_STRIP_PORT_BASE, LED_STRIP_PIN_MASK);
66 /* Set pin to output */
67 GPIO_SET_OUTPUT(LED_STRIP_PORT_BASE, LED_STRIP_PIN_MASK);
68 /* Set the pin to a default position */
69 GPIO_SET_PIN(LED_STRIP_PORT_BASE, LED_STRIP_PIN_MASK);
70
71 initialized = 1;
72}
73/*---------------------------------------------------------------------------*/
74int
75led_strip_switch(uint8_t val)
76{
77 if(!initialized) {
78 return LED_STRIP_ERROR;
79 }
80
81 if(val != LED_STRIP_ON && val != LED_STRIP_OFF) {
82 return LED_STRIP_ERROR;
83 }
84
85 /* Set the LED to ON or OFF */
86 GPIO_WRITE_PIN(LED_STRIP_PORT_BASE, LED_STRIP_PIN_MASK, val);
87
88 return val;
89}
90/*---------------------------------------------------------------------------*/
91int
93{
94 if(!initialized) {
95 return LED_STRIP_ERROR;
96 }
97
98 /* Inverse logic, return ON if the pin is low */
99 if(GPIO_READ_PIN(LED_STRIP_PORT_BASE, LED_STRIP_PIN_MASK)) {
100 return LED_STRIP_OFF;
101 }
102 return LED_STRIP_ON;
103}
104/*---------------------------------------------------------------------------*/
105
106/**
107 * @}
108 * @}
109 */
110
Header file with register and macro declarations for the cc2538 GPIO module.
#define GPIO_SOFTWARE_CONTROL(PORT_BASE, PIN_MASK)
Configure the pin to be software controlled with PIN_MASK of port with PORT_BASE.
Definition gpio.h:258
#define GPIO_SET_PIN(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE high.
Definition gpio.h:106
#define GPIO_WRITE_PIN(PORT_BASE, PIN_MASK, value)
Set pins with PIN_MASK of port with PORT_BASE to value.
Definition gpio.h:134
#define GPIO_SET_OUTPUT(PORT_BASE, PIN_MASK)
Set pins with PIN_MASK of port with PORT_BASE to output.
Definition gpio.h:85
#define GPIO_READ_PIN(PORT_BASE, PIN_MASK)
Read pins with PIN_MASK of port with PORT_BASE.
Definition gpio.h:147
int led_strip_switch(uint8_t val)
Function to turn ON/OFF the LED strip.
Definition led-strip.c:75
void led_strip_config(void)
Init function for the bright LED strip driver.
Definition led-strip.c:62
int led_strip_get(void)
Function to get the LED strip current state.
Definition led-strip.c:92
Header file for a bright LED strip driver.