aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-08-20 23:13:53 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-08-20 23:13:53 +0200
commitb4f3e71fdf3d38578d88e7569b727e17a945e053 (patch)
treecf96212dc6d9c930ba425acaadf92ce9cd199250
parenta9d83fba5d79b5d57dc3ce7873ca1f20b66fbe6c (diff)
Back to SerialPacket
-rw-r--r--car/car.ino75
1 files changed, 43 insertions, 32 deletions
diff --git a/car/car.ino b/car/car.ino
index 4ed9e5b..8405dde 100644
--- a/car/car.ino
+++ b/car/car.ino
@@ -1,7 +1,7 @@
// Dimitri Sokolyuk
// 01.01.2017
-//#include <PacketSerial.h>
+#include <PacketSerial.h>
#include <Servo.h>
#include <IRremote.h>
//#include <os48.h>
@@ -10,7 +10,7 @@
#include "pb_stream.h"
#include "elegoo.pb.h"
-//PacketSerial serial;
+PacketSerial serial;
Servo head;
IRrecv irrecv(IR);
@@ -66,38 +66,15 @@ int ir() {
return results.value;
}
-void setup() {
- Serial.begin(57600);
- pb_istream_from_stream(Serial, istream);
- pb_ostream_from_stream(Serial, ostream);
-
- pinMode(Echo, INPUT);
- pinMode(Trig, OUTPUT);
-
- pinMode(IN1, OUTPUT);
- pinMode(IN2, OUTPUT);
- pinMode(IN3, OUTPUT);
- pinMode(IN4, OUTPUT);
-
- pinMode(ENA, OUTPUT);
- pinMode(ENB, OUTPUT);
- pinMode(LED, OUTPUT);
-
- pinMode(S1, INPUT);
- pinMode(S2, INPUT);
- pinMode(S3, INPUT);
-
- pinMode(IR, INPUT);
- irrecv.enableIRIn();
+void onPacket(const uint8_t* buf, size_t size) {
+ Command cmd = Command_init_zero;
- head.attach(SRV);
- lookahead();
- stop();
+ pb_istream_t istream = pb_istream_from_buffer(buf, sizeof(buf));
+ pb_decode_delimited(&istream, Command_fields, &cmd);
}
-void loop() {
- Command cmd = Command_init_zero;
- pb_decode_delimited(&istream, Command_fields, &cmd);
+void env() {
+ uint8_t buf[256];
Event evt = Event_init_zero;
@@ -116,7 +93,41 @@ void loop() {
evt.KeyPress = ir();
evt.has_KeyPress = evt.KeyPress != 0;
+ pb_ostream_t ostream = pb_ostream_from_buffer(buf, sizeof(buf));
pb_encode_delimited(&ostream, Event_fields, &evt);
- delay(1000);
+ serial.send(buf, ostream.bytes_written);
+}
+
+void loop() {
+ env();
+ serial.update();
+}
+
+void setup() {
+ serial.begin(57600);
+ serial.setPacketHandler(&onPacket);
+ //pb_istream_from_stream(Serial, istream);
+ //pb_ostream_from_stream(Serial, ostream);
+
+ pinMode(Echo, INPUT);
+ pinMode(Trig, OUTPUT);
+
+ pinMode(IN1, OUTPUT);
+ pinMode(IN2, OUTPUT);
+ pinMode(IN3, OUTPUT);
+ pinMode(IN4, OUTPUT);
+
+ pinMode(ENA, OUTPUT);
+ pinMode(ENB, OUTPUT);
+ pinMode(LED, OUTPUT);
+
+ pinMode(S1, INPUT);
+ pinMode(S2, INPUT);
+ pinMode(S3, INPUT);
+
+ pinMode(IR, INPUT);
+ irrecv.enableIRIn();
+
+ head.attach(SRV);
}