1/22/2014

(Arduino Study) the method to make library

Original source code is like that

---
//sos_without_lib.pde
void dot();
void dash();

int pin = 13;

void setup()
{
  pinMode(pin, OUTPUT);
}

void loop()
{
  dot(); dot(); dot();
  dash(); dash(); dash();
  dot(); dot(); dot();
  delay(3000);
}

void dot()
{
  digitalWrite(pin, HIGH);
  delay(250);
  digitalWrite(pin, LOW);
  delay(250);
}

void dash()
{
  digitalWrite(pin, HIGH);
  delay(1000);
  digitalWrite(pin, LOW);
  delay(250);
}

...


To make library, we have to do 4 steps process,

step 1, write .h , .cpp files,   it is same c++ class, and copy into libraries\morse folder.
step 2, make keywords.txt,  and copy into libraries\morse folder.
step 3, make SOS.pde and copy libraries\morse\Examples\SOS
step 4, programing using the library

.h, .cpp

...
.h ->
#ifdef Morse_H
#define Morse_H

#include "WProgram.h"

class Morse{
  public:
    Morse(int pin);
    void dot();
    void dash();
  private:
    int _pin;

#endif

.cpp ->
#include "WProgram.h"
#include "morse.h"

Morse::Morse(int pin)
{
  pinMode(pin, OUTPUT);
  _pin = pin)
}

void Morse::dot()
{
  digitalWrite(pin, HIGH);
  delay(250);
  digitalWrite(pin, LOW);
  delay(250);
}

void Morse::dash()
{
  digitalWrite(pin, HIGH);
  delay(1000);
  digitalWrite(pin, LOW);
  delay(250);
}
---



...
//SOS.pde
#include < Morse.h>

Morse morse(13);
void setup(){}

void loop(){
     morse.dot();morse.dot();morse.dot();
     morse.dash();morse.dash();morse.dash();
     morse.dot();morse.dot();morse.dot();
     delay(3000);
}

---


so the directory is like that

\arduino-0022\libraries\Morse\keyword.txt,  Morse.cpp, Morse.h
\arduino-0022\libraries\Morse\Examples\SOS\SOS.pde


No comments:

Post a Comment