Go Down

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


Aug 31, 2013, 11:48 pm Last Edit: Aug 31, 2013, 11:50 pm by _frank_ Reason: 1
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.


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

Code: [Select]
 SPIClass SPI;

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

This is a very bad idea:

Code: [Select]

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

 void init(int ss_pin)
   if (ss_pin==0)
     slaveSelectPin = 53; //MEGA
     slaveSelectPin = ss_pin;
   // set the slaveSelectPin as an output:
   pinMode (slaveSelectPin, OUTPUT);
   //data for lcd dip204-4
   // initialize SPI:
   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");

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.

Code: [Select]

void setup() {
 Serial.begin(9600);      // open the serial port at 9600 bps:    
 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.
Please post technical questions on the forum, not by personal message. Thanks!

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


seems to work now without the in class-object

thank you

Go Up