[1970] | 1 | //pwm.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[3811] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2010 |
---|
[1970] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[1970] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "libroarlight.h" |
---|
| 27 | |
---|
[1972] | 28 | uint16_t _g_roar_lpwm16[] = { |
---|
[1977] | 29 | /* |
---|
[1972] | 30 | 0x0000, 0x0001, 0x0101, 0x0111, 0x1111, 0x1115, 0x1515, 0x1555, |
---|
| 31 | 0x5555, 0x5557, 0x5757, 0x5777, 0x7777, 0x777F, 0x7F7F, 0x7FFF, |
---|
| 32 | 0xFFFF |
---|
[1977] | 33 | */ |
---|
[2034] | 34 | 0xFFFF, 0x7FFF, 0x7F7F, 0x777F, 0x7777, 0x5777, 0x5757, 0x5557, |
---|
| 35 | 0x5555, 0x1555, 0x1515, 0x1115, 0x1111, 0x0111, 0x0101, 0x0001, |
---|
| 36 | 0x0000 |
---|
[1972] | 37 | }; |
---|
| 38 | |
---|
| 39 | int 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 | |
---|
| 51 | int roar_light_pwm_set (struct roar_lpwm_state * state, int value) { |
---|
| 52 | if ( state == NULL ) |
---|
| 53 | return -1; |
---|
| 54 | |
---|
[2034] | 55 | if ( value < 0 || value > (state->bits+1) ) |
---|
[1972] | 56 | return -1; |
---|
| 57 | |
---|
| 58 | state->value = value; |
---|
| 59 | |
---|
| 60 | return 0; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | int 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 | |
---|
[1973] | 69 | ROAR_DBG("roar_light_pwm_send(state=%p, vio=%p, len=%u) = ?", state, vio, len); |
---|
| 70 | |
---|
[1972] | 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 | |
---|
[1973] | 80 | if ( len == 0 ) |
---|
| 81 | return 0; |
---|
| 82 | |
---|
[3637] | 83 | if ( (buf = roar_mm_malloc(len)) == NULL ) |
---|
[1972] | 84 | return -1; |
---|
| 85 | |
---|
| 86 | buf16 = (int16_t *) buf; |
---|
| 87 | |
---|
| 88 | while (todo > 1) { |
---|
[1973] | 89 | ROAR_DBG("roar_light_pwm_send(*): loop: todo=%u, fill=%i", todo, state->fill); |
---|
| 90 | |
---|
[1972] | 91 | if ( state->fill < 16 ) { |
---|
[1973] | 92 | s = _g_roar_lpwm16[state->value]; |
---|
| 93 | s <<= state->fill; |
---|
| 94 | state->s |= s; |
---|
| 95 | state->fill += 16; |
---|
[1972] | 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 ) { |
---|
[1976] | 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 | |
---|
[1972] | 114 | buf[len-1] = state->s & 0xFF; |
---|
| 115 | state->s >>= 8; |
---|
| 116 | state->fill -= 8; |
---|
| 117 | } |
---|
| 118 | |
---|
[3636] | 119 | if ( roar_vio_write(vio, buf, len) != (ssize_t)len ) { |
---|
[3637] | 120 | roar_mm_free(buf); |
---|
[1972] | 121 | return -1; |
---|
| 122 | } |
---|
| 123 | |
---|
[3637] | 124 | roar_mm_free(buf); |
---|
[1972] | 125 | |
---|
| 126 | return 0; |
---|
| 127 | } |
---|
| 128 | |
---|
[1970] | 129 | //ll |
---|