Skip to content

Commit

Permalink
Minor changes to make it compatible with Atmel Studio
Browse files Browse the repository at this point in the history
  • Loading branch information
elcojacobs committed Oct 5, 2012
1 parent 8c177b7 commit 2b7a6e7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 36 deletions.
9 changes: 5 additions & 4 deletions CShiftPWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/* workaround for a bug in WString.h */
#define F(string_literal) (reinterpret_cast<const __FlashStringHelper *>(PSTR(string_literal)))

#include "CShiftPWM.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include <Arduino.h>
Expand Down Expand Up @@ -343,8 +346,7 @@ void CShiftPWM::InitTimer1(void){
* So the value we want for OCR1A is: timer clock frequency/(LED frequency * number of bightness levels)-1 */
m_prescaler = 1;
OCR1A = round((float) F_CPU/((float) m_ledFrequency*((float) m_maxBrightness+1)))-1;
/* Finally enable the timer interrupt
/* See datasheet 15.11.8) */
/* Finally enable the timer interrupt, see datasheet 15.11.8) */
bitSet(TIMSK1,OCIE1A);
}

Expand Down Expand Up @@ -392,8 +394,7 @@ void CShiftPWM::InitTimer2(void){
* We want the frequency of the timer to be (LED frequency)*(number of brightness levels)
* So the value we want for OCR2A is: timer clock frequency/(LED frequency * number of bightness levels)-1 */
OCR2A = round( ( (float) F_CPU / (float) m_prescaler ) / ( (float) m_ledFrequency*( (float) m_maxBrightness+1) ) -1);
/* Finally enable the timer interrupt
/* See datasheet 15.11.8) */
/* Finally enable the timer interrupt, see datasheet 15.11.8) */
bitSet(TIMSK2,OCIE2A);
}

Expand Down
6 changes: 3 additions & 3 deletions ShiftPWM.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ extern const bool ShiftPWM_balanceLoad;
// Compare with the counter (cp, 1 clockcycle) --> result is stored in carry
// Use the rotate over carry right to shift the compare result into the byte. (1 clockcycle).
#define add_one_pin_to_byte(sendbyte, counter, ledPtr) \
{ \
unsigned char pwmval=*ledPtr; \
{ \
unsigned char pwmval=*ledPtr; \
asm volatile ("cp %0, %1" : /* No outputs */ : "r" (counter), "r" (pwmval): ); \
asm volatile ("ror %0" : "+r" (sendbyte) : "r" (sendbyte) : ); \
asm volatile ("ror %0" : "+r" (sendbyte) : "r" (sendbyte) : ); \
}

// The inline function below uses normal output pins to send one bit to the SPI port.
Expand Down
59 changes: 30 additions & 29 deletions examples/ShiftPWM_Non_Blocking/ShiftPWM_Non_Blocking.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/************************************************************************************************************************************
/*
* ShiftPWM non-blocking RGB fades example, (c) Elco Jacobs, updated August 2012.
*
* This example for ShiftPWM shows how to control your LED's in a non-blocking way: no delay loops.
Expand All @@ -7,7 +7,7 @@
*
* Please go to www.elcojacobs.com/shiftpwm for documentation, fuction reference and schematics.
* If you want to use ShiftPWM with LED strips or high power LED's, visit the shop for boards.
************************************************************************************************************************************/
*/

//#include <Servo.h> <-- If you include Servo.h, which uses timer1, ShiftPWM will automatically switch to timer2

Expand All @@ -34,15 +34,26 @@ const bool ShiftPWM_balanceLoad = false;

#include <ShiftPWM.h> // include ShiftPWM.h after setting the pins!

// Function prototypes (telling the compiler these functions exist).
void oneByOne(void);
void inOutTwoLeds(void);
void inOutAll(void);
void alternatingColors(void);
void hueShiftAll(void);
void randomColors(void);
void fakeVuMeter(void);
void rgbLedRainbow(unsigned long cycleTime, int rainbowWidth);
void printInstructions(void);

// Here you set the number of brightness levels, the update frequency and the number of shift registers.
// These values affect the load of ShiftPWM.
// Choose them wisely and use the PrintInterruptLoad() function to verify your load.
unsigned char maxBrightness = 255;
unsigned char pwmFrequency = 75;
int numRegisters = 6;
int numOutputs = numRegisters*8;
int numRGBLeds = numRegisters*8/3;
int fadingMode = 0; //start with all LED's off.
unsigned int numRegisters = 6;
unsigned int numOutputs = numRegisters*8;
unsigned int numRGBLeds = numRegisters*8/3;
unsigned int fadingMode = 0; //start with all LED's off.

unsigned long startTime = 0; // start time for the chosen fading mode

Expand All @@ -60,8 +71,6 @@ void setup() {
printInstructions();
}



void loop()
{
if(Serial.available()){
Expand Down Expand Up @@ -113,7 +122,6 @@ void loop()
}
}
}
unsigned char brightness;
switch(fadingMode){
case 0:
// Turn all LED's off.
Expand Down Expand Up @@ -207,7 +215,7 @@ void alternatingColors(void){ // Alternate LED's in 6 different colors
unsigned long holdTime = 2000;
unsigned long time = millis()-startTime;
unsigned long shift = (time/holdTime)%6;
for(int led=0; led<numRGBLeds; led++){
for(unsigned int led=0; led<numRGBLeds; led++){
switch((led+shift)%6){
case 0:
ShiftPWM.SetRGB(led,255,0,0); // red
Expand All @@ -231,14 +239,14 @@ void alternatingColors(void){ // Alternate LED's in 6 different colors
}
}

void hueShiftAll(){ // Hue shift all LED's
void hueShiftAll(void){ // Hue shift all LED's
unsigned long cycleTime = 10000;
unsigned long time = millis()-startTime;
unsigned long hue = (360*time/cycleTime)%360;
ShiftPWM.SetAllHSV(hue, 255, 255);
}

void randomColors(){ // Update random LED to random color. Funky!
void randomColors(void){ // Update random LED to random color. Funky!
unsigned long updateDelay = 100;
static unsigned long previousUpdateTime;
if(millis()-previousUpdateTime > updateDelay){
Expand All @@ -247,17 +255,16 @@ void randomColors(){ // Update random LED to random color. Funky!
}
}

void fakeVuMeter(void){ // immitate a VU meter
static int peak = 0;
static int prevPeak = 0;
static int currentLevel = 0;
void fakeVuMeter(void){ // imitate a VU meter
static unsigned int peak = 0;
static unsigned int prevPeak = 0;
static unsigned long currentLevel = 0;
static unsigned long fadeStartTime = startTime;

unsigned char brightness;

unsigned long fadeTime = (currentLevel*2);// go slower near the top

unsigned long time = millis()-fadeStartTime;
unsigned long currentStep = time%(fadeTime);
currentLevel = time%(fadeTime);

if(currentLevel==peak){
// get a new peak value
Expand All @@ -267,7 +274,7 @@ void fakeVuMeter(void){ // immitate a VU meter
}
}

if(millis()-fadeStartTime > fadeTime){
if(millis() - fadeStartTime > fadeTime){
fadeStartTime = millis();
if(currentLevel<peak){ //fading in
currentLevel++;
Expand All @@ -277,7 +284,7 @@ void fakeVuMeter(void){ // immitate a VU meter
}
}
// animate to new top
for(int led=0;led<numRGBLeds;led++){
for(unsigned int led=0;led<numRGBLeds;led++){
if(led<currentLevel){
int hue = (numRGBLeds-1-led)*120/numRGBLeds; // From green to red
ShiftPWM.SetHSV(led,hue,255,255);
Expand Down Expand Up @@ -305,7 +312,7 @@ void rgbLedRainbow(unsigned long cycleTime, int rainbowWidth){
unsigned long time = millis()-startTime;
unsigned long colorShift = (360*time/cycleTime)%360; // this color shift is like the hue slider in Photoshop.

for(int led=0;led<numRGBLeds;led++){ // loop over all LED's
for(unsigned int led=0;led<numRGBLeds;led++){ // loop over all LED's
int hue = ((led)*360/(rainbowWidth-1)+colorShift)%360; // Set hue from 0 to 360 from first to last led and shift the hue
ShiftPWM.SetHSV(led, hue, 255, 255); // write the HSV values, with saturation and value at maximum
}
Expand All @@ -332,10 +339,4 @@ void printInstructions(void){
Serial.println("Type 'm' to see this info again");
Serial.println("");
Serial.println("----");
}






}

0 comments on commit 2b7a6e7

Please sign in to comment.