file: slh_demo.c
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <math.h>
#include <errno.h>
#include "slh.h"
#define PUMP_FILE "./SLHDMX16.BIN"
unsigned char buffer[NUM_DMX_CHANS];
void print_dmx_channels();
int
main( )
{
int fd;
int retval;
struct stat stat;
void *pump_buf;
DMX_CHANNELS dmx_channels;
/* open the pump file */
fd = open( PUMP_FILE, O_RDONLY );
if ( fd < 0 ) {
fprintf( stderr, "cannot open pump file (%d)\n", errno );
exit ( -10 );
}
/* get the pump file stats to determine the file size */
retval = fstat( fd, &stat );
if ( retval < 0 ) {
fprintf( stderr, "cannot get pump file stats (%d)\n", errno );
exit ( -20 );
}
fprintf( stderr, "Pumping Board; size = %d\n", (int)stat.st_size );
/* allocate buffer to hold pump file */
pump_buf = malloc( (size_t)stat.st_size + sizeof(long) );
if ( pump_buf == NULL ) {
fprintf( stderr, "unable to malloc() pump buffer\n" );
exit ( -30 );
}
/* read in pump file to buffer */
retval = read( fd, pump_buf+sizeof(long), (size_t)stat.st_size );
if ( retval < 0 ) {
fprintf( stderr, "cannot read pump file (%d)\n", errno );
exit ( -40 );
}
/* copy the pump file length into the first word of the buffer */
*((unsigned long *)pump_buf) = stat.st_size;
/* open soundlight 1512b device */
fd = open( "/dev/slh", O_RDWR );
if ( fd < 0 ) {
fprintf( stderr, "open( /dev/slh ) failed (%d)\n", errno );
exit ( -50 );
}
/* pump the board */
retval = ioctl( fd, SLH_IOCPUMP, (char*)pump_buf );
if ( retval < 0 ) {
fprintf( stderr, "ioctl( pump ) failed (%d)\n", errno );
exit ( -60 );
}
/* free pump file buffer */
free( pump_buf );
/* print non-zero DMX-512 channels */
print_dmx_channels( fd );
/* set board parameters here ??? */
/* zero channel structure */
memset( (void *)&dmx_channels, 0, sizeof(dmx_channels) );
/* set channel values */
dmx_channels.num_chan = 2;
/* change dimmer 6 value to 125 immediately */
dmx_channels.chan_value[0].channel = 6;
dmx_channels.chan_value[0].new_value = 125;
dmx_channels.chan_value[0].transition = 0;
/* change dimmer 509 value to 82 taking 5 seconds */
dmx_channels.chan_value[1].channel = 509;
dmx_channels.chan_value[1].new_value = 82;
dmx_channels.chan_value[1].transition = 5;
retval = ioctl( fd, SLH_IOCSET, (char*)&dmx_channels );
if ( retval < 0 ) {
fprintf( stderr, "ioctl( set ) failed (%d)\n", errno );
}
print_dmx_channels( fd );
exit ( 0 );
}
void
print_dmx_channels( int fd )
{
register int i;
int retval;
int found;
fprintf( stderr, "printing non-zero channels:\n" );
/* This sleep is not necessary. But, if channel values are set
* [using ioctl()] and print_dmx_channels() is called immediately
* afterwards, the change might not be seen since it is not actually
* made until the next channel timer expiration which ocurs every
* 50ms.
*/
usleep( 50000 );
/* read in DMX-512 channel values */
retval = read( fd, buffer, NUM_DMX_CHANS );
if ( retval < 0 ) {
fprintf( stderr, "read( chans ) failed (%d)\n", errno );
return;
}
if ( retval != NUM_DMX_CHANS ) {
fprintf( stderr, "read( chans ): num_chans=%d\n", retval );
}
/* print non-zero channels */
for ( found = 0, i = 0; i < 512; i++ ) {
if ( buffer[i] == 0 ) {
continue;
}
fprintf( stderr, "\tchan(%d)=%d\n", i+1, (int)buffer[i] );
found++;
}
if ( !found ) {
fprintf( stderr, "\tNo non-zero channels\n" );
}
}
C++ to HTML Conversion by ctoohtml