Go Down

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

_frank_

#15
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.

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 is a very bad idea:

Code: [Select]

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.

Code: [Select]

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

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

_frank_

seems to work now without the in class-object

thank you

Go Up