source: roaraudio/libroarlight/pwm.c @ 5381:430b1d26e12d

Last change on this file since 5381:430b1d26e12d was 5381:430b1d26e12d, checked in by phi, 12 years ago

updated copyright years

File size: 2.9 KB
Line 
1//pwm.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2012
5 *
6 *  This file is part of libroardsp a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroardsp 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.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "libroarlight.h"
27
28uint16_t _g_roar_lpwm16[] = {
29/*
30    0x0000, 0x0001, 0x0101, 0x0111,  0x1111, 0x1115, 0x1515, 0x1555,
31    0x5555, 0x5557, 0x5757, 0x5777,  0x7777, 0x777F, 0x7F7F, 0x7FFF,
32    0xFFFF
33*/
34    0xFFFF, 0x7FFF, 0x7F7F, 0x777F, 0x7777,  0x5777, 0x5757, 0x5557,
35    0x5555, 0x1555, 0x1515, 0x1115, 0x1111,  0x0111, 0x0101, 0x0001,
36    0x0000
37                            };
38
39int roar_light_pwm_new (struct roar_lpwm_state * state, int bits ) {
40 if ( state == NULL )
41  return -1;
42
43 if ( bits < 1 || bits > 32 )
44  return -1;
45
46 state->bits = bits;
47
48 return roar_light_pwm_set(state, 0);
49}
50
51int roar_light_pwm_set (struct roar_lpwm_state * state, int value) {
52 if ( state == NULL )
53  return -1;
54
55 if ( value < 0 || value > (state->bits+1) )
56  return -1;
57
58 state->value = value;
59
60 return 0;
61}
62
63int roar_light_pwm_send(struct roar_lpwm_state * state, struct roar_vio_calls * vio, size_t len) {
64 char          * buf;
65 int16_t       * buf16;
66 size_t          todo = len;
67 uint64_t        s;
68
69 ROAR_DBG("roar_light_pwm_send(state=%p, vio=%p, len=%u) = ?", state, vio, len);
70
71 if ( state == NULL )
72  return -1;
73
74 if ( vio == NULL )
75  return -1;
76
77 if ( state->bits != 16 )
78  return -1;
79
80 if ( len == 0 )
81  return 0;
82
83 if ( (buf = roar_mm_malloc(len)) == NULL )
84  return -1;
85
86 buf16 = (int16_t *) buf;
87
88 while (todo > 1) {
89  ROAR_DBG("roar_light_pwm_send(*): loop: todo=%u, fill=%i", todo, state->fill);
90
91  if ( state->fill < 16 ) {
92   s             = _g_roar_lpwm16[state->value];
93   s           <<= state->fill;
94   state->s     |= s;
95   state->fill  += 16;
96  }
97
98  *buf16 = state->s & 0xFFFF;
99  state->s    >>= 16;
100  state->fill  -= 16;
101
102  buf16++;
103  todo -= 2;
104 }
105
106 if ( todo ) {
107  if ( state->fill < 8 ) {
108   s             = _g_roar_lpwm16[state->value];
109   s           <<= state->fill;
110   state->s     |= s;
111   state->fill  += 16;
112  }
113
114  buf[len-1]    = state->s & 0xFF;
115  state->s    >>= 8;
116  state->fill  -= 8;
117 }
118
119 if ( roar_vio_write(vio, buf, len) != (ssize_t)len ) {
120  roar_mm_free(buf);
121  return -1;
122 }
123
124 roar_mm_free(buf);
125
126 return 0;
127}
128
129//ll
Note: See TracBrowser for help on using the repository browser.