Fixed Line End characters.

This commit is contained in:
skarg
2011-08-23 20:34:12 +00:00
parent 45915f2d22
commit 8b9c531b9f
+431 -431
View File
@@ -1,431 +1,431 @@
/************************************************************************** /**************************************************************************
* *
* Copyright (C) 2009 Steve Karg <skarg@users.sourceforge.net> * Copyright (C) 2009 Steve Karg <skarg@users.sourceforge.net>
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including * "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, * without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to * distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to * permit persons to whom the Software is furnished to do so, subject to
* the following conditions: * the following conditions:
* *
* The above copyright notice and this permission notice shall be included * The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software. * in all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*********************************************************************/ *********************************************************************/
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include "timer.h" #include "timer.h"
/* generic elapsed timer handling */ /* generic elapsed timer handling */
/* interval not to exceed 49.7 days */ /* interval not to exceed 49.7 days */
/* interval of 1ms may be 0 to 1ms */ /* interval of 1ms may be 0 to 1ms */
/************************************************************************* /*************************************************************************
* Description: Sets the start time for an elapsed timer * Description: Sets the start time for an elapsed timer
* Returns: the value of the start timer * Returns: the value of the start timer
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
void timer_elapsed_start( void timer_elapsed_start(
struct etimer *t) struct etimer *t)
{ {
uint32_t now = timer_milliseconds(); uint32_t now = timer_milliseconds();
if (t) { if (t) {
t->start = now; t->start = now;
} }
} }
/************************************************************************* /*************************************************************************
* Description: Gets the amount of elapsed time in milliseconds * Description: Gets the amount of elapsed time in milliseconds
* Returns: elapsed time in milliseconds * Returns: elapsed time in milliseconds
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
uint32_t timer_elapsed_time( uint32_t timer_elapsed_time(
struct etimer *t) struct etimer *t)
{ {
uint32_t now = timer_milliseconds(); uint32_t now = timer_milliseconds();
uint32_t delta = 0; uint32_t delta = 0;
if (t) { if (t) {
delta = now - t->start; delta = now - t->start;
} }
return delta; return delta;
} }
/************************************************************************* /*************************************************************************
* Description: Sets the start time with an offset * Description: Sets the start time with an offset
* Returns: elapsed time in milliseconds * Returns: elapsed time in milliseconds
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
void timer_elapsed_start_offset( void timer_elapsed_start_offset(
struct etimer *t, struct etimer *t,
uint32_t offset) uint32_t offset)
{ {
uint32_t now = timer_milliseconds(); uint32_t now = timer_milliseconds();
if (t) { if (t) {
t->start = now + offset; t->start = now + offset;
} }
} }
/************************************************************************* /*************************************************************************
* Description: Tests to see if time has elapsed * Description: Tests to see if time has elapsed
* Returns: true if time has elapsed * Returns: true if time has elapsed
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
bool timer_elapsed_milliseconds( bool timer_elapsed_milliseconds(
struct etimer *t, struct etimer *t,
uint32_t milliseconds) uint32_t milliseconds)
{ {
return (timer_elapsed_time(t) >= milliseconds); return (timer_elapsed_time(t) >= milliseconds);
} }
/************************************************************************* /*************************************************************************
* Description: Tests to see if time has elapsed * Description: Tests to see if time has elapsed
* Returns: true if time has elapsed * Returns: true if time has elapsed
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
bool timer_elapsed_seconds( bool timer_elapsed_seconds(
struct etimer * t, struct etimer * t,
uint32_t seconds) uint32_t seconds)
{ {
uint32_t milliseconds = seconds; uint32_t milliseconds = seconds;
milliseconds *= 1000L; milliseconds *= 1000L;
return timer_elapsed_milliseconds(t, milliseconds); return timer_elapsed_milliseconds(t, milliseconds);
} }
/************************************************************************* /*************************************************************************
* Description: Tests to see if time has elapsed * Description: Tests to see if time has elapsed
* Returns: true if time has elapsed * Returns: true if time has elapsed
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
bool timer_elapsed_minutes( bool timer_elapsed_minutes(
struct etimer * t, struct etimer * t,
uint32_t minutes) uint32_t minutes)
{ {
uint32_t milliseconds = minutes; uint32_t milliseconds = minutes;
milliseconds *= 1000L; milliseconds *= 1000L;
milliseconds *= 60L; milliseconds *= 60L;
return timer_elapsed_milliseconds(t, milliseconds); return timer_elapsed_milliseconds(t, milliseconds);
} }
/************************************************************************* /*************************************************************************
* Description: Tests to see if time has elapsed * Description: Tests to see if time has elapsed
* Returns: true if time has elapsed * Returns: true if time has elapsed
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
bool timer_elapsed_milliseconds_short( bool timer_elapsed_milliseconds_short(
struct etimer * t, struct etimer * t,
uint16_t value) uint16_t value)
{ {
uint32_t milliseconds; uint32_t milliseconds;
milliseconds = value; milliseconds = value;
return (timer_elapsed_time(t) >= milliseconds); return (timer_elapsed_time(t) >= milliseconds);
} }
/************************************************************************* /*************************************************************************
* Description: Tests to see if time has elapsed * Description: Tests to see if time has elapsed
* Returns: true if time has elapsed * Returns: true if time has elapsed
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
bool timer_elapsed_seconds_short( bool timer_elapsed_seconds_short(
struct etimer * t, struct etimer * t,
uint16_t value) uint16_t value)
{ {
return timer_elapsed_seconds(t, value); return timer_elapsed_seconds(t, value);
} }
/************************************************************************* /*************************************************************************
* Description: Tests to see if time has elapsed * Description: Tests to see if time has elapsed
* Returns: true if time has elapsed * Returns: true if time has elapsed
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
bool timer_elapsed_minutes_short( bool timer_elapsed_minutes_short(
struct etimer * t, struct etimer * t,
uint16_t value) uint16_t value)
{ {
return timer_elapsed_minutes(t, value); return timer_elapsed_minutes(t, value);
} }
/************************************************************************* /*************************************************************************
* Description: Starts an interval timer * Description: Starts an interval timer
* Returns: nothing * Returns: nothing
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
void timer_interval_start( void timer_interval_start(
struct itimer *t, struct itimer *t,
uint32_t interval) uint32_t interval)
{ {
if (t) { if (t) {
t->start = timer_milliseconds(); t->start = timer_milliseconds();
t->interval = interval; t->interval = interval;
} }
} }
/************************************************************************* /*************************************************************************
* Description: Starts an interval timer * Description: Starts an interval timer
* Returns: nothing * Returns: nothing
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
void timer_interval_start_seconds( void timer_interval_start_seconds(
struct itimer *t, struct itimer *t,
uint32_t seconds) uint32_t seconds)
{ {
uint32_t interval = seconds; uint32_t interval = seconds;
interval *= 1000L; interval *= 1000L;
timer_interval_start(t, interval); timer_interval_start(t, interval);
} }
/************************************************************************* /*************************************************************************
* Description: Starts an interval timer * Description: Starts an interval timer
* Returns: nothing * Returns: nothing
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
void timer_interval_start_minutes( void timer_interval_start_minutes(
struct itimer *t, struct itimer *t,
uint32_t minutes) uint32_t minutes)
{ {
uint32_t interval = minutes; uint32_t interval = minutes;
interval *= 1000L; interval *= 1000L;
interval *= 60L; interval *= 60L;
timer_interval_start(t, interval); timer_interval_start(t, interval);
} }
/************************************************************************* /*************************************************************************
* Description: Determines the amount of time that has elapsed * Description: Determines the amount of time that has elapsed
* Returns: elapsed milliseconds * Returns: elapsed milliseconds
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
uint32_t timer_interval_elapsed( uint32_t timer_interval_elapsed(
struct itimer *t) struct itimer *t)
{ {
uint32_t now = timer_milliseconds(); uint32_t now = timer_milliseconds();
uint32_t delta = 0; uint32_t delta = 0;
if (t) { if (t) {
delta = now - t->start; delta = now - t->start;
} }
return delta; return delta;
} }
/************************************************************************* /*************************************************************************
* Description: Determines the amount of time that has elapsed * Description: Determines the amount of time that has elapsed
* Returns: elapsed milliseconds * Returns: elapsed milliseconds
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
uint32_t timer_interval( uint32_t timer_interval(
struct itimer * t) struct itimer * t)
{ {
uint32_t interval = 0; uint32_t interval = 0;
if (t) { if (t) {
interval = t->interval; interval = t->interval;
} }
return interval; return interval;
} }
/************************************************************************* /*************************************************************************
* Description: Tests to see if time has elapsed * Description: Tests to see if time has elapsed
* Returns: true if time has elapsed * Returns: true if time has elapsed
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
bool timer_interval_expired( bool timer_interval_expired(
struct itimer * t) struct itimer * t)
{ {
bool expired = false; bool expired = false;
if (t) { if (t) {
if (t->interval) { if (t->interval) {
expired = timer_interval_elapsed(t) >= t->interval; expired = timer_interval_elapsed(t) >= t->interval;
} }
} }
return expired; return expired;
} }
/************************************************************************* /*************************************************************************
* Description: Sets the interval value to zero so it never expires * Description: Sets the interval value to zero so it never expires
* Returns: nothing * Returns: nothing
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
void timer_interval_no_expire( void timer_interval_no_expire(
struct itimer *t) struct itimer *t)
{ {
if (t) { if (t) {
t->interval = 0; t->interval = 0;
} }
} }
/************************************************************************* /*************************************************************************
* Description: Adds another interval to the start time. Used for cyclic * Description: Adds another interval to the start time. Used for cyclic
* timers that won't lose ticks. * timers that won't lose ticks.
* Returns: nothing * Returns: nothing
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
void timer_interval_reset( void timer_interval_reset(
struct itimer *t) struct itimer *t)
{ {
if (t) { if (t) {
t->start += t->interval; t->start += t->interval;
} }
} }
/************************************************************************* /*************************************************************************
* Description: Restarts the timer with the same interval * Description: Restarts the timer with the same interval
* Returns: nothing * Returns: nothing
* Notes: none * Notes: none
*************************************************************************/ *************************************************************************/
void timer_interval_restart( void timer_interval_restart(
struct itimer *t) struct itimer *t)
{ {
if (t) { if (t) {
t->start = timer_milliseconds(); t->start = timer_milliseconds();
} }
} }
/************************************************************************* /*************************************************************************
* Description: Return the elapsed time * Description: Return the elapsed time
* Returns: number of milliseconds elapsed * Returns: number of milliseconds elapsed
* Notes: only up to 255ms elapsed * Notes: only up to 255ms elapsed
**************************************************************************/ **************************************************************************/
uint8_t timer_milliseconds_delta( uint8_t timer_milliseconds_delta(
uint8_t start) uint8_t start)
{ {
return (timer_milliseconds_byte() - start); return (timer_milliseconds_byte() - start);
} }
/************************************************************************* /*************************************************************************
* Description: Mark the start of a delta timer * Description: Mark the start of a delta timer
* Returns: mark timer starting tick * Returns: mark timer starting tick
* Notes: only up to 255ms elapsed * Notes: only up to 255ms elapsed
**************************************************************************/ **************************************************************************/
uint8_t timer_milliseconds_mark( uint8_t timer_milliseconds_mark(
void) void)
{ {
return timer_milliseconds_byte(); return timer_milliseconds_byte();
} }
#ifdef TEST #ifdef TEST
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include "ctest.h" #include "ctest.h"
static uint32_t Milliseconds; static uint32_t Milliseconds;
uint32_t timer_milliseconds( uint32_t timer_milliseconds(
void) void)
{ {
return Milliseconds; return Milliseconds;
} }
uint32_t timer_milliseconds_set( uint32_t timer_milliseconds_set(
uint32_t value) uint32_t value)
{ {
uint32_t old_value = Milliseconds; uint32_t old_value = Milliseconds;
Milliseconds = value; Milliseconds = value;
return old_value; return old_value;
} }
void testElapsedTimer( void testElapsedTimer(
Test * pTest) Test * pTest)
{ {
struct etimer t; struct etimer t;
uint32_t test_time = 0; uint32_t test_time = 0;
timer_milliseconds_set(test_time); timer_milliseconds_set(test_time);
timer_elapsed_start(&t); timer_elapsed_start(&t);
ct_test(pTest, timer_elapsed_time(&t) == test_time); ct_test(pTest, timer_elapsed_time(&t) == test_time);
test_time = 0xffff; test_time = 0xffff;
timer_milliseconds_set(test_time); timer_milliseconds_set(test_time);
ct_test(pTest, timer_elapsed_time(&t) == test_time); ct_test(pTest, timer_elapsed_time(&t) == test_time);
test_time = 0xffffffff; test_time = 0xffffffff;
timer_milliseconds_set(test_time); timer_milliseconds_set(test_time);
ct_test(pTest, timer_elapsed_time(&t) == test_time); ct_test(pTest, timer_elapsed_time(&t) == test_time);
} }
void testIntervalTimer( void testIntervalTimer(
Test * pTest) Test * pTest)
{ {
struct itimer t; struct itimer t;
uint32_t interval = 0; uint32_t interval = 0;
uint32_t test_time = 0; uint32_t test_time = 0;
timer_milliseconds_set(test_time); timer_milliseconds_set(test_time);
timer_interval_start(&t, interval); timer_interval_start(&t, interval);
test_time = 0xffff; test_time = 0xffff;
timer_milliseconds_set(test_time); timer_milliseconds_set(test_time);
ct_test(pTest, timer_interval(&t) == interval); ct_test(pTest, timer_interval(&t) == interval);
ct_test(pTest, timer_interval_elapsed(&t) == test_time); ct_test(pTest, timer_interval_elapsed(&t) == test_time);
test_time = 0xffffffff; test_time = 0xffffffff;
timer_milliseconds_set(test_time); timer_milliseconds_set(test_time);
ct_test(pTest, timer_interval(&t) == interval); ct_test(pTest, timer_interval(&t) == interval);
ct_test(pTest, timer_interval_elapsed(&t) == test_time); ct_test(pTest, timer_interval_elapsed(&t) == test_time);
test_time = 0; test_time = 0;
timer_milliseconds_set(test_time); timer_milliseconds_set(test_time);
interval = 0xffff; interval = 0xffff;
timer_interval_start(&t, interval); timer_interval_start(&t, interval);
ct_test(pTest, timer_interval(&t) == interval); ct_test(pTest, timer_interval(&t) == interval);
interval = 0xffffffff; interval = 0xffffffff;
timer_interval_start(&t, interval); timer_interval_start(&t, interval);
ct_test(pTest, timer_interval(&t) == interval); ct_test(pTest, timer_interval(&t) == interval);
interval = 0; interval = 0;
timer_interval_start_seconds(&t, interval); timer_interval_start_seconds(&t, interval);
ct_test(pTest, timer_interval(&t) == interval); ct_test(pTest, timer_interval(&t) == interval);
interval = 60L; interval = 60L;
timer_interval_start_seconds(&t, interval); timer_interval_start_seconds(&t, interval);
interval *= 1000L; interval *= 1000L;
ct_test(pTest, timer_interval(&t) == interval); ct_test(pTest, timer_interval(&t) == interval);
} }
#ifdef TEST_TIMER #ifdef TEST_TIMER
int main( int main(
void) void)
{ {
Test *pTest; Test *pTest;
bool rc; bool rc;
pTest = ct_create("Timer", NULL); pTest = ct_create("Timer", NULL);
/* individual tests */ /* individual tests */
rc = ct_addTestFunction(pTest, testElapsedTimer); rc = ct_addTestFunction(pTest, testElapsedTimer);
assert(rc); assert(rc);
rc = ct_addTestFunction(pTest, testIntervalTimer); rc = ct_addTestFunction(pTest, testIntervalTimer);
assert(rc); assert(rc);
ct_setStream(pTest, stdout); ct_setStream(pTest, stdout);
ct_run(pTest); ct_run(pTest);
(void) ct_report(pTest); (void) ct_report(pTest);
ct_destroy(pTest); ct_destroy(pTest);
return 0; return 0;
} }
#endif #endif
#endif #endif