generic module AeroVaneReadP(uint16_t period) {
provides interface Notify<aerovector_t>;
uses {
-#if 0
- interface Alarm<T32khz, uint16_t>;
-#else
interface Timer<TMilli>;
-#endif
interface Read<uint16_t>;
interface StdControl as AnemometerControl;
- interface AsyncGet<uint16_t> as Count;
+ interface Get<uint16_t> as Revolutions;
interface State;
}
}
PHYS_PER_READ = 4, /* # of physical reads per reading */
};
- uint16_t m_count;
+ uint8_t m_count;
+ uint16_t m_speed;
int m_vane;
int m_sum;
- task void readVane();
-
void init()
{
m_count = 0;
init();
call AnemometerControl.start();
- /* FIXME: First alarm should be synced to top of the next second. How? */
-#if 0
- call Alarm.start(call Alarm.getNow() + period);
-#else
+ /* FIXME: How to get first Timer.fired() synced with top of hour? */
call Timer.startPeriodic(period);
-#endif
return SUCCESS;
}
command error_t Notify.disable()
{
call State.toIdle();
-#if 0
- call Alarm.stop();
-#else
call Timer.stop();
-#endif
call AnemometerControl.stop();
return SUCCESS;
}
-#if 0
- async event void Alarm.fired()
-#else
event void Timer.fired()
-#endif
{
- //call Alarm.start(call Alarm.getAlarm() + period);
- post readVane();
- }
+ if (++m_count == PHYS_PER_READ) {
+ /* Get anemometer revolutions for the period */
+ m_speed = call Revolutions.get();
+ }
- task void readVane()
- {
- //call Alarm.start(call Alarm.getAlarm() + period);
if (!(call State.isIdle()))
call Read.read();
}
{
int d = angle2 - angle1;
- if (d <= -180)
- d += 360;
- else if (d > 180)
- d -= 360;
+ if (d <= -512)
+ d += 1024;
+ else if (d > 512)
+ d -= 1024;
return d;
}
return;
newVane = m_vane + distance(m_vane, result);
- m_sum += result;
+ m_sum += m_vane;
m_vane = newVane;
- if (++m_count == PHYS_PER_READ) {
- norace aerovector_t vector;
-
- /* Get speed average */
- atomic vector.speed = call Count.get();
-
- /* Complete direction average */
- vector.dir = m_sum / PHYS_PER_READ;
- while (vector.dir < 0)
- vector.dir += 360;
- vector.dir %= 360;
+ if (m_count == PHYS_PER_READ) {
+ aerovector_t vector;
+
+ vector.speed = m_speed;
+ if (m_sum > 0)
+ vector.dir = (m_sum + (PHYS_PER_READ / 2)) / PHYS_PER_READ;
+ else
+ vector.dir = (m_sum - (PHYS_PER_READ / 2)) / PHYS_PER_READ + 1024;
+ vector.dir &= 0x03ff; /* %= 1024 */
init();
/* Inform the consumer of the vector */