using library in new class

Hi,
regarding to [solved] DIP204-4 with SPI - Displays - Arduino Forum, i want to create own class for spi-access to the display...
i looked into liquidcrystal-library and saw, that it is inherited from print to get number/float conversion.
so i also inherited from print, included spi.h, but i cannot access to spi-functions and constants.

In file included from sketch_aug31a.ino:1:
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h: In member function ‘void DIP204_SPI::init()’:
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:90: error: ‘OUTPUT’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:90: error: ‘pinMode’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:93: error: ‘SPI’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:93: error: ‘LSBFIRST’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:94: error: ‘SPI_MODE3’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:95: error: ‘SPI_CLOCK_DIV64’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h: In member function ‘void DIP204_SPI::clear()’:
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:118: error: ‘delay’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h: In member function ‘void DIP204_SPI::send(uint8_t, uint8_t)’:
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:191: error: ‘LOW’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:191: error: ‘digitalWrite’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:197: error: ‘SPI’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:199: error: ‘SPI’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:200: error: ‘SPI’ was not declared in this scope
./arduino-1.0.5/libraries/DIP204_SPI/dip204_spi.h:203: error: ‘HIGH’ was not declared in this scope

any help?

dip204_spi.h (5.33 KB)

Are you sure you #include <spi.h> ?
This should prodvide

#include <Arduino.h>

which seems to be missing ...

If you show your dip204_spi.h we might have less but better guesses...

dip204_spi.h is attached to first post (downloaded already by anyone, maybe you?)

there (in line 9): #include <SPI.h>

i got no error, that any include (especially spi.h) is not found.

after attaching it i only changed line 173 to "virtual size_t write(uint8_t value)" because of wrong return type.

do i need to manually include "#include <Arduino.h>"?

the "old" code (without class) is working in current Arduino-Version without errors, so i think its the class-scope, in that i can't access definitions from another library.

inheriting from both (print & spi) seems to complex for me (maybe its not working in arduino/gcc-avr).

frank:
i got no error, that any include (especially spi.h) is not found.

Do you have verbose compiling on? Missing include files are warnings only, sadly.

ok, i've checked detailed info for compiling in the Arduino-Options...and now i got the missing-info "SPI.h not found"

i have put my dip204_spi.h in folder "arduino-1.0.5/libraries/DIP204_SPI/" so i can use it as Library...

what can i do so that the compiler find the SPI.h?

i have put my dip204_spi.h in folder "arduino-1.0.5/libraries/DIP204_SPI/" so i can use it as Library...

That is not where user-develop libraries go.

What does your sketch look like? Only include files referenced in the sketch are available at compile time.

my sketch includes only my library to test the code currently:

#include <dip204_spi.h>

if i change it to

#include <SPI.h>
#include <dip204_spi.h>

it seems to work (errors only because of missing loop)

how can i get this working without including also the SPI.h in my sketch (it is included in the lib..why including it twice)?

how can i get this working without including also the SPI.h in my sketch

Two choices:
Include SPI.h in the sketch.
Use something other than the IDE.

is this the right way to use SPI in my Library or should this been done in another way?

maybe not defining a new library and using it as "normal include"

compiling works with h-file, but SPI seems not to work (no Text on Display)...

i've tested the original-Script that works with my current Setup (attachment)
sketchbook a is with class, b original

any hints? is it possible to use SPI-Library in an own class?

sketchbook.zip (4.22 KB)

is it possible to use SPI-Library in an own class?

Yes. The Ethernet and WiFi classes do that, as does the SD class.

actually i try to add debug-code, but in the moment, i initialize my class, setup and loop seems not to be called.

//DIP204_SPI lcd(53);

void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:    
  Serial.println("setup()");
}

void loop()
{
  Serial.println("loop()");
  delay(1000);
}

if i remove the // in first line, i see nothing more on serial-console...without init, i get 1x "setup()" and multiple "loop()" as expected.

actual sd-Library seems not to use the SPI-Library (sd/utility/sd2card.cpp):

// functions for hardware SPI
/** Send a byte to the card */
static void spiSend(uint8_t b) {
  SPDR = b;
  while (!(SPSR & (1 << SPIF)));
}

i did not understand that a simple assign sends data over SPI...

it seems that i cannot use the global SPI-Object in my Class and have to define my own...

private:
  SPIClass SPI;

after adding this to my class, i got an output on my display :wink:

frank:
is this the right way to use SPI in my Library or should this been done in another way?

Adding the include in the main sketch triggers the IDE to copy SPI.h to the temporary directory where it does its compiles. Even if you don't reference SPI in the main sketch. There is no real workaround to this as far as I know.

Certainly (other than having to do that) you can use SPI (or indeed any other library) in your own library.

frank:
it seems that i cannot use the global SPI-Object in my Class and have to define my own...

private:

SPIClass SPI;




after adding this to my class, i got an output on my display ;)

This doesn't sound at all right. Please post code to demonstrate this. Just describing it, doesn't.

attached my current (working code)

without the creation of the in-class-object i got no Text on Display (write and send is called right).

i've looked in SD, Ethernet and WiFi-Library...they all implement its own SPI-Code (not using the SPI-Library) and creating an Object of their SPI-Class in the main-class.

sketch_aug31a.zip (2.79 KB)

frank:
it seems that i cannot use the global SPI-Object in my Class and have to define my own...

private:

SPIClass SPI;




after adding this to my class, i got an output on my display ;)

This is a very bad idea:

class DIP204_SPI : public Print {
public:
  DIP204_SPI(){}
  DIP204_SPI(int ss_pin){init(ss_pin);}

  void init(int ss_pin)
  {
    Serial.print("DIP204_SPI::init():");
    Serial.println(ss_pin);
    if (ss_pin==0)
      slaveSelectPin = 53; //MEGA
    else
      slaveSelectPin = ss_pin;
    _displaycontrol=LCD_DISPLAYCONTROL;
    // set the slaveSelectPin as an output:
    pinMode (slaveSelectPin, OUTPUT);
    //data for lcd dip204-4
    //http://www.mikrocontroller.net/articles/AVR_LCD_KS0073/DIP204_mit_Hardware-SPI
    SPI.setBitOrder(LSBFIRST);
    SPI.setDataMode(SPI_MODE3);
    SPI.setClockDivider(SPI_CLOCK_DIV64);
    
    // initialize SPI:
    SPI.begin();
    Serial.println("init()");
    command(0x34);        // 34 0011 0100 (8Bit Bus, RE=1)
    command(LCD_4LINE);        // 09 0000 1001 (4 row Mode)
    command(0x30);        // 30 0011 0000 (8Bit Bus, N=0, RE=0, DH=0, REV=0) 
    //command(0x0C);        // 0C 0000 1100 (Display on, Cursor off, blink Cursor off) 
    command(LCD_DISPLAYCONTROL|LCD_DISPLAYON);        // 0C 0000 1100 (Display on, Cursor off, blink Cursor off) 
    command(0x06);        // 06 0000 0110 (Entry Mode Set Cursor Auto-Increment)
    Serial.println("init() finished");
    clear();
  }

Your constructor calls init which does a lot of stuff. You cannot know what order constructors are called in, and should not do any substantial work in one. Call init yourself (not from the constructor). eg.

void setup() {
  Serial.begin(9600);      // open the serial port at 9600 bps:    
  Serial.println("setup()");
  lcd.init(53);   // <--- initialize here
}

Well I see you are already doing that, so lose the "init" from the constructor.

Then delete your "own" instance of SPI that you added.

seems to work now without the in class-object

thank you

sketchbook.zip (4.22 KB)