hallard / ArduiPi_OLED

Common used OLED driver for Raspberry PI
http://hallard.me/adafruit-oled-display-driver-for-pi/
113 stars 59 forks source link

String Parameters #9

Open ChriszGitHub opened 8 years ago

ChriszGitHub commented 8 years ago

Hi,

first, my C++ know-how is not good. I will edit the oled_demo.cpp for setting Title and Subtitle Parameters. Why the Script runs in a Seqfault ?

/*********************************************************************
This is an example for our Monochrome OLEDs based on SSD1306 drivers

  Pick one up today in the adafruit shop!
  ------> http://www.adafruit.com/category/63_98

This example is for a 128x32|64 size display using SPI or I2C to communicate
4 or 5 pins are required to interface

Adafruit invests time and resources providing this open source code, 
please support Adafruit and open-source hardware by purchasing 
products from Adafruit!

Written by Limor Fried/Ladyada  for Adafruit Industries.  
BSD license, check license.txt for more information
All text above, and the splash screen must be included in any redistribution

02/18/2013  Charles-Henri Hallard (http://hallard.me)
                        Modified for compiling and use on Raspberry ArduiPi Board
                        LCD size and connection are now passed as arguments on 
                        the command line (no more #define on compilation needed)
                        ArduiPi project documentation http://hallard.me/arduipi

*********************************************************************/

#include "ArduiPi_OLED_lib.h"
#include "Adafruit_GFX.h"
#include "ArduiPi_OLED.h"

#include <getopt.h>
//#include <string>

#define PRG_NAME        "oled_demo"
#define PRG_VERSION     "1.1"

//using namespace std;

// Instantiate the display
ArduiPi_OLED display;

// Config Option
struct s_opts
{
    int oled;
    int verbose;
    char title[64];
    char subtitle[64];
} ;

int sleep_divisor = 1 ;

// default options values
s_opts opts = {
    OLED_ADAFRUIT_I2C_128x64,   // Default oled
  false                                     // Not verbose
};

#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2

#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16

static unsigned char logo16_glcd_bmp[] =
{ 0b00000000, 0b11000000,
  0b00000001, 0b11000000,
  0b00000001, 0b11000000,
  0b00000011, 0b11100000,
  0b11110011, 0b11100000,
  0b11111110, 0b11111000,
  0b01111110, 0b11111111,
  0b00110011, 0b10011111,
  0b00011111, 0b11111100,
  0b00001101, 0b01110000,
  0b00011011, 0b10100000,
  0b00111111, 0b11100000,
  0b00111111, 0b11110000,
  0b01111100, 0b11110000,
  0b01110000, 0b01110000,
  0b00000000, 0b00110000 };

/* ======================================================================
Function: usage
Purpose : display usage
Input   : program name
Output  : -
Comments: 
====================================================================== */
void usage( char * name)
{
    printf("%s\n", name );
    printf("Usage is: %s --oled type [options]\n", name);
    printf("  --<o>led type\nOLED type are:\n");
    for (int i=0; i<OLED_LAST_OLED;i++)
        printf("  %1d %s\n", i, oled_type_str[i]);

    printf("Options are:\n");
    printf("  --<v>erbose  : speak more to user\n");
    printf("  --<h>elp\n");
    printf("<?> indicates the equivalent short option.\n");
    printf("Short options are prefixed by \"-\" instead of by \"--\".\n");
    printf("Example :\n");
    printf( "%s -o 1 use a %s OLED\n\n", name, oled_type_str[1]);
    printf( "%s -o 4 -v use a %s OLED being verbose\n", name, oled_type_str[4]);
}

/* ======================================================================
Function: parse_args
Purpose : parse argument passed to the program
Input   : -
Output  : -
Comments: 
====================================================================== */
void parse_args(int argc, char *argv[])
{

int c;

   while (1) {
        int this_option_optind = optind ? optind : 1;
        int option_index = 0;

    static struct option long_Options[] =
    {
        {"oled",    required_argument,  0, 'o'},
        {"verbose", no_argument,        0, 'v'},
            {"help",    no_argument,        0, 'h'},
                {"title",   required_argument,  0, 't'},
                {"subtitle",    required_argument,  0, 's'},
        {0,         0,          0,   0}
    };

       c = getopt_long(argc, argv, "vhots:", long_Options, &option_index);

        if (c < 0)
            break;

        switch (c)
        {

            case 'v': opts.verbose = true   ;   break;

            case 'o':
                opts.oled = (int) atoi(ostarg);

                if (opts.oled < 0 || opts.oled >= OLED_LAST_OLED )
                {
                        fprintf(stderr, "--oled %d ignored must be 0 to %d.\n", opts.oled, OLED_LAST_OLED-1);
                        fprintf(stderr, "--oled set to 0 now\n");
                        opts.oled = 0;
                }
            break;

            case 'h':
                usage(argv[0]);
                exit(EXIT_SUCCESS);
            break;

            case 't': strcpy(opts.title, optarg); break;
            case 's': strcpy(opts.subtitle, optarg); break;

            case '?': 

            default:
                fprintf(stderr, "Unrecognized option.\n");
                fprintf(stderr, "Run with '--help'.\n");
                exit(EXIT_FAILURE);
        }
    } /* while */

// exit(EXIT_SUCCESS);
}

/* ======================================================================
Function: main
Purpose : Main entry Point
Input   : -
Output  : -
Comments: 
====================================================================== */
int main(int argc, char **argv)
{
    int i;

    // Oled supported display in ArduiPi_SSD1306.h
    // Get OLED type
    parse_args(argc, argue)

    // I2C change parameters to fit to your LCD
    if ( !display.init(OLED_I2C_RESET,opts.oled) )
        exit(EXIT_FAILURE);

    display.begin();

  // init done
  display.clearDisplay();   // clears the screen  buffer
  display.display();        // display it (clear display)

  display.clearDisplay();

  // Title
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0,0);
  display.printf("%s\n", opts.title);
printf("%s\n", opts.title);

  // Subtitle
  display.setTextColor(BLACK, WHITE); // 'inverted' text
  display.setTextSize(2);
  display.setTextColor(WHITE);

  display.printf("%s\n", opts.subtitle);
printf("%s\n", opts.subtitle);

  display.clearDisplay();   // clears the screen  buffer

  display.display();
  sleep(2);

    // Free PI GPIO ports
    display.close();

}
;
hallard commented 8 years ago

I think you need to declare a buffer for title and subtitle then use strcpy to copy command line data to this buffer.

struct s_opts
{
int oled;
int verbose;
char title[64];
char subtitle[64];
} ;

then

case 't': strcpy(opts.title, optarg); break;
case 's': strcpy(opts.subtitle, optarg); break;
ChriszGitHub commented 8 years ago

Hello Hallard,

thanks for fast response.

Mhhh, not work... I don't understand why...

root@raspberrypi:/usr/local/src/ArduiPi_OLED/examples# ./string_demo -v -o 3
Segmentation fault

root@raspberrypi:/usr/local/src/ArduiPi_OLED/examples# ./string_demo -v -o 3 -t 'title' -s 'subtitle'
Segmentation fault

Edit: I update the code in the first posting...

ChriszGitHub commented 8 years ago

Hello Hallard,

do you have a other idea for me...? very very thanks...