source: roaraudio/libroarlight/pwm.c @ 1972:68110cc2b9c0

Last change on this file since 1972:68110cc2b9c0 was 1972:68110cc2b9c0, checked in by phi, 15 years ago

wrote basic code

File size: 2.3 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 if ( state == NULL )
64  return -1;
65
66 if ( vio == NULL )
67  return -1;
68
69 if ( state->bits != 16 )
70  return -1;
71
72 if ( (buf = malloc(len)) == NULL )
73  return -1;
74
75 buf16 = (int16_t *) buf;
76
77 while (todo > 1) {
78  if ( state->fill < 16 ) {
79   s          = _g_roar_lpwm16[state->value];
80   s        <<= state->fill;
81   state->s  |= s;
82  }
83
84  *buf16 = state->s & 0xFFFF;
85  state->s    >>= 16;
86  state->fill  -= 16;
87
88  buf16++;
89  todo -= 2;
90 }
91
92 if ( todo ) {
93  buf[len-1]    = state->s & 0xFF;
94  state->s    >>= 8;
95  state->fill  -= 8;
96 }
97
98 if ( roar_vio_write(vio, buf, len) != len ) {
99  free(buf);
100  return -1;
101 }
102
103 free(buf);
104
105 return 0;
106}
107
108//ll
Note: See TracBrowser for help on using the repository browser.