From b4f3e71fdf3d38578d88e7569b727e17a945e053 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 20 Aug 2017 23:13:53 +0200 Subject: Back to SerialPacket --- car/car.ino | 75 +++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 32 deletions(-) (limited to 'car/car.ino') 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 +#include #include #include //#include @@ -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); } -- cgit v1.2.3