changeset 1:7b2d0a758e63

Add pcb and finish i2c slave
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Tue, 12 Jan 2016 22:28:22 +0100
parents 2b5078b37907
children 9f41bcaa3cee
files controller.ino pwm.cpp servo.fzz
diffstat 3 files changed, 36 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/controller.ino	Tue Jan 12 19:33:31 2016 +0100
+++ b/controller.ino	Tue Jan 12 22:28:22 2016 +0100
@@ -1,26 +1,52 @@
+#include <Wire.h>
 #include "pwm.h"
 
+
+const uint8_t I2C_ADDRESS = 0x09;
 const uint8_t MAX_PINS = 18;
-const uint32_t SERIAL_SPEED = 115200L;
 const uint32_t PWM_PERIOD = 20000L;
-volatile uint8_t *ports[18] = {
+
+volatile uint8_t *ports[MAX_PINS] = {
  &PORTD, &PORTD,
  &PORTD, &PORTD, &PORTD, &PORTD, &PORTD, &PORTD, &PORTB, &PORTB,
  &PORTB, &PORTB, &PORTB, &PORTB, &PORTC, &PORTC, &PORTC, &PORTC,
 };
-uint8_t pins[18] = {
+uint8_t pins[MAX_PINS] = {
     1, 0,
     2, 3, 4, 5, 6, 7, 0, 1,
     2, 3, 4, 5, 0, 1, 2, 3,
 };
 
-void setup() {
-    pwm_init(ports, pins, MAX_PINS, PWM_PERIOD);
-    for (uint8_t i = 0; i < MAX_PINS; ++i) {
-        pwm_set_phase_batch(i, 1500);
+
+void receive(int16_t count) {
+    union {
+        byte bytes[2];
+        uint16_t value;
+    } bytes2int;
+    uint8_t servo = Wire.read();
+    while (Wire.available() >= 2) {
+        if (servo >= MAX_PINS) {
+            return;
+        }
+        bytes2int.bytes[0] = Wire.read();
+        bytes2int.bytes[1] = Wire.read();
+        pwm_set_phase_batch(servo, bytes2int.value);
+        ++servo;
     }
     pwm_apply_batch();
 }
 
+
+void setup() {
+    pwm_init(ports, pins, MAX_PINS, PWM_PERIOD);
+    for (uint8_t i = 0; i < MAX_PINS; ++i) {
+        pwm_set_phase_batch(i, 0);
+    }
+    pwm_apply_batch();
+    Wire.begin(I2C_ADDRESS);
+    Wire.onReceive(receive);
+}
+
+
 void loop() {
 }
--- a/pwm.cpp	Tue Jan 12 19:33:31 2016 +0100
+++ b/pwm.cpp	Tue Jan 12 22:28:22 2016 +0100
@@ -74,9 +74,9 @@
  * check any new chips which you want to use this library with.
  */
 void pwm_init(volatile uint8_t *ports[],
-				uint8_t pins[],
-				uint8_t count,
-				uint32_t period) {
+              uint8_t pins[],
+              uint8_t count,
+              uint32_t period) {
 
 	_count = (count <= PWM_MAX_PINS ? count : PWM_MAX_PINS);
 
Binary file servo.fzz has changed