diff options
author | Christian Pointner <equinox@spreadspace.org> | 2016-05-04 20:29:32 +0200 |
---|---|---|
committer | Christian Pointner <equinox@spreadspace.org> | 2016-05-04 20:29:32 +0200 |
commit | 10fe451fed560b6c9cc16383f8b625f8a2741fac (patch) | |
tree | fd021c39496090c658631823269a2a9349edde74 /serial-pjon | |
parent | usb-pjon sending works now... recv still needs testing (diff) |
added serial pjon example... pjon receive works now as well
Diffstat (limited to 'serial-pjon')
-rw-r--r-- | serial-pjon/Makefile | 34 | ||||
-rw-r--r-- | serial-pjon/serial-pjon.cpp | 116 |
2 files changed, 150 insertions, 0 deletions
diff --git a/serial-pjon/Makefile b/serial-pjon/Makefile new file mode 100644 index 0000000..cf72c01 --- /dev/null +++ b/serial-pjon/Makefile @@ -0,0 +1,34 @@ +## +## spreadspace avr utils +## +## +## Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org> +## +## This file is part of spreadspace avr utils. +## +## spreadspace avr utils is free software: you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation, either version 3 of the License, or +## any later version. +## +## spreadspace avr utils is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>. +## + +NAME := serial-pjon +BOARD_TYPE := arduino2009v2 +OBJ := $(NAME).o +LIBS := util led serialio +CXX_LIBS := arduino-stub +EXTERNAL_LIBS := pjon +SPREADAVR_PATH := .. + +PJON_PATH := $(SPREADAVR_PATH)/contrib/PJON +PJON_OPTS := + +include $(SPREADAVR_PATH)/include.mk diff --git a/serial-pjon/serial-pjon.cpp b/serial-pjon/serial-pjon.cpp new file mode 100644 index 0000000..194edc7 --- /dev/null +++ b/serial-pjon/serial-pjon.cpp @@ -0,0 +1,116 @@ +/* + * spreadspace avr utils + * + * + * Copyright (C) 2013-2015 Christian Pointner <equinox@spreadspace.org> + * + * This file is part of spreadspace avr utils. + * + * spreadspace avr utils is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * any later version. + * + * spreadspace avr utils is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <avr/io.h> +#include <avr/wdt.h> +#include <avr/interrupt.h> +#include <avr/power.h> +#include <stdio.h> + +#include "util.h" +#include "led.h" +#include "serialio.h" + +#include "Arduino.h" +#include "PJON.h" + +PJON bus(8); // Bus connection to pin 8 + +void error_handler(uint8_t code, uint8_t data) { + if(code == CONNECTION_LOST) { + printf("Connection with device ID %d is lost.\r\n", data); + } + if(code == PACKETS_BUFFER_FULL) { + printf("Packet buffer is full, has now a length of %d\r\n", data); + printf("Possible wrong bus configuration!\r\n"); + printf("higher MAX_PACKETS in PJON.h if necessary.\r\n"); + } + if(code == MEMORY_FULL) { + printf("Packet memory allocation failed. Memory is full.\r\n"); + } + if(code == CONTENT_TOO_LONG) { + printf("Content is too long, length: %d\r\n", data); + } + if(code == ID_ACQUISITION_FAIL) { + printf("Can't acquire a free id %d\r\n", data); + } +} + +void recv_handler(uint8_t length, uint8_t *payload) { + if(length == 0) { + printf("got 0 byte message...\r\n"); + return; + } + led_on(); + printf("got message(%d bytes): '", length); + for(uint16_t i = 0; i < length; ++i) + putchar(payload[i]); + printf("'\r\n"); + delay(30); + led_off(); +} + + +void handle_cmd(uint8_t cmd) +{ + switch(cmd) { + case '1': case '2': case '3': + case '4': case '5': case '6': + case '7': case '8': case '9': bus.set_id(cmd - '0'); printf("device id is now: %d\r\n", cmd - '0'); break; + case 'b': bus.send(BROADCAST, "hello world", 11); break; + case 's': bus.send(9, "hi!", 3); break; + case '!': reset2bootloader(); break; + default: printf("error\r\n"); return; + } + printf("ok\r\n"); +} + +int main(void) +{ + MCUSR &= ~(1 << WDRF); + wdt_disable(); + + cpu_init(); + led_init(); + serialio_init(57600, 0); + sei(); + + arduino_init(); + bus.begin(); + bus.set_error(error_handler); + bus.set_receiver(recv_handler); + + for(;;) { + int16_t BytesReceived = serialio_bytes_received(); + while(BytesReceived > 0) { + int ReceivedByte = fgetc(stdin); + if(ReceivedByte != EOF) { + handle_cmd(ReceivedByte); + } + BytesReceived--; + } + + serialio_task(); + bus.update(); + bus.receive(50); + } +} |