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