Skip to content

Instantly share code, notes, and snippets.

@djungelorm
Created November 14, 2018 22:55
Show Gist options
  • Save djungelorm/1d4278e12d9a37f88642a5f6e065ac1b to your computer and use it in GitHub Desktop.
Save djungelorm/1d4278e12d9a37f88642a5f6e065ac1b to your computer and use it in GitHub Desktop.
kRPC fixes for Arduino Due
#include <krpc/communication.h>
#include <krpc/error.h>
#if defined(KRPC_COMMUNICATION_POSIX)
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
krpc_error_t krpc_open(krpc_connection_t * connection, const krpc_connection_config_t * arg) {
const char * port = arg;
int fd = open(port, O_RDWR | O_NOCTTY);
if (fd < 0)
KRPC_RETURN_ERROR(IO, "failed to open serial port");
*connection = fd;
return KRPC_OK;
}
krpc_error_t krpc_close(krpc_connection_t connection) {
close(connection);
return KRPC_OK;
}
krpc_error_t krpc_read(krpc_connection_t connection, uint8_t * buf, size_t count) {
size_t total = 0;
while (total < count) {
int result = read(connection, buf, count);
if (result == -1) {
KRPC_RETURN_ERROR(IO, "read failed");
} else if (result == 0) {
KRPC_RETURN_ERROR(EOF, "eof received");
} else {
total += result;
}
}
return KRPC_OK;
}
krpc_error_t krpc_write(krpc_connection_t connection, const uint8_t * buf, size_t count) {
if (count != write(connection, buf, count))
KRPC_RETURN_ERROR(IO, "write failed");
return KRPC_OK;
}
#endif
#if defined(KRPC_COMMUNICATION_ARDUINO)
krpc_error_t krpc_open(krpc_connection_t * connection, const krpc_connection_config_t * arg) {
if (arg)
(*connection)->begin(arg->speed);
else
(*connection)->begin(9600);
while (!*connection) {
}
return KRPC_OK;
}
krpc_error_t krpc_close(krpc_connection_t connection) {
connection->end();
return KRPC_OK;
}
krpc_error_t krpc_read(krpc_connection_t connection, uint8_t * buf, size_t count) {
size_t read = 0;
while (true) {
read += connection->readBytes(buf+read, count-read);
if (read == count)
return KRPC_OK;
}
}
krpc_error_t krpc_write(krpc_connection_t connection, const uint8_t * buf, size_t count) {
if (count != connection->write(buf, count))
KRPC_RETURN_ERROR(IO, "write failed");
return KRPC_OK;
}
#endif // KRPC_PLATFORM_ARDUINO
#pragma once
#include <USB/USBAPI.h>
#include <krpc/error.h>
#include <stddef.h>
#include <stdint.h>
#if !defined(KRPC_COMMS_CUSTOM)
#if defined(ARDUINO)
#define KRPC_COMMUNICATION_ARDUINO
#ifndef __cplusplus
#error "Require a C++ compiler to build kRPC for Arduino"
#endif
#include <HardwareSerial.h>
#elif !defined(KRPC_COMMS_CUSTOM)
#define KRPC_COMMUNICATION_POSIX
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef KRPC_COMMUNICATION_POSIX
typedef int krpc_connection_t;
typedef char krpc_connection_config_t;
#endif
#ifdef KRPC_COMMUNICATION_ARDUINO
typedef Serial_ * krpc_connection_t;
typedef struct {
uint32_t speed;
uint8_t config;
} krpc_connection_config_t;
#endif
/* Open a connection */
krpc_error_t krpc_open(krpc_connection_t * connection, const krpc_connection_config_t * config);
/* Close a connection */
krpc_error_t krpc_close(krpc_connection_t connection);
/* Read count bytes of data from the connection into buf */
krpc_error_t krpc_read(krpc_connection_t connection, uint8_t * buf, size_t count);
/* Write count bytes of data from into buf to the connection */
krpc_error_t krpc_write(krpc_connection_t connection, const uint8_t * buf, size_t count);
#ifdef __cplusplus
} // extern "C"
#endif
#include <krpc.h>
#include <krpc/services/krpc.h>
#include <krpc/services/space_center.h>
Serial_ * conn; // <- weirdly the type for SerialUSB is "Serial_"
void setup() {
conn = &SerialUSB;
// Open the serial port connection
krpc_open(&conn, NULL);
// Set up communication with the server
krpc_connect(conn, "Arduino Example");
...
}
void loop() {
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment