source: roaraudio/libroarlight/pwm.c @ 1973:4e6945223979

Last change on this file since 1973:4e6945223979 was 1973:4e6945223979, checked in by phi, 15 years ago

fixed some small bugs

File size: 2.6 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    0x0000, 0x0001, 0x0101, 0x0111,  0x1111, 0x1115, 0x1515, 0x1555,
29    0x5555, 0x5557, 0x5757, 0x5777,  0x7777, 0x777F, 0x7F7F, 0x7FFF,
30    0xFFFF
31                            };
32
33int roar_light_pwm_new (struct roar_lpwm_state * state, int bits ) {
34 if ( state == NULL )
35  return -1;
36
37 if ( bits < 1 || bits > 32 )
38  return -1;
39
40 state->bits = bits;
41
42 return roar_light_pwm_set(state, 0);
43}
44
45int roar_light_pwm_set (struct roar_lpwm_state * state, int value) {
46 if ( state == NULL )
47  return -1;
48
49 if ( value < 0 || value > state->bits )
50  return -1;
51
52 state->value = value;
53
54 return 0;
55}
56
57int roar_light_pwm_send(struct roar_lpwm_state * state, struct roar_vio_calls * vio, size_t len) {
58 char          * buf;
59 int16_t       * buf16;
60 size_t          todo = len;
61 uint64_t        s;
62
63 ROAR_DBG("roar_light_pwm_send(state=%p, vio=%p, len=%u) = ?", state, vio, len);
64
65 if ( state == NULL )
66  return -1;
67
68 if ( vio == NULL )
69  return -1;
70
71 if ( state->bits != 16 )
72  return -1;
73
74 if ( len == 0 )
75  return 0;
76
77 if ( (buf = malloc(len)) == NULL )
78  return -1;
79
80 buf16 = (int16_t *) buf;
81
82 while (todo > 1) {
83  ROAR_DBG("roar_light_pwm_send(*): loop: todo=%u, fill=%i", todo, state->fill);
84
85  if ( state->fill < 16 ) {
86   s             = _g_roar_lpwm16[state->value];
87   s           <<= state->fill;
88   state->s     |= s;
89   state->fill  += 16;
90  }
91
92  *buf16 = state->s & 0xFFFF;
93  state->s    >>= 16;
94  state->fill  -= 16;
95
96  buf16++;
97  todo -= 2;
98 }
99
100 if ( todo ) {
101  buf[len-1]    = state->s & 0xFF;
102  state->s    >>= 8;
103  state->fill  -= 8;
104 }
105
106 if ( roar_vio_basic_write(vio, buf, len) != len ) {
107  free(buf);
108  return -1;
109 }
110
111 free(buf);
112
113 return 0;
114}
115
116//ll
Note: See TracBrowser for help on using the repository browser.