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