Skip to content

Commit

Permalink
fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
sansil committed Mar 13, 2019
1 parent 946617e commit 4ccda06
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 205 deletions.
5 changes: 5 additions & 0 deletions .vscode/arduino.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"board": "arduino:avr:mega",
"configuration": "cpu=atmega2560",
"sketch": "examples/test_timer/test_timer.ino"
}
20 changes: 20 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"/home/sansil/Documents/arduino-1.8.5/hardware/arduino/avr/**",
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"forcedInclude": [
"/home/sansil/Documents/arduino-1.8.5/hardware/arduino/avr/cores/arduino/Arduino.h"
]
}
],
"version": 4
}
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# SoftwareTimer
Software Timer library for Arduino
## SoftwareTimer

### Software Timers library for Arduino

Create software timers from one hardware timer.
60 changes: 35 additions & 25 deletions examples/test_timer/test_timer.ino
Original file line number Diff line number Diff line change
@@ -1,51 +1,61 @@
#include "softwareTimer.h"

//events to fire
char ev0 = 0;
char ev2 =0;
char ev3 =0;
char ev2 = 0;
char ev3 = 0;

Stimer timer0(&ev0);
Stimer mTimer0(&ev0);
Stimer mTimer1;
Stimer mTimer2(&ev2);
Stimer timer3(&ev3);
Stimer mTimer3(&ev3);

void setup()
{

void setup() {

Serial.begin(115200);
Serial.print("tiempo 0:");
Serial.print("time 0:");
Serial.println(millis());

timer0.start(10);
//time in ms
mTimer0.start(10);
mTimer1.start(100);
mTimer2.start(1000);
timer3.start(10);
mTimer3.start(10000);
}

void loop() {
void loop()
{

if(ev0){
Serial.print("[TIMER 0] tiempo end: ");
if (ev0)
{
Serial.print("[TIMER 0] time end: ");
Serial.println(millis());
ev0=0;
timer0.start(1000);
ev0 = 0;
//mTimer0.start(1000);
}
if(ev2){
Serial.print("[TIMER 2] tiempo end: ");
if (ev2)
{
Serial.print("[TIMER 2] time end: ");
Serial.println(millis());
ev2=0;
mTimer2.start(1000);
ev2 = 0;
//mTimer2.start(1000);
}
if(ev3){
Serial.print("[TIMER 3] tiempo end: ");
if (ev3)
{
Serial.print("[TIMER 3] time end: ");
Serial.println(millis());
ev3=0;
timer3.start(16000);
ev3 = 0;
mTimer3.start(16000);
}
if(mTimer1.timeout_flag){
Serial.print("[TIMER 1] tiempo end: ");

//own event
if (mTimer1.timeout_flag)
{
Serial.print("[TIMER 1] time end: ");
Serial.println(millis());
mTimer1.timeout_flag = 0;
mTimer1.start(100);
//mTimer1.start(100);
}

//Serial.println(debug_i);
Expand Down
12 changes: 6 additions & 6 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=Software Timer
version=0.0.1
name=Software Timers
version=0.1.1
author=Santiago Silva Dalla <sansil32@gmail.com>
maintainer=Santiago Silva Dalla <sansil32@gmail.com>
sentence=Software timer library.
paragraph=Software timer library.
category=Data Storage
sentence=A library to create software timers from one hardware timer.
paragraph=Software timers library.
category=Timing
url=https://github.com/sansil/SoftwareTimer
architectures=*
architectures=avr
116 changes: 62 additions & 54 deletions src/hardwareTimer.cpp
Original file line number Diff line number Diff line change
@@ -1,80 +1,88 @@
#include "hardwareTimer.h"

void start_hardware_timer(TIME unTime){
/*
* Seteo el timer en multiplo de 1seg.
* unTime esta en ms.
void start_hardware_timer(TIME unTime)
{
/*
* Set timer on 1sec.
* unTime in ms.
* prescaler 8 max_ms : 32 ms
* prescaler 64 max_ms : 262ms
* prescaler 256 max_ms : 1048 ms
* prescaler 1024 max_ms : 4164 ms
*/


TIME ticks=0;
float prescaler=1;
TIME ticks = 0;
float prescaler = 1;
cli();

if ((unTime>=0) & (unTime <= 32)){
if ((unTime >= 0) & (unTime <= 32))
{
prescaler = 8.0;
}else if((unTime>=33) & (unTime <= 262)){
}
else if ((unTime >= 33) & (unTime <= 262))
{
prescaler = 64.0;
}else if ((unTime>=263) & (unTime <= 1048)){
}
else if ((unTime >= 263) & (unTime <= 1048))
{
prescaler = 256.0;
}else if((unTime>=1049) & (unTime <= 4164)){
}
else if ((unTime >= 1049) & (unTime <= 4164))
{
prescaler = 1024.0;
}else{
prescaler = 1024.0; //TODO control de error
}
// ticks = round((unsigned long)((float)unTime*15624)/1000); //1s 15624 ticks max value 65535
//Serial.println(ticks);
// disable global interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
//OCR1A = ticks;
OCR1A = round((16000.0f/prescaler)*unTime - 1);
// turn on CTC mode:
else
{
prescaler = 1024.0; //TODO: control de error
}

TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1 = 0;

OCR1A = round((16000.0f / prescaler) * unTime - 1);
TIFR1 = 0xFF; // reset flags
// turn on CTC mode:
TCCR1B |= (1 << WGM12);
// Set CS10 and CS12 bits for 1024 prescaler:
switch ((int)prescaler) {
case 8:
TCCR1B |= (1 << CS11);
break;
case 64:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS11);
break;
case 256:
TCCR1B |= (1 << CS12);
break;
case 1024:
default:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
break;
}
// enable timer compare interrupt:
// Set CS10 and CS12 bits for 1024 prescaler:
switch ((int)prescaler)
{
case 8:
TCCR1B |= (1 << CS11);
break;
case 64:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS11);
break;
case 256:
TCCR1B |= (1 << CS12);
break;
case 1024:
default:
TCCR1B |= (1 << CS10);
TCCR1B |= (1 << CS12);
break;
}
// enable timer compare interrupt:
TIMSK1 |= (1 << OCIE1A);
// enable global interrupts:
sei();
}

ISR(TIMER1_COMPA_vect) {
/*
* En la interrupcion hago un update con el timepo que expiro y seteo un nuevo timer con el valor de timer_next( si es que hay uno)
*/
ISR(TIMER1_COMPA_vect)
{

TCCR1A = 0;
TCCR1B = 0; //apago el timer sino queda ciclico por defecto
TCCR1B = 0; //stop timer
time_now = update_time_now();
timers_update(time_now - time_timer_set); // actualizo el nuevo timer_next
/* start physical timer for next shortest time if one exists */
if (timer_next) {
time_timer_set = time_now;
start_hardware_timer(timer_next->time);

timers_update(time_now - time_timer_set); // update new timer_next
/* start physical timer for next shortest time if one exists */
if (timer_next)
{
time_timer_set = time_now;
start_hardware_timer(timer_next->time);
}
}

TIME update_time_now(){
TIME update_time_now()
{
return millis();
}
Loading

0 comments on commit 4ccda06

Please sign in to comment.