1 /****************************************************************************
3 * Copyright (c) 2006 Dave Hylands <dhylands@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
14 ****************************************************************************/
21 * This implements the usermode program for talking to the robostix.
23 *****************************************************************************/
25 /* ---- Include Files ---------------------------------------------------- */
34 /* ---- Public Variables ------------------------------------------------- */
40 /* ---- Private Constants and Types -------------------------------------- */
41 /* ---- Private Variables ------------------------------------------------ */
48 int (*parseArgs)( int argc, char **argv );
49 void (*execFunc)( int cmd );
54 int ParseNone( int argc, char **argv );
55 int ParseOnOffArg( int argc, char **argv );
56 int ParseOnOffPulseArg( int argc, char **argv );
59 void Power( int cmd );
60 void ReadIOctl( int cmd );
61 void SimpleIOctl( int cmd );
65 { "power", "on|off", "Controls the robostix voltage regulators", ParseOnOffArg, Power, -1 },
66 { "reset", "on|off|pulse", "Resets the robostix", ParseOnOffPulseArg, SimpleIOctl, ROBOSTIX_IOCTL_RESET },
67 { "245", "on|off", "Controls the 245 buffer chip", ParseOnOffArg, SimpleIOctl, ROBOSTIX_IOCTL_245_ENABLE },
69 { "sck", "on|off", "Sets/clears the SCK line", ParseOnOffArg, SimpleIOctl, ROBOSTIX_IOCTL_SET_SCK },
70 { "ss", "on|off", "Sets/clears the SS line", ParseOnOffArg, SimpleIOctl, ROBOSTIX_IOCTL_SET_SS },
71 { "txd", "on|off", "Sets/clears the IR-TXD line", ParseOnOffArg, SimpleIOctl, ROBOSTIX_IOCTL_SET_IR_TXD },
72 { "mosi", "on|off", "Sets/clears the MOSI line", ParseOnOffArg, SimpleIOctl, ROBOSTIX_IOCTL_SET_MOSI },
74 { "rxd", "", "Reads the IR-RXD line", ParseNone, ReadIOctl, ROBOSTIX_IOCTL_GET_IR_RXD },
75 { "irq", "", "Reads the TM-IRQ line", ParseNone, ReadIOctl, ROBOSTIX_IOCTL_GET_IRQ },
76 { "miso", "", "Reads the MISO line", ParseNone, ReadIOctl, ROBOSTIX_IOCTL_GET_MISO },
81 /* ---- Private Function Prototypes -------------------------------------- */
82 /* ---- Functions -------------------------------------------------------- */
85 /****************************************************************************
89 ***************************************************************************/
91 int main( int argc, char **argv )
103 if (( gFd = open( "/dev/robostix", O_RDWR )) < 0 )
105 perror( "Unable to open /dev/robostix" );
110 cmdStr = argv[ argIdx++ ];
112 for ( cmd = gCmd; cmd->cmdStr != NULL; cmd++ )
114 if ( strcasecmp( cmdStr, cmd->cmdStr ) == 0 )
119 if ( cmd->cmdStr == NULL )
121 fprintf( stderr, "Unrecognized command: '%s'\n", cmdStr );
125 if ( cmd->parseArgs( argc - argIdx, &argv[ argIdx ] ))
127 cmd->execFunc( cmd->cmd );
137 /****************************************************************************
139 * Checks to see if the argument is on/off (or equivalent)
141 ***************************************************************************/
143 int IsOnOffArg( int argc, char **argv )
145 if (( strcasecmp( *argv, "on" ) == 0 )
146 || ( strcasecmp( *argv, "t" ) == 0 )
147 || ( strcasecmp( *argv, "1" ) == 0 ))
153 if (( strcasecmp( *argv, "off" ) == 0 )
154 || ( strcasecmp( *argv, "f" ) == 0 )
155 || ( strcasecmp( *argv, "0" ) == 0 ))
165 /****************************************************************************
167 * Parses no arguments
169 ***************************************************************************/
171 int ParseNone( int argc, char **argv )
177 /****************************************************************************
179 * Parses a command line argument for legel on/off values
181 ***************************************************************************/
183 int ParseOnOffArg( int argc, char **argv )
185 if ( IsOnOffArg( argc, argv ))
190 fprintf( stderr, "Expecting on/off, found: '%s'\n", *argv );
195 /****************************************************************************
197 * Parses a command line argument for legel on/off values
199 ***************************************************************************/
201 int ParseOnOffPulseArg( int argc, char **argv )
203 if (( strcasecmp( *argv, "pulse" ) == 0 )
204 || ( strcasecmp( *argv, "2" ) == 0 ))
210 if ( IsOnOffArg( argc, argv ))
215 fprintf( stderr, "Expecting on/off/pulse, found: '%s'\n", *argv );
218 } // ParseOnoffPulseArg
220 /****************************************************************************
224 ***************************************************************************/
226 void Power( int cmd )
228 SimpleIOctl( ROBOSTIX_IOCTL_POWER_VCC5 );
232 /****************************************************************************
236 ***************************************************************************/
238 void SimpleIOctl( int cmd )
240 if ( ioctl( gFd, cmd, gVal ) != 0 )
242 fprintf( stderr, "ioctl call failed: %d\n", errno );
247 /****************************************************************************
251 ***************************************************************************/
253 void ReadIOctl( int cmd )
255 if ( ioctl( gFd, cmd, &gVal ) != 0 )
257 fprintf( stderr, "ioctl call failed: %d\n", errno );
262 /****************************************************************************
266 ***************************************************************************/
272 for ( cmd = gCmd; cmd->cmdStr != NULL; cmd++ )
274 printf( "%-12s %-12s %s\n", cmd->cmdStr, cmd->argStr, cmd->helpStr );