File: digipanel.c

package info (click to toggle)
digitools 1.03-1.2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch
  • size: 232 kB
  • ctags: 356
  • sloc: ansic: 2,143; makefile: 260; sh: 36
file content (525 lines) | stat: -rw-r--r-- 12,566 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
    digipanel.c - A user-space program to write to the ASUS DigiMatrix
                  front panel.
    Copyright (c) 2005 Richard Taylor <richard@artaylor.co.uk>
                  2006 Andrew Calkin <calkina@geexbox.org> and
                       Ben Potter <linux@bpuk.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <time.h>
#include "digitools.h"
#include "i2c-dev.h"
#include "it8705.h"
#include "oz263.h"
#include "digipanel.h"
#include "sndctrl.h"

static struct option digipanel_longopts[] = {
    { "display",     1, 0, 'd' },
    { "reinit",      1, 0, 'D' },
    { "temp",        0, 0, 't' },
    { "volume",      0, 0, 'l' },
    { "time",        0, 0, 's' },
    { "kill",        0, 0, 'k' },
    { "version",     0, 0, 'v' },
    { "help",        0, 0, 'h' },
    { NULL,          0, 0,  0  }
};

static struct option digiradio_longopts[] = {
    { "radio",       0, 0, 'r' },
    { "control",     1, 0, 'c' },
    { "version",     0, 0, 'v' },
    { "help",        0, 0, 'h' },
    { NULL,          0, 0,  0  }
};

static void digipanel_help(char *argv0)
{
#ifndef SILENT
  printf(
"\n"
"Usage: %s [OPTIONS]\n"
"\n"
"  WARNING: If you are using this on a system other than the ASUS\n"
"  DigiMatrix, then there is a chance that you may destroy an EEPROM.\n"
"  This program needs to be run as root.\n"
"\n"
"  -s, --time             Display the local time.\n"
"  -t, --temp             Display the temperature.\n"
"  -l, --volume           Display (any changes of) the volume control.\n"
"  -k, --kill             Stop the daemon (if running) and exit.\n"
"\n"
"  -d, --display [\"xxxxxx\"]   Set the display to the numbers specified.\n"
"  -D, --reinit [\"xxxxxx\"]  As above, but re-initilises the display first.\n"
"\n"
"  -v, --version          Print version number.\n"
"  -h, --help             Display this help message.\n"
"\n", argv0);
#endif
}

static void digiradio_help(char *argv0)
{
#ifndef SILENT
  printf(
"\n"
"Usage: %s [OPTIONS]\n"
"\n"
"  WARNING: If you are using this on a system other than the ASUS\n"
"  DigiMatrix, then there is a chance that you may destroy an EEPROM.\n"
"  This program needs to be run as root.\n"
"\n"
"  -r, --radio [am | fm | off]  Set the radio to am, fm or off.\n"
"  -c, --control [seek-|seek+|fine-|fine+|ch-|ch+|prog|cancel|store]\n"
"      Control the radio, where:\n"
"          seek+/-      seeks to the next station\n"
"          fine+/-      manually increases / decreases frequency\n"
"          ch+/-        selects a stored presets\n"
"          prog         begins storing the current frequency\n"
"                       (use ch+/- to choose preset location)\n"
"          cancel       aborts the store\n"
"          store        commits the frequency to the flashing preset\n"
"\n"
"  -v, --version          Print version number.\n"
"  -h, --help             Display this help message.\n"
"\n", argv0);
#endif
}

int digipanel_main(int argc, char *argv[])
{
  extern char digits[7];
  extern int file;
  int ch, longindex = 0;

  BOOL b_update_temp = FALSE;
  BOOL b_update_time = FALSE;
  BOOL b_update_volume = FALSE;

  extern LED led;
  extern PP pp;
  extern DOTS dots;

  time_t current_time;
  struct tm *time_date;
  /* default displays are set to '    ' and '  ' respectively */
  char ascii_time[5] = {0x0f, 0x0f, 0x0f, 0x0f,'\0'};
  char current_temp[3] = {0x0f, 0x0f,'\0'};

  char volume;
  long vol_current;
  long vol_min, vol_max;
  getvolrange(&vol_min, &vol_max);
  int vol_step=1;

  if ( (int)getuid() != 0)
  {
#ifndef SILENT
    fprintf(stderr,"Must be root to run this program!\n");
#endif
    return -1;
  }

  if (argc == 1)
  {
#ifndef SILENT
    printf("\nPlease supply some parameters.\n"
             "Use %s -h for list.\n\n", argv[0]);
#endif
    return 1;
  }

  /* Parse input params */
  for (;;)
  {
    if ((ch = getopt_long(argc, argv, "d:D:tlskvh",
                digipanel_longopts, &longindex)) == -1)
      break;


    switch (ch)
    {
      case 'd':
      case 'D':
        if (!file)
          file=oz263_I2C_init();

        strncpy(digits, optarg, 6);
        digits[6]='\0';
#ifndef SILENT
        printf("Args = %s\n", optarg);
        printf("Setting display to: %s", digits);
#endif
        if (strstr(optarg, " play"))
        {
          pp = PP_PLAY;
#ifndef SILENT
          printf(" play");
#endif
        }
        else if (strstr(optarg, " pause"))
        {
          pp = PP_PAUSE;
#ifndef SILENT
          printf(" pause");
#endif
        }

        if (strstr(optarg, " am"))
        {
          led = LED_AM;
#ifndef SILENT
          printf(" am");
#endif
        }
        else if (strstr(optarg, " fm"))
        {
          led = LED_FM;
#ifndef SILENT
          printf(" fm");
#endif
        }
        else if (strstr(optarg, " cd"))
        {
          led = LED_CD;
#ifndef SILENT
          printf(" cd");
#endif
        }
        else if (strstr(optarg, " hd"))
        {
          led = LED_HD;
#ifndef SILENT
          printf(" hd");
#endif
        }

        if (strstr(optarg, "both"))
        {
          dots = DOTS_BOTH;
#ifndef SILENT
          printf(" :");
#endif
        }
        else if (strstr(optarg, "middle"))
        {
          dots = DOTS_MIDDLE;
#ifndef SILENT
          printf(" -");
#endif
        }
        else if (strstr(optarg, "bottom"))
        {
          dots = DOTS_BOTTOM;
#ifndef SILENT
          printf(" .");
#endif
        }
        printf("\n");
        if (ch == 'D')
          oz263_display(file, digits, led, pp, dots, (BOOL)1);
        else
          oz263_display(file, digits, led, pp, dots, (BOOL)0);
        break;

      case 't':
        it87_open();
        (void)it87_ec_port_open();
        b_update_temp = TRUE;
        break;

      case 'l':
        b_update_volume = TRUE;
        break;

      case 's':
        b_update_time = TRUE;
        break;

      case 'v':
#ifndef SILENT
        printf("ASUS DigiMatrix Panel Control, from DigiTools "
               "Version %s\n", DIGI_VER);
#endif
        break;

      case 'k':
#ifndef SILENT
        printf("Stopping the %s daemon\n",argv[0]);
#endif
        daemon_kill();
        exit(0);

      case 'h':
        digipanel_help(argv[0]);
        return 0;
        break;

      default:
        digipanel_help(argv[0]);
        return 0;
        break;
    }
  }

  if (b_update_temp || b_update_time || b_update_volume)
  {
    if (!file)
      file=oz263_I2C_init();
    daemon_kill();
    /* Attempt to enter daemon mode, return control to terminal */
    daemon_init();
  }
  else
    exit(0);

  /* Begin daemon loop */
  unsigned int temp_counter = 0;

  while (1)
  {
    if (temp_counter >=
           (TEMP_UPDATE_INTERVAL_MS / VOL_UPDATE_INTERVAL_MS))
    {
      temp_counter = 0;

      if (b_update_temp)
        sprintf(current_temp, "%02X", it87_get_temp());

      if (b_update_time)
      {
        /* Make the dots flash */
        if (dots == DOTS_NONE)
          dots = DOTS_BOTH;
        else
          dots = DOTS_NONE;
        time(&current_time);
        time_date = localtime(&current_time);
        strftime(ascii_time, 5, "%H%M", time_date);
      }

      if (b_update_time || b_update_temp)
      {
        sprintf(digits, "%s%s", current_temp, ascii_time);
        oz263_display(file, digits, led, pp, dots, (BOOL)0);
      }

    }

    if (b_update_volume)
    {
      oz263_write_byte(file, 0x24);
      volume = oz263_read_byte(file);

      if (volume != -120 && volume != 0)
      {
        /* Set delay time to hold display */
        for (temp_counter=12; temp_counter > 0; temp_counter--)
        {
          getvolume(&vol_current);
          if (volume > 0) /*use vol_max-1 so never 100 (3digits)*/
          {
            if (vol_current+vol_step < vol_max-1)
              vol_current=vol_current+vol_step;
            else
              vol_current=vol_max-1;
          }
          if (volume < 0)
          {
            if (vol_current-vol_step > vol_min)
              vol_current=vol_current-vol_step;
            else
              vol_current=vol_min;
          }
          setvolume(vol_current);

          dots = DOTS_NONE;
          sprintf(digits, "%02ld    ",
             (100*(vol_current-vol_min))/(vol_max-vol_min));
          oz263_display(file, digits, led, pp, dots, (BOOL)0);

          usleep(VOL_UPDATE_INTERVAL_MS * 1000); /* Delay of 500ms*/
          oz263_write_byte(file, 0x24);
          volume = oz263_read_byte(file);
        }
      }
    }

    usleep(VOL_UPDATE_INTERVAL_MS * 1000); /* Delay of 500ms*/
    temp_counter++;
  }
}

int digiradio_main(int argc, char *argv[])
{
  int file=0;
  int ch, longindex = 0;

  if ( (int)getuid() != 0)
  {
#ifndef SILENT
    fprintf(stderr,"Must be root to run this program!\n");
#endif
    return -1;
  }

  if (argc == 1)
  {
#ifndef SILENT
    printf("\nPlease supply some parameters.\n"
             "Use %s -h for list.\n\n", argv[0]);
#endif
    return 1;
  }

  /* Parse input params */
  for (;;)
  {
    if ((ch = getopt_long(argc, argv, "r:c:vh",
                          digiradio_longopts, &longindex)) == -1)
      break;

    switch (ch)
    {
      case 'r':
        if (!file)
          file=oz263_I2C_init();
#ifndef SILENT
        printf("Setting radio ");
#endif
        if (!strcmp(optarg, "fm"))
        {
#ifndef SILENT
          printf("ON  - FM\n");
#endif
          oz263_radio_mode(file, FM_RADIO);
        }
        else if (!strcmp(optarg, "am"))
        {
#ifndef SILENT
          printf("ON - AM\n");
#endif
          oz263_radio_mode(file, AM_RADIO);
        }
        else
        {
#ifndef SILENT
          printf("OFF \n");
#endif
          oz263_radio_mode(file, OFF_MODE);
        }
        break;

      case 'c':
        if (!file)
          file=oz263_I2C_init();
#ifndef SILENT
        printf("Controlling radio: ");
#endif
        if (!strcmp(optarg, "seek-"))
        {
#ifndef SILENT
          printf("Seeking down\n");
#endif
          oz263_radio_control(file, SEEK_DOWN);
        }
        else if (!strcmp(optarg, "seek+"))
        {
#ifndef SILENT
          printf("Seeking up\n");
#endif
          oz263_radio_control(file, SEEK_UP);
        }
        else if (!strcmp(optarg, "fine-"))
        {
#ifndef SILENT
          printf("Fine tuning down\n");
#endif
          oz263_radio_control(file, FINE_TUNE_DOWN);
        }
        else if (!strcmp(optarg, "fine+"))
        {
#ifndef SILENT
          printf("Fine tuning up\n");
#endif
          oz263_radio_control(file, FINE_TUNE_UP);
        }
        else if (!strcmp(optarg, "ch-"))
        {
#ifndef SILENT
          printf("Previous preset\n");
#endif
          oz263_radio_control(file, CHANNEL_PREV);
        }
        else if (!strcmp(optarg, "ch+"))
        {
#ifndef SILENT
          printf("Next preset\n");
#endif
          oz263_radio_control(file, CHANNEL_NEXT);
        }
        else if (!strcmp(optarg, "prog"))
        {
#ifndef SILENT
          printf("Program preset - now select preset with ch+/-\n");
#endif
          oz263_radio_control(file, STORE_START);
        }
        else if (!strcmp(optarg, "cancel"))
        {
#ifndef SILENT
          printf("Program store cancelled\n");
#endif
          oz263_radio_control(file, STORE_CANCEL);
        }
        else if (!strcmp(optarg, "store"))
        {
#ifndef SILENT
          printf("Preset stored\n");
#endif
          oz263_radio_control(file, STORE_COMMIT);
        }
        break;

      case 'v':
#ifndef SILENT
        printf("ASUS DigiMatrix Radio Control, from DigiTools "
               "Version %s\n", DIGI_VER);
#endif
        break;

      case 'h':
        digiradio_help(argv[0]);
        return 0;
        break;

      default:
        digiradio_help(argv[0]);
        return 0;
        break;
    }
  }

  exit(0);
}