Pages: [1]   Go Down
Author Topic: Assistance in creating a Library  (Read 474 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm a complete noob in regards to creating a header file.  I looked at the LibraryTutorial at http://www.arduino.cc/en/Hacking/LibraryTutorial but i couldn't figure out how to apply it to my code.

I have multiple functions that one calls another that calls another (primarily to help me follow what's going on).  The end result is i call RemotePress(Variable) and it calls the appropriate function which may call another which may call another (you get the idea), but it's really just one source function call being RemotePress() and it does an action after it figures out what it is supposed to do.  To complicate matters, it's an overloaded function, so it's declared for INT and for CHAR since i can pass it a number or string.  Can someone help me figure out how to make a header file for this?  Maybe i'm trying to overcomplicate the conversion to a header file.  the void loop() in this example is not needed for the header, it's just to demonstrate two possible arguments.  I want to basically do an include in a project and then be able to do RemotePress(3); and have it do the functions in the include file.  The configuration isn't complete yet, there's still some tweaking (like not screwing with all of PORTB) but that can be tweaked after it's made into a header.  Any help would be greatly appreciated.

Code:
#define LED_PIN 12

void setup()
{
  pinMode(LED_PIN, OUTPUT);     //Set LED Pin as Output
  digitalWrite(LED_PIN, LOW);   //Set LED Pin low
}


void loop()
{
RemotePress(1);
RemotePress("Power");
}

void RemotePress(int RemoteButton)
{
  switch (RemoteButton)
  {
    case 1:
      IR_1();
      break;
    case 2:
      IR_2();
      break;
    case 3:
      IR_3();
      break;
    case 4:
      IR_4();
      break;
    case 5:
      IR_5();
      break;
    case 6:
      IR_6();
      break;
    case 7:
      IR_7();
      break;
    case 8:
      IR_8();
      break;
    case 9:
      IR_9();
      break;
    case 0:
      IR_0();
      break;
    default:
      break;
  }
}

void RemotePress(char *RemoteButton)
{  if (strcmp(RemoteButton, "Power"))
  {
    IR_Power();
  }
  else if (strcmp(RemoteButton, "ChannelUp"))
  {
    IR_ChannelUp();
  }
  else
  {
  }
}

void IROn(int OnCycles)
{
  PORTB=B00010000;
  delayMicroseconds(840*OnCycles);
}

void IROff(int OffCycles)
{
  PORTB=B00000000;
  delayMicroseconds(840*OffCycles);
}

void IROn1Off1(int On1Off1Cycles)
{
  while (On1Off1Cycles > 0)
  {
    IROn(1);
    IROff(1);
    On1Off1Cycles--;
  }
}

void IROn1Off3(int On1Off3Cycles)
{
  while (On1Off3Cycles > 0)
  {
    IROn(1);
    IROff(3);
    On1Off3Cycles--;
  }
}


void IRHeader()
{
  IROn(4);
  IROff(4);
  IROn1Off3(2);
  IROn1Off1(1);
  IROn1Off3(2);
  IROn1Off1(1);
}

void IRFooter()
{
  IROff(1);
  IROn1Off3(2);
  IROff(4);
  delay(1000);
}

void IR_0()
{
  //
  IRHeader();
  //
  IROff(2);
  IROn1Off1(2);
  IROn1Off3(2);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(2);
  IROn1Off1(1);
  IROn(1);
  //
  IRFooter();
  //
}

void IR_1()
{
  //
  IRHeader();
  //
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(2);
  IROn1Off3(4);
  IROn(1);
  //
  IRFooter();
  //
}

void IR_2()
{
  //
  IRHeader();
  //
  IROff(2);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(3);
  IROn(1);
  //
  IRFooter();
  //
}

void IR_3()
{
  //
  IRHeader();
  //
  IROn1Off3(1);
  IROn1Off1(2);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(2);
  IROn1Off3(1);
  IROn1Off1(1);
  IROn1Off3(2);
  IROn(1);
  //
  IRFooter();
  //
}

void IR_4()
{
  //
  IRHeader();
  //
  IROff(2);
  IROn1Off3(1);
  IROn1Off1(2);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(4);
  IROn1Off3(2);
  IROn(1);
  //
  IRFooter();
  //
}

void IR_5()
{
  //
  IRHeader();
  //
  IROn1Off1(1);
  IROn1Off3(1);
  IROn1Off1(1);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(2);
  IROn1Off3(2);
  IROn1Off1(1);
  IROn1Off3(1);
  IROn(1);
  //
  IRFooter();
  //
}

void IR_6()
{
  //
  IRHeader();
  //
  IROff(2);
  IROn1Off1(1);
  IROn1Off3(1);
  IROn1Off1(1);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(1);
  IROn1Off3(1);
  IROn(1);
  //
  IRFooter();
  //
}

void IR_7()
{
  //
  IRHeader();
  //
  IROn1Off3(2);
  IROn1Off1(1);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(2);
  IROn1Off3(1);
  IROn1Off1(2);
  IROn1Off3(1);
  IROn(1);
  //
  IRFooter();
  //
}

void IR_8()
{
  //
  IRHeader();
  //
  IROff(2);
  IROn1Off3(2);
  IROn1Off1(1);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(5);
  IROn1Off3(1);
  IROn(1);
  //
  IRFooter();
  //
}

void IR_9()
{
  //
  IRHeader();
  //
  IROn1Off1(2);
  IROn1Off3(2);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(2);
  IROn1Off3(3);
  IROn1Off1(1);
  IROn(1);
  //
  IRFooter();
  //
}

void IR_Up()
{
  //
  IRHeader();
  //
  IROn1Off3(1);
  IROn1Off1(6);
  IROn1Off3(1);
  IROn1Off1(2);
  IROn1Off3(1);
  IROn1Off1(1);
  IROn1Off3(2);
  IROn1Off1(1);
  IROff(1);
  //
  IRFooter();
  //
}

void IR_Power()
{
  //
  IRHeader();
  //
  IROff(2);
  IROn1Off3(2);
  IROn1Off1(5);
  IROn1Off3(1);
  IROn1Off1(5);
  IROn1Off3(5);
  //
  IRPowerFooter();
  //
}

void IRPowerFooter()
{
  IROff(1);
  IROn1Off3(3);
  delay(1000);
}

void IR_ChannelUp()
{
  //
  IRHeader();
  //
  IROff(2);
  IROn1Off1(1);
  IROn1Off3(3);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(3);
  IROn1Off3(1);
  IROn1Off1(2);
  IROn(1);
  //
  IRFooter();
  //
}

« Last Edit: March 15, 2011, 10:56:54 pm by tempmj » Logged

http://www.controllerprojects.com
My hobby controller projects site.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can someone help me figure out how to make a header file for this?

What do you hope to accomplish by creating a header file?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I wish to accomplish making it more dynamic, just add the header into a project and be able to use the function.  I plan on having several projects that deal with sending remote control codes via IR.  Unfortunately all the IR related libraries i could find examples for deal with RC-5 or other 38kHz modulated IR protocols... i'm dealing with a Scientific Atlanta set top box which is 56kHz modulated with .84 ms increments on it's "ONs and OFFs" so i am trying to make a new header to support this scenario.  If i get it working well i will release it on my website for anyone that would like to use it.  Right now there are some things i need to tweak, such as it relies on a 555 timer to do the 56kHz modulation and i'm accessing PORTB to manipulate one pin with no regard to the state of the other pins on PORTB, but i plan on doing the modulation on the atmega so it will be all software driven and refine my Direct Port Access.  Once that is done i would like to release it as a library.
« Last Edit: March 15, 2011, 10:25:20 pm by tempmj » Logged

http://www.controllerprojects.com
My hobby controller projects site.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12283
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So you'd like to turn your code into a library that can be used by multiple Sketches?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

exactly... i want to be able to do the equivalent of this (maybe named differently, and this is just pseudocode) to turn the power on and go to channel 137.  This would be used for all of my applications that need to control a Scientific Atlanta or Cisco set top box


#include IRControl.h

void loop()
{
RemotePress("Power");
RemotePress(1);
RemotePress(3);
RemotePress(7);
}

Right now if i put those commands in the void loop() of the code eample above it works perfectly.
« Last Edit: March 15, 2011, 10:29:24 pm by tempmj » Logged

http://www.controllerprojects.com
My hobby controller projects site.

0
Offline Offline
Jr. Member
**
Karma: 1
Posts: 77
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you just want the code for functions that are finished, and known work, to be out of the way of current programming, create a new tab using the arrow symbol at the right side of the next to the top blue-green line.  Name it something like     functions    and cut and past the finished code to that tab.  Arduino will take care of including the file with no further effort on your part.  It all gets saved at once and it is possible to compile and load with any of the tabs (really files) open in the editor.  After it all works, then make a library. 
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, 95% of it's done, so it's no problem for me to tweak it once it's made into a library... i'm just trying to figure out how i would go about making that library.  I can follow the example on the arduino website, but that's with one really basic scenario.  My mind isn't wrapping around how to apply it to this code.
Logged

http://www.controllerprojects.com
My hobby controller projects site.

Pages: [1]   Go Up
Jump to: