Home | History | Annotate | Download | only in SparkFun_OLED
      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 SparkFun OLED
     19  * Display panel.
     20  */
     21 
     22 #include <eboled.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_DISPLAY_TEXT "hello world"
     32 
     33 // Structure to hold the decoded command line options
     34 struct pgm_options {
     35   uint8_t      invert_color;
     36   std::string  display_text;
     37 };
     38 
     39 // Be sure to keep the options for longopts and shortopts in the same order
     40 // so that Usage() is correct.
     41 static struct option longopts[] = {
     42   {"help",  no_argument,        NULL,   '?'},
     43   {"text",  required_argument,  NULL,   't'},
     44   {"invert",no_argument,        NULL,   'i'},
     45   {NULL,    0,                  NULL,    0 }
     46 };
     47 static char shortopts[] = "?t:i";
     48 
     49 // Describes the options for this program.
     50 void Usage(char *pgm_name) {
     51   printf("Usage: %s [options...]\n", pgm_name);
     52   printf("Prints a message on the SparkFun OLED Display\n");
     53   printf("Options:\n");
     54   for (int i = 0, j = 0; longopts[i].name; i++, j++) {
     55     if (shortopts[j] == ':')
     56       j++;
     57     printf(" --%-6s or -%c\n", longopts[i].name, shortopts[j]);
     58   }
     59   return;
     60 }
     61 
     62 // Processes all command line options.
     63 //   sets the options members for commnd line options
     64 //   returns (0) on success, -1 otherwise.
     65 int ReadOpts(int argc, char **argv, struct pgm_options *options) {
     66   int  ch = 0;
     67 
     68   while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
     69     switch (ch) {
     70       case 'i':
     71         options->invert_color = true;
     72         break;
     73       case 't':
     74         options->display_text = optarg;
     75         break;
     76       default:
     77         Usage(argv[0]);
     78         return -1;
     79     }
     80   }
     81   argc -= optind;
     82   argv += optind;
     83 
     84   if (options->display_text.length() == 0)
     85     options->display_text = DEFAULT_DISPLAY_TEXT;
     86 
     87   return 0;
     88 }
     89 
     90 int main(int argc, char* argv[]) {
     91   pgm_options options = {false, ""};
     92 
     93   if (ReadOpts(argc, argv, &options) < 0)
     94     return 1;
     95 
     96   upm::EBOLED *display = new upm::EBOLED(
     97       mraa_get_default_i2c_bus(MRAA_MAIN_PLATFORM_OFFSET),
     98       EBOLED_DEFAULT_CD, EBOLED_DEFAULT_RESET);
     99 
    100   if (options.invert_color) {
    101     display->setTextColor(upm::COLOR_BLACK);
    102     display->fillScreen(upm::COLOR_WHITE);
    103   }
    104 
    105   display->write(options.display_text);
    106   display->refresh();
    107   sleep(5);
    108   delete display;
    109 
    110   return 0;
    111 }
    112