Here is an early drop of the Universal Saber (USaber) library I've been working on. So far, only a few blades are supported but I have plans to support other hardware and blade types as I can find time to add them.
Blade Types:
CheapieBlade: Emulates the blade behavior of a cheap toy lightsaber!
SingleLedBlade: Basic one-LED blade with power up/down effects and random flicker!
StringBlade: A basic six-segment string blade.
No documentation yet, but here is some unit test code to show intended usage.
/*
* SaberBlades.ino
* This will test lightsaber blades!
* Created on: Mar 8, 2016
* Author: JakeSoft
*/
#include "SingleLedBlade.h"
#include "CheapieBlade.h"
#include "StringBlade.h"
#define LED_PIN1 3
#define LED_PIN2 5
#define LED_PIN3 6
#define LED_PIN4 9
#define LED_PIN5 10
#define LED_PIN6 11
void TestBlade(IBladeManager* apBlade)
{
bool lPowerupComplete = false;
bool lPowerdownComplete = false;
apBlade->Init();
Serial.println("On()");
apBlade->On();
delay(2000);
Serial.println("Off()");
apBlade->Off();
delay(2000);
Serial.println("PowerUp(2000)");
while(!lPowerupComplete)
{
apBlade->SetChannel(255, 0);
lPowerupComplete = apBlade->PowerUp(2000);
}
delay(1500);
Serial.println("PowerDown(2000)");
while(!lPowerdownComplete)
{
lPowerdownComplete = apBlade->PowerDown(2000);
}
delay(1500);
//Note: Testing flicker patterns is skipped if no patterns are supported
for(int lFlicker = 0; lFlicker < apBlade->GetFeatures().Flickers; lFlicker++)
{
Serial.print("Flicker pattern ");
Serial.println((int)lFlicker);
long lStartTime = millis();
//Turn on the blade
apBlade->On();
//Apply flicker for 5 seconds for
while(millis() - lStartTime < 5000)
{
apBlade->ApplyFlicker(lFlicker);
}
//Turn off the blade
apBlade->Off();
delay(1000);
}
//Test setting individual channels
for(int lChannel = 0; lChannel < apBlade->GetFeatures().Channels; lChannel++)
{
Serial.print("Channel ");
Serial.print(lChannel);
for(int lPower = 0; lPower <= 255; lPower++)
{
apBlade->SetChannel(lPower, lChannel);
apBlade->PerformIO();
delay(1);
}
delay(1500);
for(int lPower = 255; lPower >= 0; lPower--)
{
apBlade->SetChannel(lPower, lChannel);
apBlade->PerformIO();
delay(1);
}
apBlade->Off();
}
delay(1000);
}
/**
* The setup function is called once at startup of the sketch
*/
void setup()
{
Serial.begin(9600);
}
/**
* The loop function is called in an endless loop
*/
void loop()
{
Serial.println("\nTesting CheapieBlade");
IBladeManager* lpBlade = new CheapieBlade(LED_PIN1);
TestBlade(lpBlade);
delete lpBlade;
Serial.println("\n\nTesting SingleLedBlade");
lpBlade = new SingleLedBlade(LED_PIN1);
TestBlade(lpBlade);
delete lpBlade;
Serial.println("\n\nTesting StringBlade");
lpBlade = new StringBlade(LED_PIN1, LED_PIN2, LED_PIN3,
LED_PIN4, LED_PIN5, LED_PIN6);
TestBlade(lpBlade);
delete lpBlade;
delay(5000);
}
Comment out the blade types you aren't interested in or, create your own derived class to implement your own custom solution.
I'm hoping this will be step towards standardization that will allow many contributors to supply cross-compatible code for some really gee-wiz lightsaber projects. Try it out and let me know what you guys think. MTFBWY.