Jump to content
View in the app

A better way to browse. Learn more.

Tinkerunity

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[C/C++] Alternierende Threshold Callbacks für Distance IR

Featured Replies

Geschrieben

Folgendes Problem:

Es soll ein Callback-Event ausgelöst werden, wenn beim Distance IR Bricklet die gemessene Entfernung einen Schwellwert unterschreitet. Allerdings soll der Callback dann nicht periodisch aufgerufen werden, sondern erst wieder, wenn der Schwellwert wieder überschritten wird (quasi Gut- und Schlecht-Alarm).

 

Vermutlich gibt das bisher die API nicht her. Ich stelle mir vor, dass dieses Verhalten durch eine "-1" bei der debounce_period dargestellt werden könnte:

distance_ir_set_debounce_period(&dist, -1);
distance_ir_register_callback(&dist, DISTANCE_IR_CALLBACK_DISTANCE_REACHED, cb_distance_ir);
distance_ir_set_distance_callback_threshold(&dist, '<', 1000, 0;

In der Bricklet Library müsste dann in simple_tick() der Part von "Handle threshold signals" so angepasst werden, dass

if (BC->signal_period[i] == -1) {
   switch(BC->threshold_option[i]) {
      case 'o': BC->threshold_option[i] = 'i';
                break;
      case 'i': BC->threshold_option[i] = 'o';
                break;
      case '<': BC->threshold_option[i] = '>';
                break;
      case '>': BC->threshold_option[i] = '<';
                break;
   }
}

Auf diese Weise würde man dann jeweils beim Überschreiten der Thresholds in beide Richtungen einen Event bekommen.Supertoll wäre es noch, wenn die Art des Events (Alarm true/false) auch beim Callback-Aufruf aus einem Parameter hervorgehen würde, aber das ging zu Not auch ohne.

 

@TF: Liesse sich das einbauen?

Geschrieben

So etwas nennt man Hysterese.

 

Wäre bestimmt noch eine sinnvolle Implementierung für API(?) --> so dass es gleich als Funktion zum Ausführen vorhanden ist.

  • 2 weeks later...
Geschrieben
  • Autor

Hier mein Vorschlag für eine Anpassung der Firmware (Bricklet):

diff --git a/software/src/config.h b/software/src/config.h
index 531eaa3..71a3b9f 100644
--- a/software/src/config.h
+++ b/software/src/config.h
@@ -10,7 +10,7 @@
#define BRICKLET_HARDWARE_NAME "Distance IR Bricklet 1.0"
#define BRICKLET_FIRMWARE_VERSION_MAJOR 1
#define BRICKLET_FIRMWARE_VERSION_MINOR 1
-#define BRICKLET_FIRMWARE_VERSION_REVISION 1
+#define BRICKLET_FIRMWARE_VERSION_REVISION 2

#define LOGGING_LEVEL LOGGING_DEBUG
#define DEBUG_BRICKLET 0
@@ -28,8 +28,8 @@ typedef struct {
        uint32_t signal_period[NUM_SIMPLE_VALUES];
        uint32_t signal_period_counter[NUM_SIMPLE_VALUES];

-       uint32_t threshold_debounce;
-       uint32_t threshold_period_current[NUM_SIMPLE_VALUES];
+       int32_t  threshold_debounce;
+       int32_t  threshold_period_current[NUM_SIMPLE_VALUES];
        int32_t  threshold_min[NUM_SIMPLE_VALUES];
        int32_t  threshold_max[NUM_SIMPLE_VALUES];
        char     threshold_option[NUM_SIMPLE_VALUES];

und für die Brickletlib:

diff --git a/bricklet_simple.c b/bricklet_simple.c
index 3574134..80e1890 100644
--- a/bricklet_simple.c
+++ b/bricklet_simple.c
@@ -276,7 +276,19 @@ void simple_tick(uint8_t tick_type) {
                                        BA->send_blocking_with_timeout(&sgvr,
                                                                                                   sizeof(SimpleGetValueReturn),
                                                                                                   *BA->com_current);
-                                       BC->threshold_period_current[i] = 0;
+
+                                       // Toggle threshold trigger if debounce has been set to -1
+                                       if(BC->threshold_debounce == -1) {
+                                               switch(BC->threshold_option[i]) {
+                                                       case 'o': BC->threshold_option[i] = 'i';
+                                                                 break;
+                                                       case 'i': BC->threshold_option[i] = 'o';
+                                                                 break;
+                                               }
+                                               BC->threshold_period_current[i] = -500;
+                                       } else {
+                                               BC->threshold_period_current[i] = 0;
+                                       }

                                        logbli("threshold value: %d\n\r", BC->value[i]);
                                }

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.