Statistics
| Branch: | Revision:

ddr4s / fw / wiringPi / examples / scrollPhat / scphat.c @ 32:cadb9025f1e0

History | View | Annotate | Download (6.13 KB)

1
/*
2
 * scphat.c:
3
 *        Little program to allow use of the Pimoroni Sctoll Phat
4
 *        from the command-line.
5
 *
6
 * Copyright (c) 2015-2016 Gordon Henderson. <projects@drogon.net>
7
 ***********************************************************************
8
 * This file is part of wiringPi:
9
 *        https://projects.drogon.net/raspberry-pi/wiringpi/
10
 *
11
 *    wiringPi is free software: you can redistribute it and/or modify
12
 *    it under the terms of the GNU Lesser General Public License as published by
13
 *    the Free Software Foundation, either version 3 of the License, or
14
 *    (at your option) any later version.
15
 *
16
 *    wiringPi is distributed in the hope that it will be useful,
17
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 *    GNU Lesser General Public License for more details.
20
 *
21
 *    You should have received a copy of the GNU Lesser General Public License
22
 *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
23
 ***********************************************************************
24
 */
25

    
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <errno.h>
29
#include <string.h>
30

    
31
#include <wiringPi.h>
32
#include <scrollPhat.h>
33

    
34
static char *progName ;
35

    
36

    
37
/*
38
 * checkArgs:
39
 *        Count the arguments for each little function
40
 *********************************************************************************
41
 */
42

    
43
static void checkArgs (char *command, int num, int arg, int argc)
44
{
45
  if ((arg + num) < argc)
46
    return ;
47

    
48
  fprintf (stderr, "%s: Not enough data for %s command.\n", progName, command) ;
49
  exit (EXIT_FAILURE) ;
50
}
51

    
52

    
53
/*
54
 * doClear:
55
 *        Clear the display
56
 *********************************************************************************
57
 */
58

    
59
static int doClear (void)
60
{
61
  scrollPhatClear () ;
62
  return 1 ;
63
}
64

    
65

    
66
/*
67
 * doBright
68
 *********************************************************************************
69
 */
70

    
71
static int doBright (int arg, int argc, char *argv [])
72
{
73
  checkArgs ("bright", 1, arg, argc) ;
74
  scrollPhatIntensity (atoi (argv [arg+1])) ;
75
  return 2 ;
76
}
77

    
78

    
79

    
80
/*
81
 * doPlot
82
 *********************************************************************************
83
 */
84

    
85
static int doPlot (int arg, int argc, char *argv [])
86
{
87
  checkArgs ("plot", 2, arg, argc) ;
88
  scrollPhatPoint (atoi (argv [arg+1]), atoi (argv [arg+2]), 1) ;
89
  scrollPhatUpdate () ;
90
  return 3 ;
91
}
92

    
93

    
94
/*
95
 * doLine
96
 *********************************************************************************
97
 */
98

    
99
static int doLine (int arg, int argc, char *argv [])
100
{
101
  checkArgs ("line", 4, arg, argc) ;
102
  scrollPhatLine (atoi (argv [arg+1]), atoi (argv [arg+2]),
103
                          atoi (argv [arg+3]), atoi (argv [arg+4]), 1) ;
104
  scrollPhatUpdate () ;
105
  return 5 ;
106
}
107

    
108

    
109
/*
110
 * doLineTo
111
 *********************************************************************************
112
 */
113

    
114
static int doLineTo (int arg, int argc, char *argv [])
115
{
116
  checkArgs ("lineto", 2, arg, argc) ;
117
  scrollPhatLineTo (atoi (argv [arg+1]), atoi (argv [arg+2]), 1) ;
118
  scrollPhatUpdate () ;
119
  return 3 ;
120
}
121

    
122

    
123
/*
124
 * doWait
125
 *********************************************************************************
126
 */
127

    
128
static int doWait (int arg, int argc, char *argv [])
129
{
130
  checkArgs ("wait", 1, arg, argc) ;
131
  delay (atoi (argv [arg+1]) * 100) ;
132
  scrollPhatUpdate () ;
133
  return 2 ;
134
}
135

    
136

    
137
/*
138
 * doSpeed
139
 *********************************************************************************
140
 */
141

    
142
static int doSpeed (int arg, int argc, char *argv [])
143
{
144
  checkArgs ("speed", 1, arg, argc) ;
145
  scrollPhatPrintSpeed (atoi (argv [arg+1])) ;
146
  return 2 ;
147
}
148

    
149

    
150
/*
151
 * doScroll
152
 *********************************************************************************
153
 */
154

    
155
static int doScroll (int arg, int argc, char *argv [])
156
{
157
  checkArgs ("scroll", 1, arg, argc) ;
158
  scrollPhatPuts (argv [arg+1]) ;
159
  return 2 ;
160
}
161

    
162

    
163
static void failUsage (void)
164
{
165
  fprintf (stderr, "Usage: %s command [paremters] ...\n", progName) ;
166
  fprintf (stderr, "  commands:\n") ;
167
  fprintf (stderr, "    clear/cls        - Clear the display\n") ;
168
  fprintf (stderr, "    bright N         - Set display brightness; 1-100\n") ;
169
  fprintf (stderr, "    plot X Y         - Set a single pixel at location X Y; 0-10, 0-4\n") ;
170
  fprintf (stderr, "    line X1 Y1 X2 Y2 - Draw a line from the 2 points\n") ;
171
  fprintf (stderr, "    lineto X2 Y2     - Draw a line from the last point to the new one\n") ;
172
  fprintf (stderr, "    wait/delay N     - Wait for N 10ths seconds\n") ;
173
  fprintf (stderr, "    speed N          - Set scrolling speed (cps)\n") ;
174
  fprintf (stderr, "    scroll S         - Scroll the given string\n") ;
175
  fprintf (stderr, "\n") ;
176
  fprintf (stderr, "  Example: %s plot 0 0 wait 50 scroll \"  Hello  \"\n", progName) ;
177
  exit (EXIT_FAILURE) ;
178
}
179

    
180

    
181
/*
182
 * the works
183
 *********************************************************************************
184
 */
185

    
186
int main (int argc, char *argv [])
187
{
188
  int arg = 1 ;
189
  char *command ;
190

    
191
  progName = argv [0] ;
192

    
193
  wiringPiSetupSys () ;
194

    
195
  if (scrollPhatSetup () != 0)
196
  {
197
    fprintf (stderr, "%s: Unable to initialise the scrollPhat: %s\n", progName, strerror (errno)) ;
198
    exit (EXIT_FAILURE) ;
199
  }
200

    
201
  progName = argv [0] ;
202

    
203
  if (argc < 2)
204
  {
205
    fprintf (stderr, "%s: Nothing to do...\n", argv [0]) ;
206
    failUsage () ;
207
  }
208

    
209
  while (arg != argc)
210
  {
211
    command = argv [arg] ;
212
    /**/ if (strcasecmp (command, "clear")  == 0) arg += doClear  () ;
213
    else if (strcasecmp (command, "cls")    == 0) arg += doClear  () ;
214
    else if (strcasecmp (command, "bright") == 0) arg += doBright (arg, argc, argv) ;
215
    else if (strcasecmp (command, "plot")   == 0) arg += doPlot   (arg, argc, argv) ;
216
    else if (strcasecmp (command, "line")   == 0) arg += doLine   (arg, argc, argv) ;
217
    else if (strcasecmp (command, "lineto") == 0) arg += doLineTo (arg, argc, argv) ;
218
    else if (strcasecmp (command, "wait")   == 0) arg += doWait   (arg, argc, argv) ;
219
    else if (strcasecmp (command, "delay")  == 0) arg += doWait   (arg, argc, argv) ;
220
    else if (strcasecmp (command, "speed")  == 0) arg += doSpeed  (arg, argc, argv) ;
221
    else if (strcasecmp (command, "scroll") == 0) arg += doScroll (arg, argc, argv) ;
222
    else
223
    {
224
      fprintf (stderr, "%s: Unknown command: %s.\n", argv [0], argv [arg]) ;
225
      failUsage () ;
226
    }
227
  }
228

    
229
  return 0 ;
230
}