Go Down

Topic: using library in new class (Read 5404 times) previous topic - next topic

_frank_

Aug 31, 2013, 12:00 pm Last Edit: Aug 31, 2013, 12:11 pm by _frank_ Reason: 1
Hi,
regarding to http://forum.arduino.cc/index.php?topic=72256, 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.

Code: [Select]
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?

michael_x

Are you sure you #include <spi.h>  ?
This should prodvide
Code: [Select]
#include <Arduino.h>
which seems to be missing ...

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

_frank_

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).

nickgammon


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.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

_frank_

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?


PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

_frank_

#6
Aug 31, 2013, 02:49 pm Last Edit: Aug 31, 2013, 02:51 pm by _frank_ Reason: 1
my sketch includes only my library to test the code currently:
Code: [Select]
#include <dip204_spi.h>

if i change it to
Code: [Select]
#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)?

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

_frank_

#8
Aug 31, 2013, 03:03 pm Last Edit: Aug 31, 2013, 03:05 pm by _frank_ Reason: 1
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"

_frank_

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?

PaulS

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

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

The art of getting good answers lies in asking good questions.

_frank_

#11
Aug 31, 2013, 05:10 pm Last Edit: Aug 31, 2013, 05:25 pm by _frank_ Reason: 1
actually i try to add debug-code, but in the moment, i initialize my class, setup and loop seems not to be called.

Code: [Select]
//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):
Code: [Select]
// 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...

_frank_

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

Code: [Select]
private:
  SPIClass SPI;


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

nickgammon


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.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

nickgammon


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

Code: [Select]
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.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up