Pages: [1]   Go Down
Author Topic: Making a Library for I2C OLede 128X64  (Read 993 times)
0 Members and 1 Guest are viewing this topic.
Devon UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
If you dont ask you will never know
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.
  I have read up on creating a Library. But I am having lots of issues in doing so.
The library I am trying to make is for a neat little Oled from E bay
http://www.ebay.co.uk/itm/180882829539?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1439.l2649

The code they supplied workes well and I am trying to make a library for it.
I have deleated everything and starting again from scratch.
Any pointers would be realy helpfull.

Other Library's do not work with this one smiley-sad
Thanks in advance Antony.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But I am having lots of issues in doing so.
Creating a library isn't that difficult. A source file and a header file are all that is required. If you are having issues, tell us what they are, and post the code, and we'll help.
Logged

Devon UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
If you dont ask you will never know
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.
Still not sorted this so will post code and files.
Below is a working code that displays text and scrolls it.
Code:
#include <Wire.h>
#define OLED_ADDRESS    0x51
#define WRITE_CMD       0x01
#define WRITE_DAT       0x02
#define RESET           0x03
#define DISP_8X16STR    0x10
#define FILL_AREA       0x12
#define SET_SCROHOR     0x13
#define SET_ADDRESS     0x21
#define SCROLL_RIGHT    0x26
#define SCROLL_LEFT     0x27

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  OLEDReset();
}
void loop()
{
  delay(1000); 
  OLEDFillArea(0,7,0,128,0);
  delay(1000);
  OLEDdisplay(0,0, "This is Line one");
  OLEDdisplay(2,0, "This is Line two");
  OLEDdisplay(4,0, "Welcome The UK");
  OLEDdisplay(6,0, "Bottom Line");
  ScrollingHorizontal(0x27, 4, 5, 0x07);                                       
  delay(2000);
  ScrollingHorizontal(0x26, 6, 7, 0x07);
  delay(2000);
  OLEDReset();
  delay(2000);
}

void OLEDReset()
{
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(RESET);   
  Wire.endTransmission();
}
void WriteData()
{
  unsigned int i;
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(WRITE_DAT);   
    {
    Wire.write(0xFF); 
  }
  Wire.endTransmission();
}
void OLEDdisplay(uint8_t page, uint8_t column, const char *str)
{
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(DISP_8X16STR);
  Wire.write(page);
  Wire.write(column);
  while(*str != '\0')
  {
    Wire.write(*str++);
  }
  Wire.endTransmission();
}
void OLEDFillArea(uint8_t startpage, uint8_t endpage,uint8_t startcolumn, uint8_t endcolumn,uint8_t filldata)
{
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(FILL_AREA);
  Wire.write(startpage);
  Wire.write(endpage);
  Wire.write(startcolumn);
  Wire.write(endcolumn);
  Wire.write(filldata);
  Wire.endTransmission();
}
void ScrollingHorizontal(uint8_t lr, uint8_t startpage, uint8_t endpage,uint8_t frames)
{
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(SET_SCROHOR);
  Wire.write(lr);
  Wire.write(startpage);
  Wire.write(endpage);
  Wire.write(frames);
  Wire.endTransmission();
}

void WriteCommand(const char *cmd)
{
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(WRITE_CMD);   
  while(*cmd != '\0')
  {
    Wire.write(*cmd++);
  }
  Wire.endTransmission();
}

This is the Code now.
Code:
#include <Wire.h>
#include <OledKozig.h>
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  OLED.Reset();
}
void loop()
{
  delay(1000); 
  OLED.FillArea(0,7,0,128,0);
  delay(1000);
  OLED.display(0,0, "This is Line one");
  OLED.display(2,0, "This is Line two");
  OLED.display(4,0, "Welcome The UK");
  OLED.display(6,0, "Bottom Line");
  OLED.ScrollingHorizontal(0x27, 4, 5, 0x07);                                       
  delay(2000);
  OLED.ScrollingHorizontal(0x26, 6, 7, 0x07);
  delay(2000);
  OLED.Reset();
  delay(2000);
}
Now the H file.
Code:
#ifndef OledKozig_h
#define OledKozig_h

#include <Wire.h>
#define OLED_ADDRESS    0x51
#define WRITE_CMD       0x01
#define WRITE_DAT       0x02
#define RESET           0x03
#define DISP_8X16STR    0x10
#define FILL_AREA       0x12
#define SET_SCROHOR     0x13
#define SET_ADDRESS     0x21
#define SCROLL_RIGHT    0x26
#define SCROLL_LEFT     0x27

class Oled

{
public:
void Reset();
void WriteData();


void display(uint8_t page, uint8_t column, const char *str);


void FillArea(uint8_t startpage, uint8_t endpage,uint8_t startcolumn, uint8_t endcolumn,uint8_t filldata);

void ScrollingHorizontal(uint8_t lr, uint8_t startpage, uint8_t endpage, uint8_t frames);

void WriteCommand(const char *cmd);


};
#endif
And the CPP file
Code:
#include "OledKozig.h"

void OLED::Reset()
{
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(RESET);   
  Wire.endTransmission();
}
void OLED::WriteData()
{
  unsigned int i;
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(WRITE_DAT);   
    {
    Wire.write(0xFF); 
  }
  Wire.endTransmission();
}
void OLED::display(uint8_t page, uint8_t column, const char *str)
{
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(DISP_8X16STR);
  Wire.write(page);
  Wire.write(column);
  while(*str != '\0')
  {
    Wire.write(*str++);
  }
  Wire.endTransmission();
}
void OLED::FillArea(uint8_t startpage, uint8_t endpage,uint8_t startcolumn, uint8_t endcolumn,uint8_t filldata)
{
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(FILL_AREA);
  Wire.write(startpage);
  Wire.write(endpage);
  Wire.write(startcolumn);
  Wire.write(endcolumn);
  Wire.write(filldata);
  Wire.endTransmission();
}
void OLED::ScrollingHorizontal(uint8_t lr, uint8_t startpage, uint8_t endpage,uint8_t frames)
{
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(SET_SCROHOR);
  Wire.write(lr);
  Wire.write(startpage);
  Wire.write(endpage);
  Wire.write(frames);
  Wire.endTransmission();
}

void OLED::WriteCommand(const char *cmd)
{
  Wire.beginTransmission(OLED_ADDRESS);
  Wire.write(WRITE_CMD);   
  while(*cmd != '\0')
  {
    Wire.write(*cmd++);
  }
  Wire.endTransmission();
}

Any help/pointers would be nice.
Thanks Antony.
Modified :- Code updated
« Last Edit: September 27, 2012, 05:58:30 pm by antony1876 » Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have defined a class. You have not created an instance of that class, in the sketch. You are not calling methods on that (non-existent) instance.
Logged

Devon UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
If you dont ask you will never know
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Paul.
  Sorry I don't know what you are referring to??. Must be my age.
I am totally new to creating a library. And only just started with the Arduino.
I have looked over the code. And see no errors. I know there are some. But blind to me.

Regards Antony.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you don't understand classes and instances of classes, why are you trying to create one?

The idea behind creating a class is so that you can make multiple instances of the class, and each one has certain properties and behaviors.

Suppose you create a dog class. You can have multiple dogs - Spot, Fido, Rover, Crappy, etc. Each has the same abilities - it can eat, bark, shit all over the place, chew up the furniture, etc.

What you have done so far is define what a dog is, but you haven't gotten a dog. Then, you want the dog to bark, eat, etc. But, you don't have a dog, so it can't eat. The dog you don't have can't bark, either. On the other hand, the dog you don't have can't shit all over the place or chew up the furniture, so that's not all bad.  smiley-razz

Quote
I have looked over the code. And see no errors. I know there are some. But blind to me.
What was it brought you here in the first place?

Quote
I have read up on creating a Library. But I am having lots of issues in doing so.
You haven't said what those issues are.

Does the code you posted compile? I can't really believe that it does. If it doesn't, then the error messages that the compiler gives you should provide you (and certainly will provide us) clues as to what is wrong.
Logged

Devon UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
If you dont ask you will never know
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.
Ouch kick in the nuts..
The first compleat code compiles OK with no issues. Not bad for a newbie.smiley
Not at home ATM so no access to the list of errors. Will post later on.
Yes I have a lot to learn and I push myself hard to understand HOW..
I really do apreachite the help.

Regards Antony

Update Errors:-  as of  11:44pm Manual typed.
OLED.cpp: In function 'void setup()';
OLED:10: error: 'OLED' was not declared in this scope
OLED.cpp: In function 'void loop()';
OLED:15: error: 'OLED' was not declared in this scopeVoid setup()':
OLEDReset' was not decared in this scope

Not able to Copy and paste from  Message panel  ??
« Last Edit: September 27, 2012, 05:44:50 pm by antony1876 » Logged

Devon UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
If you dont ask you will never know
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi .
Latest update Is I now only have 2 errors which are :-
Code:
OLED.cpp: In function 'void setup()':
OLED:10: error: 'OLED' was not declared in this scope
OLED.cpp: In function 'void loop()':
OLED:15: error: 'OLED' was not declared in this scope
Any help please
Have been on this Library for 9.5 hours now and not able to compleat. smiley-sad
Regards Antony.
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 74
Posts: 2210
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your class is declared like 'class Oled', but you refer to it as 'OLED'. Names must match.

Then like pauls said, you need an instance to work with.

Code:
Oled o_MyOled;
//...
o_MyOled.OLEDdisplay(...);

Can copy errors out of IDE by highlighting text with mouse, then ctrl+c on keyboard.
Logged


Devon UK
Offline Offline
Jr. Member
**
Karma: 0
Posts: 67
If you dont ask you will never know
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi
Oled RIP
Not able to work out my errors.
Antony:(
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50166
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Not able to work out my errors.
Or post them (and the updated code), either, I see. Too bad.
Logged

Pages: [1]   Go Up
Jump to: