Skip to content

Commit a972598

Browse files
committed
Send not zero same speed every 0.5 sec
1 parent 8445e8d commit a972598

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

.re-natal

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"prod": "env/prod"
77
},
88
"modules": [
9-
"react-native-ble-manager"
9+
"react-native-ble-manager",
10+
"moment"
1011
],
1112
"imageDirs": [
1213
"images"

setup_memo.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ re-natal init reNatalEsp32controlApp
66
cd re-natal-esp32-control-app
77
npm install --save react-native-ble-manager
88
re-natal use-component react-native-ble-manager
9+
re-natal use-component moment
910
re-natal use-android-device real
1011
re-natal use-figwheel
1112
rect-native link

src/re_natal_esp32control_app/views/ble_control/common.cljs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
[re-natal-esp32control-app.views.common :as v.common]
55
[re-frame.core :refer [subscribe dispatch dispatch-sync]]))
66

7+
(def moment (js/require "moment"))
8+
79
(def service-id "00ff")
810
(def characteristic-id "ff01")
911

@@ -18,12 +20,21 @@
1820
(= (:rf speed1) (:rf speed2))
1921
(= (:rb speed1) (:rb speed2))))
2022

23+
(defn- zero-speed? [speed]
24+
(empty?
25+
(for [key [:lf :lb :rf :rb]
26+
:when (not (zero? (or (key speed) 0)))]
27+
key)))
28+
2129
(defn send-speed []
2230
(let [speed (subscribe [:speed])
23-
sent-speed (subscribe [:sent-speed])]
24-
(when-not (same-speed? @speed @sent-speed)
31+
sent-speed (subscribe [:sent-speed])
32+
now-500ms (.subtract (moment) 500 "ms")]
33+
(when (or (not (same-speed? @speed @sent-speed))
34+
(and (not (zero-speed? @speed))
35+
(.isAfter now-500ms (moment (:sent_at @sent-speed)))))
2536
(ble-send @speed)
26-
(dispatch [:set-sent-speed @speed]))))
37+
(dispatch [:set-sent-speed (assoc @speed :sent_at (js/Date.))]))))
2738

2839
(defn- rate-x-y [p-x p-y view-x view-y view-w view-h]
2940
(let [harf-w (/ view-w 2)

0 commit comments

Comments
 (0)