Library for JrkG2. this bases on Arduino Library (https://github.com/pololu/jrk-g2-arduino)

Files at this revision

API Documentation at this revision

Comitter:
sgrsn
Date:
Sun Aug 26 08:42:11 2018 +0000
Parent:
1:d611aa1f9f70
Commit message:
Use AsyncSerial for Serial require.

Changed in this revision

JrkG2.cpp Show annotated file Show diff for this revision Revisions of this file
JrkG2.h Show annotated file Show diff for this revision Revisions of this file
diff -r d611aa1f9f70 -r e78c0ddcf337 JrkG2.cpp
--- a/JrkG2.cpp	Sat Aug 25 13:15:38 2018 +0000
+++ b/JrkG2.cpp	Sun Aug 26 08:42:11 2018 +0000
@@ -1,5 +1,7 @@
 #include <JrkG2.h>
 
+#define serial_delay_us 1500    //baud:9600
+
 /**** JrkG2Serial ****/
 
 void JrkG2Serial::commandW7(uint8_t cmd, uint8_t val)
@@ -25,8 +27,10 @@
     uint8_t val;
     
     sendCommandHeader(cmd);
+    wait_us(serial_delay_us);
     if(_stream->readable() != 1)
     {
+        _stream->abort_read();
         _lastError = JrkG2CommReadError;
         return 0;
     }
@@ -41,12 +45,10 @@
     
     sendCommandHeader(cmd);
     
-    //if(_stream->ByteCount() != 2)  //バッファリング未実装のため不可 
-    // → BufferedSerialを使用すべきか, タイムアウトも実装したい
-    
-    //これ以降も同様の理由で修正される
-    if(_stream->readable() == 0)
+    wait_us(serial_delay_us);
+    if(_stream->readable() != 2)
     {
+        _stream->abort_read();
         _lastError = JrkG2CommReadError;
         return 0;
     }
@@ -65,11 +67,12 @@
     
     sendCommandHeader(cmd);
     serialW7(offset);
+    wait_us(serial_delay_us);
     serialW7(length);
-    
-    //if(_stream->ByteCount() != length)
-    if(_stream->readable() != 1)
+    wait_us(serial_delay_us);
+    if(_stream->readable() != length)
     {
+        _stream->abort_read();
         _lastError = JrkG2CommReadError;
         // Set the buffer bytes to 0 so the program will not use an uninitialized
         // variable.
diff -r d611aa1f9f70 -r e78c0ddcf337 JrkG2.h
--- a/JrkG2.h	Sat Aug 25 13:15:38 2018 +0000
+++ b/JrkG2.h	Sun Aug 26 08:42:11 2018 +0000
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "mbed.h"
+#include "AsyncSerial.hpp"
 
 /*example**********************************
 
@@ -12,7 +13,7 @@
     //on the other or both
     
     //on Serial
-    Serial device(p9, p10);
+    AsyncSerial device(p9, p10);
     JrkG2Serial jrk(&device);
     
     //on I2C
@@ -1695,10 +1696,10 @@
 /// Represents a serial connection to a Jrk G2.
 ///
 /// For the high-level commands you can use on this object, see JrkG2Base.
-class JrkG2Serial : public JrkG2Base //public Stream
+class JrkG2Serial : public JrkG2Base
 {
 public:
-    JrkG2Serial(Serial *stream, uint8_t deviceNumber = 255) :
+    JrkG2Serial(AsyncSerial *stream, uint8_t deviceNumber = 255) :
     _deviceNumber(deviceNumber)
     {
         _stream = stream;
@@ -1708,7 +1709,9 @@
     uint8_t getDeviceNumber() { return _deviceNumber; }
 
 private:
-    Serial *_stream;
+    //Serial *_stream;
+    AsyncSerial *_stream;
+    
     const uint8_t _deviceNumber;
     
     virtual void commandQuick(uint8_t cmd) { sendCommandHeader(cmd); }
@@ -1722,7 +1725,6 @@
     uint8_t length, uint8_t * buffer);
     
     void sendCommandHeader(uint8_t cmd);
-    //void serialW7(uint8_t val) { _stream->write(val & 0x7F); }
     void serialW7(uint8_t val) { _stream->putc(val & 0x7F); }
 };