mirror of
https://github.com/Genaker/LoraSA.git
synced 2026-08-08 01:43:01 +02:00
Events for modular UI and other listeners
This commit is contained in:
@@ -102,6 +102,20 @@ int BarChart::y2pos(float y)
|
||||
return height - height * (y - min_y) / (max_y - min_y);
|
||||
}
|
||||
|
||||
void BarChart::onEvent(Event &e)
|
||||
{
|
||||
if (e.type != DETECTED)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int u = updatePoint(e.emitter.current_frequency, e.detected.rssi);
|
||||
if (e.emitter.animated)
|
||||
{
|
||||
drawOne(u);
|
||||
}
|
||||
}
|
||||
|
||||
void DecoratedBarChart::reset(uint16_t x, uint16_t y, uint16_t w, uint16_t h)
|
||||
{
|
||||
Chart::reset(x, y, w, h);
|
||||
|
||||
@@ -12,7 +12,8 @@ size_t StackedChart::addChart(Chart *c)
|
||||
cc[charts_sz] = c;
|
||||
free(charts);
|
||||
|
||||
c->reset(pos_x + c->pos_x, pos_y + c->pos_y, trim_w(c->pos_x, c->width, width), c->height);
|
||||
c->reset(pos_x + c->pos_x, pos_y + c->pos_y, trim_w(c->pos_x, c->width, width),
|
||||
c->height);
|
||||
charts = cc;
|
||||
return charts_sz++;
|
||||
}
|
||||
@@ -82,3 +83,13 @@ void StackedChart::draw()
|
||||
for (int i = 0; i < charts_sz; i++)
|
||||
charts[i]->draw();
|
||||
}
|
||||
|
||||
void StackedChart::onEvent(Event &e)
|
||||
{
|
||||
if (e.type != SCAN_TASK_COMPLETE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
draw();
|
||||
}
|
||||
|
||||
@@ -55,3 +55,13 @@ int WaterfallChart::x2pos(float x)
|
||||
|
||||
return width * (x - min_x) / (max_x - min_x);
|
||||
}
|
||||
|
||||
void WaterfallChart::onEvent(Event &e)
|
||||
{
|
||||
if (e.type != DETECTED)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
updatePoint(e.time_ms, e.detected.freq, e.detected.rssi);
|
||||
}
|
||||
|
||||
+8
-3
@@ -10,6 +10,7 @@ typedef OLEDDisplay Display_t;
|
||||
#endif
|
||||
|
||||
#include <cstdint>
|
||||
#include <events.h>
|
||||
#include <models.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -58,7 +59,7 @@ struct ProgressChart : Chart
|
||||
virtual void drawOne(int x) = 0;
|
||||
};
|
||||
|
||||
struct BarChart : ProgressChart
|
||||
struct BarChart : ProgressChart, Listener
|
||||
{
|
||||
float min_x, max_x, min_y, max_y;
|
||||
float level_y;
|
||||
@@ -83,6 +84,7 @@ struct BarChart : ProgressChart
|
||||
int updatePoint(float x, float y) override;
|
||||
void drawOne(int x) override;
|
||||
void draw() override;
|
||||
void onEvent(Event &) override;
|
||||
|
||||
int x2pos(float x);
|
||||
int y2pos(float y);
|
||||
@@ -109,7 +111,7 @@ struct DecoratedBarChart : Chart
|
||||
void draw() override;
|
||||
};
|
||||
|
||||
struct StackedChart : Chart
|
||||
struct StackedChart : Chart, Listener
|
||||
{
|
||||
Chart **charts;
|
||||
size_t charts_sz;
|
||||
@@ -134,9 +136,11 @@ struct StackedChart : Chart
|
||||
void reset(uint16_t x, uint16_t y, uint16_t w, uint16_t h) override;
|
||||
|
||||
void draw() override;
|
||||
|
||||
void onEvent(Event &e) override;
|
||||
};
|
||||
|
||||
struct WaterfallChart : Chart
|
||||
struct WaterfallChart : Chart, Listener
|
||||
{
|
||||
float min_x, max_x;
|
||||
float level_y, threshold;
|
||||
@@ -156,6 +160,7 @@ struct WaterfallChart : Chart
|
||||
void reset(uint16_t x, uint16_t y, uint16_t w, uint16_t h) override;
|
||||
|
||||
void draw() override;
|
||||
void onEvent(Event &e) override;
|
||||
|
||||
int x2pos(float x);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef LORASA_EVENTS_H
|
||||
#define LORASA_EVENTS_H
|
||||
|
||||
struct Event;
|
||||
enum EventType
|
||||
{
|
||||
DETECTED = 0,
|
||||
SCAN_TASK_COMPLETE,
|
||||
_MAX_EVENT_TYPE // unused as event type
|
||||
};
|
||||
struct Listener;
|
||||
|
||||
#include <cstdint>
|
||||
#include <scan.h>
|
||||
|
||||
struct Event
|
||||
{
|
||||
EventType type;
|
||||
uint64_t epoch;
|
||||
uint64_t time_ms;
|
||||
|
||||
Scan &emitter;
|
||||
|
||||
union
|
||||
{
|
||||
struct
|
||||
{
|
||||
float rssi;
|
||||
float freq;
|
||||
bool trigger;
|
||||
bool detected;
|
||||
size_t detected_at;
|
||||
} detected;
|
||||
};
|
||||
|
||||
Event(Scan &emitter, EventType type, uint64_t time_ms)
|
||||
: emitter(emitter), type(type), epoch(emitter.epoch), time_ms(time_ms) {};
|
||||
};
|
||||
|
||||
struct Listener
|
||||
{
|
||||
virtual void onEvent(Event &event) = 0;
|
||||
};
|
||||
|
||||
#endif
|
||||
+42
-3
@@ -66,8 +66,8 @@ uint16_t Scan::rssiMethod(size_t samples, uint16_t *result, size_t res_size)
|
||||
return max_signal;
|
||||
}
|
||||
|
||||
size_t Scan::detect(uint16_t *result, bool *filtered_result, size_t result_size,
|
||||
int samples)
|
||||
Event Scan::detect(uint16_t *result, bool *filtered_result, size_t result_size,
|
||||
int samples)
|
||||
{
|
||||
size_t max_rssi_x = result_size;
|
||||
|
||||
@@ -122,7 +122,46 @@ size_t Scan::detect(uint16_t *result, bool *filtered_result, size_t result_size,
|
||||
}
|
||||
}
|
||||
|
||||
return max_rssi_x;
|
||||
Event event(*this, EventType::DETECTED, 0);
|
||||
event.epoch = epoch;
|
||||
event.detected.detected = max_rssi_x < result_size;
|
||||
event.detected.freq = current_frequency;
|
||||
event.detected.rssi =
|
||||
event.detected.detected ? -(float)result[max_rssi_x] : LO_RSSI_THRESHOLD;
|
||||
event.detected.detected_at = max_rssi_x;
|
||||
event.detected.trigger =
|
||||
event.detected.detected && event.detected.rssi >= trigger_level;
|
||||
detection_count++;
|
||||
|
||||
return event;
|
||||
}
|
||||
|
||||
size_t Scan::addEventListener(EventType t, Listener &l)
|
||||
{
|
||||
size_t c = listener_count[(size_t)t];
|
||||
Listener **new_list = new Listener *[c + 1];
|
||||
new_list[c] = &l;
|
||||
listener_count[(size_t)t] = c + 1;
|
||||
|
||||
if (c > 0)
|
||||
{
|
||||
Listener **old_list = eventListeners[(size_t)t];
|
||||
memcpy(new_list, old_list, c * sizeof(Listener *));
|
||||
delete[] old_list;
|
||||
}
|
||||
|
||||
eventListeners[(size_t)t] = new_list;
|
||||
return c;
|
||||
}
|
||||
|
||||
void Scan::fireEvent(Event &event)
|
||||
{
|
||||
Listener **list = eventListeners[(size_t)event.type];
|
||||
size_t c = listener_count[(size_t)event.type];
|
||||
for (int i = 0; i < c; i++)
|
||||
{
|
||||
list[i]->onEvent(event);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+15
-4
@@ -1,4 +1,5 @@
|
||||
#include <cstdint>
|
||||
#include <events.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifndef LORASA_CORE_H
|
||||
@@ -41,11 +42,18 @@ struct Scan
|
||||
bool sound_on;
|
||||
bool led_flag;
|
||||
uint64_t detection_count;
|
||||
bool animated;
|
||||
float trigger_level;
|
||||
|
||||
Listener **eventListeners[(size_t)EventType::_MAX_EVENT_TYPE];
|
||||
size_t listener_count[(size_t)EventType::_MAX_EVENT_TYPE];
|
||||
|
||||
Scan()
|
||||
: epoch(0), current_frequency(0), fr_begin(0), fr_end(0),
|
||||
drone_detection_level(0), sound_on(false), led_flag(false),
|
||||
detection_count(0) {};
|
||||
drone_detection_level(0), sound_on(false), led_flag(false), detection_count(0),
|
||||
animated(false), trigger_level(0), listener_count{
|
||||
0,
|
||||
} {};
|
||||
|
||||
virtual float getRSSI() = 0;
|
||||
|
||||
@@ -57,8 +65,11 @@ struct Scan
|
||||
// those values that represent a detection event.
|
||||
// It returns index that represents strongest signal at which a detection event
|
||||
// occurred.
|
||||
static size_t detect(uint16_t *result, bool *filtered_result, size_t result_size,
|
||||
int samples);
|
||||
Event detect(uint16_t *result, bool *filtered_result, size_t result_size,
|
||||
int samples);
|
||||
|
||||
size_t addEventListener(EventType t, Listener &l);
|
||||
void fireEvent(Event &e);
|
||||
};
|
||||
|
||||
// Remove reading without neighbors
|
||||
|
||||
Reference in New Issue
Block a user