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