forked from mikaelpatel/Arduino-OWI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Arduino.ino
103 lines (98 loc) · 2.28 KB
/
Arduino.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "GPIO.h"
#include "OWI.h"
#include "Slave/OWI.h"
#include "Driver/Arduino.h"
/** One-Wire Slave Device Bus Manager. */
Slave::OWI<BOARD::D7> owi(Arduino::FAMILY_CODE);
/**
* Setup One-Wire Remote Arduino Slave Device. Set alarm
* state to allow alarm search before reading analog input.
*/
void setup()
{
owi.alarm(true);
}
/**
* Service incoming one-wire rom and remote arduino functions.
*/
void loop()
{
// Additional sketch code could be placed here before
// polling one-wire bus for commands
if (!owi.rom_command()) return;
const size_t BUF_MAX = 32;
uint8_t buf[BUF_MAX];
uint16_t value;
uint8_t pin;
uint8_t mode;
uint8_t* bp;
uint8_t count;
uint8_t res;
uint8_t duty;
// Read and dispatch remote arduino commands
switch (owi.read_command()) {
case Arduino::PIN_MODE:
pin = owi.read(6);
mode = owi.read(2);
pinMode(pin, mode);
break;
case Arduino::DIGITAL_READ:
pin = owi.read(6);
value = digitalRead(pin);
owi.write(value, 1);
break;
case Arduino::DIGITAL_WRITE:
pin = owi.read(6);
value = owi.read(1);
digitalWrite(pin, value);
break;
case Arduino::ANALOG_READ:
pin = owi.read(6);
value = analogRead(pin);
owi.alarm(value > 512);
owi.write(&value, sizeof(value));
break;
case Arduino::ANALOG_WRITE:
pin = owi.read(6);
duty = owi.read();
analogWrite(pin, duty);
break;
case Arduino::SRAM_READ:
owi.read(&bp, sizeof(bp));
count = owi.read();
owi.write(bp, count);
break;
case Arduino::SRAM_WRITE:
owi.read(&bp, sizeof(bp));
count = owi.read();
owi.read(bp, count + 1);
break;
case Arduino::EEPROM_READ:
owi.read(&bp, sizeof(bp));
count = owi.read();
owi.crc(0);
while (count--) owi.write(eeprom_read_byte(bp++));
owi.write(owi.crc());
break;
case Arduino::EEPROM_WRITE:
owi.read(&bp, sizeof(bp));
count = owi.read();
res = 0b00;
if (count > BUF_MAX) {
while (count--) owi.read();
owi.read();
}
else if (owi.read(buf, count)) {
eeprom_write_block(buf, bp, count);
res = 0b10;
}
owi.write(res, 2);
break;
case Arduino::DIGITAL_PINS:
owi.write(NUM_DIGITAL_PINS, 6);
break;
case Arduino::ANALOG_PINS:
owi.write(NUM_ANALOG_INPUTS, 6);
break;
}
}