[1974] | 1 | //driver_pwmled.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
| 5 | * |
---|
| 6 | * This file is part of roard 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 | * RoarAudio 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. |
---|
[1974] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "roard.h" |
---|
| 27 | |
---|
[3602] | 28 | #if !defined(ROAR_WITHOUT_DCOMP_PWMLED) && !defined(ROAR_WITHOUT_VIO_DSTR) |
---|
[2502] | 29 | |
---|
[2367] | 30 | int driver_pwmled_open_vio (struct roar_vio_calls * inst, char * device, struct roar_audio_info * info, int fh, struct roar_stream_server * sstream) { |
---|
[1978] | 31 | struct roar_vio_defaults def; |
---|
[1980] | 32 | struct driver_pwmled * self = malloc(sizeof(struct driver_pwmled)); |
---|
[1978] | 33 | |
---|
[1980] | 34 | if ( self == NULL ) |
---|
[1978] | 35 | return -1; |
---|
[1974] | 36 | |
---|
| 37 | if ( fh == -1 ) { |
---|
| 38 | if ( device == NULL ) |
---|
| 39 | device = "/dev/ttyS0"; |
---|
| 40 | |
---|
[1980] | 41 | if ( roar_vio_dstr_init_defaults(&def, ROAR_VIO_DEF_TYPE_NONE, O_WRONLY, 0644) == -1 ) { |
---|
| 42 | free(self); |
---|
[1974] | 43 | return -1; |
---|
[1978] | 44 | } |
---|
| 45 | |
---|
[1980] | 46 | if ( roar_vio_open_dstr(&(self->vio), device, &def, 1) == -1 ) { |
---|
| 47 | free(self); |
---|
| 48 | return -1; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | inst->inst = self; |
---|
[1974] | 52 | } else { |
---|
[1978] | 53 | /* |
---|
[1974] | 54 | if ( roar_vio_open_fh(inst, fh) == -1 ) |
---|
| 55 | return -1; |
---|
[1978] | 56 | */ |
---|
[1980] | 57 | free(self); |
---|
[1978] | 58 | return -1; |
---|
[1974] | 59 | } |
---|
| 60 | |
---|
[1978] | 61 | inst->read = NULL; |
---|
| 62 | inst->write = driver_pwmled_write; |
---|
| 63 | inst->lseek = NULL; |
---|
[1974] | 64 | inst->nonblock = NULL; |
---|
[1978] | 65 | inst->sync = NULL; |
---|
| 66 | inst->ctl = driver_pwmled_ctl; |
---|
| 67 | inst->close = driver_pwmled_close; |
---|
[1974] | 68 | |
---|
| 69 | info->codec = ROAR_CODEC_DMX512; |
---|
| 70 | |
---|
[1984] | 71 | if ( info->rate == g_sa->rate ) { |
---|
| 72 | self->rate = 9600; |
---|
| 73 | } else { |
---|
| 74 | self->rate = info->rate; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | self->channel = 0; |
---|
| 78 | |
---|
[1981] | 79 | if ( roar_light_pwm_new(&(self->state), 16) == -1 ) { |
---|
| 80 | free(self); |
---|
| 81 | return -1; |
---|
| 82 | } |
---|
| 83 | |
---|
[2368] | 84 | if ( sstream != NULL ) |
---|
| 85 | driver_pwmled_ctl(inst, ROAR_VIO_CTL_SET_SSTREAM, sstream); |
---|
| 86 | |
---|
[1974] | 87 | return 0; |
---|
| 88 | } |
---|
| 89 | |
---|
[1978] | 90 | int driver_pwmled_close (struct roar_vio_calls * vio) { |
---|
[1980] | 91 | int ret = roar_vio_close(&(((struct driver_pwmled*)(vio->inst))->vio)); |
---|
[1978] | 92 | |
---|
| 93 | if ( vio->inst != NULL ) |
---|
| 94 | free(vio->inst); |
---|
| 95 | |
---|
| 96 | return ret; |
---|
| 97 | } |
---|
| 98 | |
---|
[1974] | 99 | // TODO: this function should be optimized. |
---|
| 100 | ssize_t driver_pwmled_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
[1980] | 101 | struct driver_pwmled * self = vio->inst; |
---|
[2034] | 102 | int value; |
---|
[1974] | 103 | |
---|
| 104 | if ( vio == NULL || buf == NULL ) |
---|
| 105 | return -1; |
---|
| 106 | |
---|
| 107 | if ( count != 512 ) |
---|
| 108 | return -1; |
---|
| 109 | |
---|
[2034] | 110 | value = ((unsigned char*)buf)[self->channel] / 15; |
---|
| 111 | |
---|
| 112 | if ( roar_light_pwm_set(&(self->state), value) == -1 ) |
---|
[1974] | 113 | return -1; |
---|
| 114 | |
---|
[2056] | 115 | // ROAR_WARN("driver_pwmled_write(*): value=%i", value); |
---|
| 116 | |
---|
[2034] | 117 | if ( value ) { |
---|
| 118 | // bit per word, bit per byte |
---|
| 119 | return roar_light_pwm_send(&(self->state), &(self->vio), self->rate/11/8/100) == 0 ? count : -1; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | return count; |
---|
[1974] | 123 | } |
---|
| 124 | |
---|
| 125 | int driver_pwmled_ctl(struct roar_vio_calls * vio, int cmd, void * data) { |
---|
[1990] | 126 | struct driver_pwmled * self = vio->inst; |
---|
[1974] | 127 | |
---|
| 128 | if ( vio == NULL ) |
---|
| 129 | return -1; |
---|
| 130 | |
---|
| 131 | switch (cmd) { |
---|
| 132 | case ROAR_VIO_CTL_SET_SSTREAM: |
---|
| 133 | ROAR_STREAM(data)->dir = ROAR_DIR_LIGHT_OUT; |
---|
| 134 | ROAR_STREAM_SERVER(data)->codec_orgi = ROAR_CODEC_DMX512; |
---|
| 135 | break; |
---|
[1987] | 136 | case ROAR_VIO_CTL_SET_DMXSCHAN: |
---|
[1988] | 137 | if ( *(uint16_t*)data > 511 ) |
---|
| 138 | return -1; |
---|
| 139 | |
---|
[1987] | 140 | self->channel = *(uint16_t*)data; |
---|
| 141 | break; |
---|
[1974] | 142 | default: |
---|
| 143 | return -1; |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | return 0; |
---|
| 147 | } |
---|
| 148 | |
---|
[2502] | 149 | #endif |
---|
| 150 | |
---|
[1974] | 151 | //ll |
---|