Shop OBEX P1 Docs P2 Docs Learn Events
Issue with writing LCD in my function — Parallax Forums

Issue with writing LCD in my function

If I initialize an LCD in my main program and try to write to it in a function, I get this
Compile... Failed!

-------- compiler messages --------
Included libraries: simpletools
single.c: In function 'DispLCD':
single.c:54:13: error: 'debug_lcd' undeclared (first use in this function)
single.c:54:13: note: each undeclared identifier is reported only once for each function it appears in

Comments

  • Tom,
    Are you using Blockly or Simple IDE. Can you cut & paste the "C" code into code blocks so if blockly we can see how blockly is translating into c (what's being declared & where.)

    Tom M.
  • tomcrawfordtomcrawford Posts: 1,126
    edited 2018-03-05 18:23
    I'm trying to write a DS1302 clock in Blockly. Here is the C code it generates:
    Edit: The lcd is getting connected; I can write to it from main().
    // ------ Libraries and Definitions ------
    #include "simpletools.h"
    
    // ------ Global Variables and Objects ------
    int Temp;
    int MyMode;
    int Seconds;
    int Hours;
    int Minutes;
    int Date;
    int Month;
    int Day;
    int Year;
    
    // ------ Function Declarations ------
    int constrain(int __cVal, int __cMin, int __cMax);
    void DispLCD();
    void ReadDs();
    
    // ------ Main Program ------
    int main() {
      serial *debug_lcd = serial_open(17, 17, 0, 19200);
    
      // ERROR: LCD is not initialized!
      writeChar(debug_lcd, 17);
      pause(5);
      writeChar(debug_lcd, 12);
      pause(5);
      MyMode = 0;
      while (1) {
        ReadDs();
        switch (MyMode) {
          case 0:
            break;
        }
        pause(1000);
      }
    
    }
    
    // ------ Functions ------
    int constrain(int __cVal, int __cMin, int __cMax) {
      if (__cVal < __cMin) __cVal = __cMin;
      if (__cVal > __cMax) __cVal = __cMax;
      return __cVal;
    }
    
    void DispLCD() {
      writeChar(debug_lcd, (128 + (constrain(1, 0, 3) * 20) + constrain(0, 0, 19)));
      Temp = (Hours / 10);
      dprint(debug_lcd, "%x", Temp);
      Temp = (Hours % 10);
      dprint(debug_lcd, "%x", Temp);
      dprint(debug_lcd, ":");
      return;
    }
    
    void ReadDs() {
      low(6);
      shift_out(8, 7, LSBFIRST, 8, 0xBF);
      Seconds = (shift_in(9, 7, LSBPRE, 8));
      Minutes = (shift_in(9, 7, LSBPRE, 8));
      Hours = (shift_in(9, 7, LSBPRE, 8));
      Date = (shift_in(9, 7, LSBPRE, 8));
      Month = (shift_in(9, 7, LSBPRE, 8));
      Day = (shift_in(9, 7, LSBPRE, 8));
      Year = (shift_in(9, 7, LSBPRE, 8));
      high(6);
      return;
    }
    
  • Can you initialize debug_lcd in the function instead of the main and only write to it in the function?

    Tom M
  • I suppose I could, but I want to write to it from a number of functions. Of course, that's because I want a nice modular program.
  • twm47099twm47099 Posts: 867
    edited 2018-03-05 22:59
    I tried my suggestion above (initializing in the function) . It did not work.
    
    serial *debug_lcd = serial_open(17, 17, 0, 19200);
    
    is still located in main.

    I tried running debug_lcd in a new cog and initializing in the new cog. Same problem; the initializing statement shows up in main.
    I tried printing to debug_lcd in main (without a separate function) and it worked.

    Tom M
  • When I am programming manually in C, I will declare my serial devices as globals, outside of main().

    For example for a bluetooth device that I am going to call in a function or use in a separate cog I will:
    fdserial *blut;     // declare bluetooth as serial device
    
    I think that Blockly needs to do the same.

    Tom M
  • Thanks for looking at it, Tom.

    Blockly folks, I think this is a pretty serious restriction. What do I have to do to get this on the "to-fix" list?
  • Hello tomcrawford,

    Thanks for bringing this up. The best way to get any bug or enhancement on our radar is to post it to the Parallax GitHub issues list for that project. This one would go to:

    https://github.com/parallaxinc/BlocklyProp/issues/

    I went ahead and posted this one as an example:

    https://github.com/parallaxinc/BlocklyProp/issues/1391

    ...and encourage you to join and submit if you find any more issues.

    Thanks again, Andy
Sign In or Register to comment.