OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [example/] [demo_neopixel/] [main.c] - Blame information for rev 66

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 52 zero_gravi
// #################################################################################################
2
// # << NEORV32 - Smart LED (NeoPixel/WS2812) Hardware Interface (NEOLED) Demo Program >>          #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6
// # Copyright (c) 2021, Stephan Nolting. All rights reserved.                                     #
7
// #                                                                                               #
8
// # Redistribution and use in source and binary forms, with or without modification, are          #
9
// # permitted provided that the following conditions are met:                                     #
10
// #                                                                                               #
11
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
// #    conditions and the following disclaimer.                                                   #
13
// #                                                                                               #
14
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
// #    conditions and the following disclaimer in the documentation and/or other materials        #
16
// #    provided with the distribution.                                                            #
17
// #                                                                                               #
18
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
// #    endorse or promote products derived from this software without specific prior written      #
20
// #    permission.                                                                                #
21
// #                                                                                               #
22
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
// # ********************************************************************************************* #
32
// # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
// #################################################################################################
34
 
35
 
36
/**********************************************************************//**
37
 * @file demo_neopixel/main.c
38
 * @author Stephan Nolting
39
 * @brief NeoPixel (WS2812) interface demo using the processor's smart LED interface (NEOLED).
40
 **************************************************************************/
41
 
42
#include <neorv32.h>
43
 
44
 
45
/**********************************************************************//**
46
 * @name User configuration
47
 **************************************************************************/
48
/**@{*/
49
/** UART BAUD rate */
50
#define BAUD_RATE 19200
51 62 zero_gravi
/** Number of RGB LEDs in stripe (24-bit data) */
52 52 zero_gravi
#define NUM_LEDS_24BIT (12)
53 62 zero_gravi
/** Max intensity (0..255) */
54
#define MAX_INTENSITY (16)
55 52 zero_gravi
/**@}*/
56
 
57
 
58 62 zero_gravi
// prototypes
59
uint32_t hsv2rgb(int h, int v);
60
 
61
 
62 52 zero_gravi
/**********************************************************************//**
63
 * Main function
64 62 zero_gravi
 * This demo uses a 12-LED RGB ring
65 52 zero_gravi
 *
66
 * @note This program requires the NEOLED controller to be synthesized (UART0 is optional).
67
 *
68 60 zero_gravi
 * @return 0 if execution was successful
69 52 zero_gravi
 **************************************************************************/
70
int main() {
71
 
72
  // capture all exceptions and give debug info via UART0
73
  // this is not required, but keeps us safe
74
  neorv32_rte_setup();
75
 
76
 
77 62 zero_gravi
  // setup UART0 at default baud rate, no parity bits, no hw flow control
78 65 zero_gravi
  neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
79 52 zero_gravi
 
80
 
81
  // check if NEOLED unit is implemented at all, abort if not
82
  if (neorv32_neoled_available() == 0) {
83 65 zero_gravi
    neorv32_uart0_printf("Error! No NEOLED unit synthesized!\n");
84 60 zero_gravi
    return 1;
85 52 zero_gravi
  }
86
 
87
 
88 62 zero_gravi
  // illustrate setup
89
  neorv32_uart0_printf("<<< NEORV32 NeoPixel (WS2812) hardware interface (NEOLED) demo >>>\n"
90
                       "(TM) 'NeoPixel' is a trademark of Adafruit Industries.\n\n"
91
                       "This demo uses the following LED setup:\n"
92
                       "NEORV32.neoled_o -> %u RGB-LEDs (24-bit)\n\n", (uint32_t)NUM_LEDS_24BIT);
93 52 zero_gravi
 
94
 
95 62 zero_gravi
  // use the "neorv32_neoled_setup_ws2812()" setup function here instead the raw "neorv32_neoled_setup()"
96 52 zero_gravi
  // neorv32_neoled_setup_ws2812() will configure all timing parameters according to the WS2812 specs. for the current processor clock speed
97 62 zero_gravi
  neorv32_neoled_setup_ws2812();
98
  neorv32_neoled_enable(); // enable module
99
  neorv32_neoled_set_mode(0); // mode = 0 = 24-bit
100 52 zero_gravi
 
101
 
102
  // check NEOLED configuration
103 62 zero_gravi
  neorv32_uart0_printf("Checking NEOLED configuration:\n"
104
                       " Hardware FIFO size: %u entries\n"
105 64 zero_gravi
                       " Control register:   0x%x\n\n", neorv32_neoled_get_buffer_size(), NEORV32_NEOLED.CTRL);
106 52 zero_gravi
 
107
 
108
  // clear all LEDs
109
  neorv32_uart0_printf("Clearing all LEDs...\n");
110
  int i;
111 62 zero_gravi
  for (i=0; i<NUM_LEDS_24BIT; i++) {
112
    neorv32_neoled_write_blocking(0);
113 52 zero_gravi
  }
114 62 zero_gravi
  neorv32_cpu_delay_ms(500);
115 52 zero_gravi
 
116
 
117 62 zero_gravi
  // a simple animation example: rotating rainbow
118
  // this example uses BLOCKING NEOLED functions that check the FIFO flags before writing new data
119
  // non-blocking functions should only be used when checking the FIFO flags (half-full) in advance (for example using the NEOLED interrupt)
120 52 zero_gravi
  neorv32_uart0_printf("Starting animation...\n");
121
 
122 62 zero_gravi
  int angle = 0, led_id = 0;
123 52 zero_gravi
  while (1) {
124 62 zero_gravi
    for (led_id=0; led_id<NUM_LEDS_24BIT; led_id++) {
125
      // give every LED a different color
126
      neorv32_neoled_write_blocking(hsv2rgb(angle + (360/NUM_LEDS_24BIT) * led_id, MAX_INTENSITY));
127 52 zero_gravi
    }
128 62 zero_gravi
    angle += 1; // rotation increment per frame
129 52 zero_gravi
 
130 62 zero_gravi
    neorv32_neoled_strobe_blocking(); // send strobe ("RESET") command
131
    neorv32_cpu_delay_ms(10); // delay between frames
132
  }
133 52 zero_gravi
 
134 62 zero_gravi
  return 0;
135
}
136 52 zero_gravi
 
137
 
138 62 zero_gravi
/**********************************************************************//**
139
 * Convert HSV color to RGB.
140
 *
141
 * @note Very simple version: using integer arithmetic and ignoring saturation (saturation is always MAX).
142
 *
143
 * @param[in] h Hue (color angle), 0..359
144
 * @param[in] v Value (intensity), 0..255
145
 * @return LSB-aligned 24-bit RGB data [G,R,B]
146
 **************************************************************************/
147
uint32_t hsv2rgb(int h, int v) {
148
 
149
  h = h % 360;
150
  int r, g, b;
151
  int i = h / 60;
152
  int difs = h % 60;
153
  int rgb_adj = (v * difs) / 60;
154
 
155
  switch (i) {
156
    case 0:
157
      r = v;
158
      g = 0 + rgb_adj;
159
      b = 0;
160
      break;
161
    case 1:
162
      r = v - rgb_adj;
163
      g = v;
164
      b = 0;
165
      break;
166
    case 2:
167
      r = 0;
168
      g = v;
169
      b = 0 + rgb_adj;
170
      break;
171
    case 3:
172
      r = 0;
173
      g = v - rgb_adj;
174
      b = v;
175
      break;
176
    case 4:
177
      r = 0 + rgb_adj;
178
      g = 0;
179
      b = v;
180
      break;
181
    default:
182
      r = v;
183
      g = 0;
184
      b = v - rgb_adj;
185
      break;
186 52 zero_gravi
  }
187
 
188 62 zero_gravi
  uint32_t res = 0;
189
  res |= (((uint32_t)g) & 0xff) << 16;
190
  res |= (((uint32_t)r) & 0xff) << 8;
191
  res |= (((uint32_t)b) & 0xff) << 0;
192
 
193
  return res;
194 52 zero_gravi
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.