2015-06-02 3 views
-2

Я совершенно не знаком с C/C++. Я сделал несколько более высоких языков, таких как Java и C#, но это еще что-то, что я должен сказать. Я пытаюсь создать модуль таймера для Arduino, чтобы я мог легко выполнить очередь, которая должна быть выполнена через определенное количество времени без использования delay(). Я хотел бы создать его как можно интуитивно, чтобы я мог легко его повторно использовать.Новичок и модуль таймера, который не компилируется

До сих пор я пробовал много разных подходов. То, что я сделал, в основном читает руководства о том, как достичь определенной цели на C/C++, и создал мой маленький проект из этого.

Основной файл .ino:

#include "Timer.h" 

Timer timer; 

void setup() 
{ 
    // Init the timer 
    timer.init(); 

    pinMode(13, OUTPUT); 

    // Define the state 
    int state = 1; 

    // Create the action 
    TimerAction action; 
    action.create(1000, &timerElapsed, 1 -1); 
    action.state = &state; 
} 

void loop() 
{ 
    timer.run(); 
} 

void timerElapsed(TimerAction action) { 
    int *state = (int*)action.state; 
    if (state == 0) { 
     digitalWrite(13, HIGH); 
     *state = 1; 
    } 
    else 
    { 
     digitalWrite(13, LOW); 
     *state = 0; 
    } 
} 

Это очень просто. Я пытаюсь моргнуть на борту, как доказательство концепции.

Это Timer.h файл:

#ifndef _TIMER_h 
#define _TIMER_h 

#if defined(ARDUINO) && ARDUINO >= 100 
    #include "arduino.h" 
#else 
    #include "WProgram.h" 
#endif 

struct Timer { 
    TimerAction* actions; 
    uint8_t size; 

    void init(); 
    void queue(TimerAction action); 
    void dequeue(TimerAction action); 
    void dequeueIdx(int idx); 
    void run(); 
}; 

struct TimerAction { 
    // Sets whether to repeat the action 
    uint8_t repetitive; 
    // Sets how many times to repeat the action, -1 for infinite 
    // Will only be used when repetitive == 1 
    long finite; 

    void (*callback)(TimerAction sender); 
    void *state; 

    unsigned long last; 
    unsigned long interval; 

    void create(long interval, void(*callback)(TimerAction sender), uint8_t repetitive = 0U, long finite = -1L); 
}; 

#endif 

Это Timer.ino файл:

#include "Timer.h" 

void Timer::init() { 
    size = 0; 
    actions = new TimerAction[10]; 
} 

void Timer::queue(TimerAction action) { 
    action.last = millis(); 
    actions[size - 1] = action; 

    size++; 
} 

void Timer::dequeue(TimerAction action) { 
    for (int i = 0; i < size; i++) 
     if (&(actions[i]) == &action) { 
      memmove(actions + i, actions + i + 1, (size - (i + 1)) * sizeof(TimerAction)); 
      break; 
     } 
} 

void Timer::dequeueIdx(int idx) { 
    memmove(actions + idx, actions + idx + 1, (size - (idx + 1)) * sizeof(TimerAction)); 
} 

void Timer::run() { 
    for (int i = 0; i < size; i++) 
     if ((actions[i].last + actions[i].interval) >= millis()) { 
      TimerAction action = actions[i]; 
      action.callback(action); 
      if (action.repetitive == 1) { 
       if (action.finite > 0) 
        action.finite--; 
       else 
        if (action.finite == 0) 
         dequeueIdx(i); 
      } 
      else 
       dequeueIdx(i); 
     } 
} 

void TimerAction::create(long _interval, void(*_callback)(TimerAction sender), 
         uint8_t _repetitive = 0U, long _finite = -1L) { 
    interval = _interval; 
    callback = _callback; 
    repetitive = _repetitive; 
    finite = _finite; 
} 

Эти ошибки компилятор вылетают:

Arduino: 1.6.1 (Windows 8.1), Board: "Arduino Uno" 

In file included from Stoplicht.ino:1:0: 

Timer.h:13:2: error: 'TimerAction' does not name a type 

    TimerAction* actions; 

^

Timer.h:17:13: error: 'TimerAction' has not been declared 

    void queue(TimerAction action); 

      ^

Timer.h:18:15: error: 'TimerAction' has not been declared 

    void dequeue(TimerAction action); 

      ^

Timer.ino: In member function 'void Timer::init()': 

Timer.ino:5:2: error: 'actions' was not declared in this scope 

Timer.ino: At global scope: 

Timer.ino:8:6: error: prototype for 'void Timer::queue(TimerAction)' does not match any in class 'Timer' 

In file included from Stoplicht.ino:1:0: 

Timer.h:17:7: error: candidate is: void Timer::queue(int) 

    void queue(TimerAction action); 

    ^

Timer.ino:15:6: error: prototype for 'void Timer::dequeue(TimerAction)' does not match any in class 'Timer' 

In file included from Stoplicht.ino:1:0: 

Timer.h:18:7: error: candidate is: void Timer::dequeue(int) 

    void dequeue(TimerAction action); 

    ^

Timer.ino: In member function 'void Timer::dequeueIdx(int)': 

Timer.ino:24:10: error: 'actions' was not declared in this scope 

Timer.ino: In member function 'void Timer::run()': 

Timer.ino:29:8: error: 'actions' was not declared in this scope 

Timer.ino: At global scope: 

Timer.ino:45:52: error: default argument given for parameter 3 of 'void TimerAction::create(long int, void (*)(TimerAction), uint8_t, long int)' [-fpermissive] 

In file included from Stoplicht.ino:1:0: 

Timer.h:36:7: error: after previous specification in 'void TimerAction::create(long int, void (*)(TimerAction), uint8_t, long int)' [-fpermissive] 

    void create(long interval, void(*callback)(TimerAction sender), uint8_t repetitive = 0U, long finite = -1L); 

    ^

Timer.ino:45:52: error: default argument given for parameter 4 of 'void TimerAction::create(long int, void (*)(TimerAction), uint8_t, long int)' [-fpermissive] 

In file included from Stoplicht.ino:1:0: 

Timer.h:36:7: error: after previous specification in 'void TimerAction::create(long int, void (*)(TimerAction), uint8_t, long int)' [-fpermissive] 

    void create(long interval, void(*callback)(TimerAction sender), uint8_t repetitive = 0U, long finite = -1L); 

    ^

Позвольте мне объяснить, почему Я использую Array за std::vector. Моя мысль была такова: Arduino довольно слаб, и к actions к нему придет доступ. Поэтому я подумал, что сначала нужно выполнить его, но он будет следить за тем, чтобы таймер не использовал слишком много ресурсов Arduino.

Я пробовал много вещей, но я не совсем понимаю, где проблема. Вот почему я прошу эксперта взглянуть на мой код и, возможно, поставить меня на правильный путь.

+5

Перед тем, как использовать его в объявлении класса Timer, вам необходимо направить объявление 'struct TimerAction;'. –

+0

OT: Ваш охранник является незаконным. См. Http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier –

+0

@ πάνταῥεῖ omg ответы в комментариях staaaahppppppp –

ответ

3

В Timer.h у вас есть:

TimerAction* actions; 

В момент времени, что компилятор видит эту строку кода он не видел, что такое TimerAction есть. Как таковой он не знает, что такое тип, и дает вам ошибку. Что вам нужно сделать, это forward declareTimerAction до Timer, чтобы компилятор знал, что он делает.

#ifndef _TIMER_h 
#define _TIMER_h 

#if defined(ARDUINO) && ARDUINO >= 100 
    #include "arduino.h" 
#else 
    #include "WProgram.h" 
#endif 

struct TimerAction; 

struct Timer { 
//...