1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * This is an example to display text on the Grove
19 * LCD RGB Display panel.
20 */
21
22#include <jhd1313m1.h>
23#include <mraa.hpp>
24
25#include <getopt.h>
26#include <stdio.h>
27#include <sys/types.h>
28#include <unistd.h>
29#include <string>
30
31#define DEFAULT_COLOR_RED    255
32#define DEFAULT_COLOR_GREEN  255
33#define DEFAULT_COLOR_BLUE   255
34#define DEFAULT_DISPLAY_TEXT "hello world"
35
36// Structure to hold the decoded command line options
37struct pgm_options {
38  uint8_t      red, blue, green;
39  std::string  display_text;
40};
41
42// Be sure to keep the options for longopts and shortopts in the same order
43// so that Usage() is correct.
44static struct option longopts[] = {
45  {"help",  no_argument,        NULL,   '?'},
46  {"text",  required_argument,  NULL,   't'},
47  {"red",   no_argument,        NULL,   'r'},
48  {"green", no_argument,        NULL,   'g'},
49  {"blue",  no_argument,        NULL,   'b'},
50  {NULL,    0,                  NULL,    0 }
51};
52static char shortopts[] = "?t:rgb";
53
54// Describes the options for this program.
55void Usage(char *pgm_name) {
56  printf("Usage: %s [options...]\n", pgm_name);
57  printf("Prints a message on the Grove LCD RGB Display\n");
58  printf("Options:\n");
59  for (int i = 0, j = 0; longopts[i].name; i++, j++) {
60    if (shortopts[j] == ':')
61      j++;
62    printf(" --%-6s or -%c\n", longopts[i].name, shortopts[j]);
63  }
64  return;
65}
66
67// Processes all command line options.
68//   sets the options members for commnd line options
69//   returns (0) on success, -1 otherwise.
70int ReadOpts(int argc, char **argv, struct pgm_options *options) {
71  int  ch = 0;
72  bool color_specified = false;
73
74  while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
75    switch (ch) {
76      case 'r':
77        options->red = 255;
78        color_specified = true;
79        break;
80      case 'g':
81        options->green = 255;
82        color_specified = true;
83        break;
84      case 'b':
85        options->blue = 255;
86        color_specified = true;
87        break;
88      case 't':
89        options->display_text = optarg;
90        break;
91      default:
92        Usage(argv[0]);
93        return -1;
94    }
95  }
96  argc -= optind;
97  argv += optind;
98
99  if (!color_specified) {
100    options->red   = DEFAULT_COLOR_RED;
101    options->green = DEFAULT_COLOR_GREEN;
102    options->blue  = DEFAULT_COLOR_BLUE;
103  }
104  if (options->display_text.length() == 0)
105    options->display_text = DEFAULT_DISPLAY_TEXT;
106
107  return 0;
108}
109
110int main(int argc, char* argv[]) {
111  pgm_options options = {0, 0, 0, ""};
112
113  if (ReadOpts(argc, argv, &options) < 0)
114    return 1;
115
116  upm::Jhd1313m1 display(mraa_get_default_i2c_bus(MRAA_MAIN_PLATFORM_OFFSET));
117  display.write(options.display_text.c_str());
118  display.setColor(options.red, options.green, options.blue);
119  sleep(5);
120
121  display.displayOff();
122  return 0;
123}
124