aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-08-20 15:27:52 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-08-20 15:27:52 +0200
commit615215d3137d6bd753cbd855928a4dbea105fd05 (patch)
treeeca194596e79592ff48bacebbd0b89ceb4460cbc
parent0f3a959b2f0bb78b12e404230d4a7a3db79e03e4 (diff)
stream
-rw-r--r--car/car.ino30
-rw-r--r--car/common.cpp38
-rw-r--r--car/common.h10
-rw-r--r--car/elegoo/main.go15
-rw-r--r--car/pb_stream.cpp49
-rw-r--r--car/pb_stream.h12
6 files changed, 84 insertions, 70 deletions
diff --git a/car/car.ino b/car/car.ino
index 772aff3..0f5118c 100644
--- a/car/car.ino
+++ b/car/car.ino
@@ -1,16 +1,22 @@
// Dimitri Sokolyuk
// 01.01.2017
+//#include <PacketSerial.h>
#include <Servo.h>
-//#include <IRremote.h>
+#include <IRremote.h>
//#include <os48.h>
#include "config.h"
#include "ir.h"
-#include "common.h"
+
+#include "pb_stream.h"
#include "elegoo.pb.h"
+//PacketSerial serial;
Servo head;
-//IRrecv irrecv(IR);
+IRrecv irrecv(IR);
+
+pb_istream_t istream;
+pb_ostream_t ostream;
void motor(int e, int a, int b, int v) {
if (v > 0) {
@@ -81,7 +87,6 @@ void ultra() {
}
}
-/* FIXME broken package
void ir() {
decode_results results;
if (irrecv.decode(&results)) {
@@ -106,10 +111,11 @@ void ir() {
delay(150);
}
}
-*/
void setup() {
Serial.begin(57600);
+ pb_istream_from_stream(Serial, istream);
+ pb_ostream_from_stream(Serial, ostream);
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
@@ -128,7 +134,7 @@ void setup() {
pinMode(S3, INPUT);
pinMode(IR, INPUT);
-// irrecv.enableIRIn();
+ irrecv.enableIRIn();
head.attach(SRV);
lookahead();
@@ -136,22 +142,18 @@ void setup() {
}
void loop() {
- pb_istream_t input = pb_istream_from_serial();
- Command cmd = {};
- if (Serial.available())
- pb_decode_delimited(&input, Command_fields, &cmd);
+ //Command cmd;
+ //pb_decode_delimited(&istream, Command_fields, &cmd);
- pb_ostream_t output = pb_ostream_from_serial();
Event evt = {};
int d = distance();
if (d > 0) {
evt.Distance = d;
evt.has_Distance = true;
- pb_encode_delimited(&output, Event_fields, &evt);
+ pb_encode_delimited(&ostream, Event_fields, &evt);
}
delay(1000);
//ultra();
- //ir();
+ ir();
}
-
diff --git a/car/common.cpp b/car/common.cpp
deleted file mode 100644
index 7b5522f..0000000
--- a/car/common.cpp
+++ /dev/null
@@ -1,38 +0,0 @@
-//#include <Arduino.h>
-#include <HardwareSerial.h>
-#include "pb_encode.h"
-#include "pb_decode.h"
-
-#define MAXSZ 32
-
-bool
-write_callback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
-{
- int result = Serial.write(buf, count);
-// Serial.flush();
- return result == count;
-}
-
-bool
-read_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
-{
- int result = Serial.readBytes(buf, count);
- if (result == 0) {
- stream->bytes_left = 0; // EOF
- }
- return result == count;
-}
-
-pb_ostream_t
-pb_ostream_from_serial()
-{
- pb_ostream_t stream = {write_callback, NULL, MAXSZ, 0};
- return stream;
-}
-
-pb_istream_t
-pb_istream_from_serial()
-{
- pb_istream_t stream = {read_callback, NULL, MAXSZ};
- return stream;
-}
diff --git a/car/common.h b/car/common.h
deleted file mode 100644
index 2192c1c..0000000
--- a/car/common.h
+++ /dev/null
@@ -1,10 +0,0 @@
-#ifndef COMMON_H
-#define COMMON_H
-
-#include "pb_encode.h"
-#include "pb_decode.h"
-
-pb_istream_t pb_istream_from_serial();
-pb_ostream_t pb_ostream_from_serial();
-
-#endif
diff --git a/car/elegoo/main.go b/car/elegoo/main.go
index 6f1d945..a17ac2a 100644
--- a/car/elegoo/main.go
+++ b/car/elegoo/main.go
@@ -11,14 +11,13 @@ import (
func Write(w io.Writer, buf []byte) {
sz := proto.EncodeVarint(uint64(len(buf)))
- w.Write(sz)
- w.Write(buf)
+ w.Write(append(sz, buf...))
}
func Read(r io.Reader) []byte {
- buf := [10]byte{}
- r.Read(buf[:])
- sz, n := proto.DecodeVarint(buf[:])
+ buf := make([]byte, 80)
+ n, _ := r.Read(buf)
+ sz, n := proto.DecodeVarint(buf[:n])
nbuf := make([]byte, int(sz))
@@ -58,13 +57,13 @@ func main() {
if err != nil {
log.Fatal(err)
}
- log.Printf("Send: %v", buf)
- Write(s, buf)
+ log.Printf("Send: %x", buf)
+ //Write(s, buf)
go func() {
for {
buf := Read(s)
- log.Printf("Got: %v", buf)
+ log.Printf("Got: %x", buf)
evt := &Event{}
proto.Unmarshal(buf, evt)
log.Println(evt)
diff --git a/car/pb_stream.cpp b/car/pb_stream.cpp
new file mode 100644
index 0000000..0eb7fc0
--- /dev/null
+++ b/car/pb_stream.cpp
@@ -0,0 +1,49 @@
+#include "pb_stream.h"
+
+#define MAXSZ (size_t)-1
+
+bool
+os_read(pb_istream_t *stream, uint8_t *buf, size_t count)
+{
+ Stream *s = static_cast<Stream *>(stream->state);
+ while (count > 0) {
+ if (s->available() > 0) {
+ size_t readCount = s->readBytes((char *)buf, count);
+ count -= readCount;
+ }
+ }
+ return true;
+}
+
+
+void
+pb_istream_from_stream(Stream &stream, pb_istream_t &istream)
+{
+ istream.callback = &os_read;
+ istream.state = &stream;
+ istream.bytes_left = MAXSZ;
+#ifndef PB_NO_ERRMSG
+ istream.errmsg = NULL;
+#endif
+}
+
+bool
+os_write(pb_ostream_t *stream, const uint8_t *buf, size_t count)
+{
+ if (stream == NULL || buf == NULL) {
+ return false;
+ }
+ Print *s = static_cast<Print *>(stream->state);
+ return (s->write(buf, count) == count);
+}
+
+void
+pb_ostream_from_stream(Print &stream, pb_ostream_t &ostream) {
+ ostream.callback = &os_write;
+ ostream.state = &stream;
+ ostream.max_size = MAXSZ;
+ ostream.bytes_written = 0;
+#ifndef PB_NO_ERRMSG
+ ostream.errmsg = NULL;
+#endif
+}
diff --git a/car/pb_stream.h b/car/pb_stream.h
new file mode 100644
index 0000000..00be77c
--- /dev/null
+++ b/car/pb_stream.h
@@ -0,0 +1,12 @@
+#ifndef PB_STREAM
+#define PB_STREAM
+
+#include <Stream.h>
+#include <Print.h>
+#include "pb_encode.h"
+#include "pb_decode.h"
+
+void pb_istream_from_stream(Stream &stream, pb_istream_t &istream);
+void pb_ostream_from_stream(Print &stream, pb_ostream_t &ostream);
+
+#endif