source: roaraudio/libroarlight/pwm.c @ 1977:3002ea118038

Last change on this file since 1977:3002ea118038 was 1977:3002ea118038, checked in by phi, 15 years ago

negative logic!

File size: 2.8 KB
Line 
1//pwm.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "libroarlight.h"
26
27uint16_t _g_roar_lpwm16[] = {
28/*
29    0x0000, 0x0001, 0x0101, 0x0111,  0x1111, 0x1115, 0x1515, 0x1555,
30    0x5555, 0x5557, 0x5757, 0x5777,  0x7777, 0x777F, 0x7F7F, 0x7FFF,
31    0xFFFF
32*/
33    0xFFFF, 0x7F7F, 0x777F, 0x7777,  0x5777, 0x5757, 0x5557, 0x5555,
34    0x1555, 0x1515, 0x1115, 0x1111,  0x0111, 0x0101, 0x0001, 0x0000
35                            };
36
37int roar_light_pwm_new (struct roar_lpwm_state * state, int bits ) {
38 if ( state == NULL )
39  return -1;
40
41 if ( bits < 1 || bits > 32 )
42  return -1;
43
44 state->bits = bits;
45
46 return roar_light_pwm_set(state, 0);
47}
48
49int roar_light_pwm_set (struct roar_lpwm_state * state, int value) {
50 if ( state == NULL )
51  return -1;
52
53 if ( value < 0 || value > state->bits )
54  return -1;
55
56 state->value = value;
57
58 return 0;
59}
60
61int roar_light_pwm_send(struct roar_lpwm_state * state, struct roar_vio_calls * vio, size_t len) {
62 char          * buf;
63 int16_t       * buf16;
64 size_t          todo = len;
65 uint64_t        s;
66
67 ROAR_DBG("roar_light_pwm_send(state=%p, vio=%p, len=%u) = ?", state, vio, len);
68
69 if ( state == NULL )
70  return -1;
71
72 if ( vio == NULL )
73  return -1;
74
75 if ( state->bits != 16 )
76  return -1;
77
78 if ( len == 0 )
79  return 0;
80
81 if ( (buf = malloc(len)) == NULL )
82  return -1;
83
84 buf16 = (int16_t *) buf;
85
86 while (todo > 1) {
87  ROAR_DBG("roar_light_pwm_send(*): loop: todo=%u, fill=%i", todo, state->fill);
88
89  if ( state->fill < 16 ) {
90   s             = _g_roar_lpwm16[state->value];
91   s           <<= state->fill;
92   state->s     |= s;
93   state->fill  += 16;
94  }
95
96  *buf16 = state->s & 0xFFFF;
97  state->s    >>= 16;
98  state->fill  -= 16;
99
100  buf16++;
101  todo -= 2;
102 }
103
104 if ( todo ) {
105  if ( state->fill < 8 ) {
106   s             = _g_roar_lpwm16[state->value];
107   s           <<= state->fill;
108   state->s     |= s;
109   state->fill  += 16;
110  }
111
112  buf[len-1]    = state->s & 0xFF;
113  state->s    >>= 8;
114  state->fill  -= 8;
115 }
116
117 if ( roar_vio_write(vio, buf, len) != len ) {
118  free(buf);
119  return -1;
120 }
121
122 free(buf);
123
124 return 0;
125}
126
127//ll
Note: See TracBrowser for help on using the repository browser.