Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed time to unsigned long #204

Merged
merged 1 commit into from
Dec 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/CommanderPRO/SimpleFanController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ void SimpleFanController::addFan(uint8_t index, PWMFan* fan) {
}

bool SimpleFanController::updateFans() {
long currentUpdate = millis();
long lastUpdateNumber = lastUpdate / updateRate;
long currentUpdateNumber = currentUpdate / updateRate;
unsigned long currentUpdate = millis();
unsigned long lastUpdateNumber = lastUpdate / updateRate;
unsigned long currentUpdateNumber = currentUpdate / updateRate;
lastUpdate = currentUpdate;
if (lastUpdateNumber < currentUpdateNumber) {
if (triggerSave) {
Expand Down
2 changes: 1 addition & 1 deletion examples/CommanderPRO/SimpleFanController.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ class SimpleFanController : public FanController {
* Indicates that the configuration of the fans has been changed and should be saved.
*/
bool triggerSave = false;
long lastUpdate = 0;
unsigned long lastUpdate = 0;
};
4 changes: 2 additions & 2 deletions examples/HoodLoader2CLPBridge/CLPUSBSerialBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void CLPUSBSerialBridge::handleHID() {
#ifdef DEBUG
Serial.print(F("C"));
Serial.println(rawHIDAndSerialBuffer[0], HEX);
long time = micros();
unsigned long time = micros();
#endif // DEBUG
if (!waitForSynchronization()) {
#ifdef DEBUG
Expand All @@ -93,7 +93,7 @@ void CLPUSBSerialBridge::handleHID() {
sendResponse();

#ifdef DEBUG
long duration = micros() - time;
unsigned long duration = micros() - time;
Serial.print(F("D"));
Serial.println(duration);
#endif // DEBUG
Expand Down
137 changes: 137 additions & 0 deletions examples/UnitTests2/UnitTests2.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
Copyright 2020 Leon Kiefer

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#line 17 "UnitTests2.ino"

#include <AUnit.h>

#include "FastLEDController.h"
#include "FastLEDControllerUtils.h"

using namespace aunit;

class FastLEDControllerTest : public TestOnce {
protected:
void assertCRGB(const CRGB& actual, const CRGB& expected) {
assertEqual(actual.r, expected.r);
assertEqual(actual.g, expected.g);
assertEqual(actual.b, expected.b);
}
void assertCRGBArray(const CRGB* const leds, int from, int to, const CRGB& expected) {
for (int i = from; i <= to; i++) {
assertCRGB(leds[i], expected);
}
}
};

class TestFastLEDController : public FastLEDController {
public:
TestFastLEDController() : FastLEDController(false) {
}

void setLastUpdate(unsigned long time) {
lastUpdate = time;
}

void setCurrentUpdate(unsigned long time) {
currentUpdate = time;
}

using FastLEDController::animation_step;
using FastLEDController::animation_step_count;
};

test(getLEDs) {
CRGB leds[10];
FastLEDController ledController(false);
ledController.addLEDs(0, leds, 10);
assertEqual(ledController.getLEDs(0), leds);
assertEqual(ledController.getLEDs(1), nullptr);
}

testF(FastLEDControllerTest, simpleScaleUp) {
CRGB leds[20];
FastLEDController ledController(false);
fill_solid(leds, 20, CRGB::Black);
ledController.addLEDs(0, leds, 20);

fill_solid(leds, 10, CRGB::White);
CLP::reverse(&ledController, 0);

assertCRGBArray(leds, 0, 9, CRGB::Black);
assertCRGBArray(leds, 10, 19, CRGB::White);
}

test(animation_step) {
TestFastLEDController ledController;
ledController.setLastUpdate(155);
ledController.setCurrentUpdate(165);
assertEqual(ledController.animation_step(5, 100), 0);
assertEqual(ledController.animation_step(10, 100), 50);
assertEqual(ledController.animation_step(20, 100), 25);
assertEqual(ledController.animation_step(40, 100), 12);
assertEqual(ledController.animation_step(5000, 100), 3);
}

test(animation_step_long) {
TestFastLEDController ledController;
ledController.setLastUpdate(3000000000);
ledController.setCurrentUpdate(300000005);
assertEqual(ledController.animation_step(5, 100), 0);
assertEqual(ledController.animation_step(10, 100), 50);
assertEqual(ledController.animation_step(20, 100), 25);
assertEqual(ledController.animation_step(40, 100), 12);
assertEqual(ledController.animation_step(5000, 100), 0);
}

test(animation_step_count) {
TestFastLEDController ledController;
ledController.setLastUpdate(155);
ledController.setCurrentUpdate(165);
assertEqual(ledController.animation_step_count(5, 100), 200);
assertEqual(ledController.animation_step_count(10, 100), 100);
assertEqual(ledController.animation_step_count(20, 100), 50);
assertEqual(ledController.animation_step_count(40, 100), 25);
assertEqual(ledController.animation_step_count(5000, 100), 0);
}

test(animation_step_count_offset) {
TestFastLEDController ledController;
ledController.setLastUpdate(195);
ledController.setCurrentUpdate(205);
assertEqual(ledController.animation_step_count(10000, 100), 1);
}

test(animation_step_count_long) {
TestFastLEDController ledController;
ledController.setLastUpdate(3000000000);
ledController.setCurrentUpdate(3000000010);
assertEqual(ledController.animation_step_count(5, 100), 200);
assertEqual(ledController.animation_step_count(10, 100), 100);
assertEqual(ledController.animation_step_count(20, 100), 50);
assertEqual(ledController.animation_step_count(40, 100), 25);
assertEqual(ledController.animation_step_count(5000, 100), 0);
}

void setup() {
delay(1000);
Serial.begin(115200);
while (!Serial)
;
}

void loop() {
TestRunner::run();
}
2 changes: 1 addition & 1 deletion src/CLPUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void CLP::printFps(const int interval) {

unsigned long now = millis();
frameCount++;
if (now - lastMillis >= interval) {
if (now - lastMillis >= (unsigned int)interval) {
double framesPerSecond = (frameCount * 1000.0) / interval;
Serial.print(F("FPS: "));
Serial.println(framesPerSecond, 1);
Expand Down
10 changes: 5 additions & 5 deletions src/FastLEDController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ int FastLEDController::applySpeed(int duration, const GroupSpeed speed) {
}

int FastLEDController::animation_step(int duration, int steps) {
int currentStep = ((currentUpdate % duration) / ((float)duration)) * steps;
int currentStep = ((currentUpdate % (unsigned int)duration) / ((float)duration)) * steps;
return currentStep;
}

int FastLEDController::animation_step_count(int duration, int steps) {
long lastAnimationNumber = lastUpdate / duration;
long currentAnimationNumber = currentUpdate / duration;
int lastStep = ((lastUpdate % duration) / ((float)duration)) * steps;
int currentStep = ((currentUpdate % duration) / ((float)duration)) * steps;
unsigned long lastAnimationNumber = lastUpdate / (unsigned int)duration;
unsigned long currentAnimationNumber = currentUpdate / (unsigned int)duration;
int lastStep = ((lastUpdate % (unsigned int)duration) / ((float)duration)) * steps;
int currentStep = ((currentUpdate % (unsigned int)duration) / ((float)duration)) * steps;

return currentStep - lastStep + (currentAnimationNumber - lastAnimationNumber) * steps;
}
Expand Down
4 changes: 2 additions & 2 deletions src/FastLEDController.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ class FastLEDController : public LEDController {

ChannelData channelData[CHANNEL_NUM];

long lastUpdate = 0;
long currentUpdate = 0;
unsigned long lastUpdate = 0;
unsigned long currentUpdate = 0;

int applySpeed(int duration, const GroupSpeed speed);
/**
Expand Down
3 changes: 2 additions & 1 deletion src/LEDController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ void LEDController::startLEDAutodetection(uint8_t channel) {
bool LEDController::saveIfNeeded() {
if (triggerSave) {
triggerSave = false;
save();
return save();
}
return false;
}
2 changes: 1 addition & 1 deletion src/LEDController.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class LEDController : public ILEDController {
/**
* Stores the time at which the last command was received by the LEDController.
*/
long lastCommand = 0;
unsigned long lastCommand = 0;

/**
* Trigger update of the LEDs
Expand Down