-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tiny.cpp
61 lines (55 loc) · 1.5 KB
/
Tiny.cpp
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
#include "Tiny.h"
#include "Wire.h"
//PRIVATE
void ATtinyExtender::write8(uint8_t reg, uint8_t data) {
Wire.beginTransmission(TINY24_ADDR);
Wire.write((byte)reg);
Wire.write((byte)data);
Wire.endTransmission();
}
uint8_t ATtinyExtender::read8(uint8_t reg) {
Wire.beginTransmission(TINY24_ADDR);
Wire.write((byte)reg);
Wire.endTransmission();
Wire.requestFrom(TINY24_ADDR, 1);
return Wire.read();
}
uint16_t ATtinyExtender::read16(uint8_t reg) {
Wire.beginTransmission(TINY24_ADDR);
Wire.write((byte)reg);
Wire.endTransmission();
Wire.requestFrom(TINY24_ADDR, 2);
return (Wire.read()<<8)|Wire.read();
}
//PUBLIC
ATtinyExtender::ATtinyExtender(void) {}
void ATtinyExtender::begin(void) {
Wire.begin(0, 2);
}
void ATtinyExtender::pinMode(uint8_t pin, uint8_t mode) {
ATtinyExtender::write8(pin-1, mode);
}
boolean ATtinyExtender::digitalRead(uint8_t pin) {
return ATtinyExtender::read8(8+((pin-1)&7));
}
void ATtinyExtender::digitalWrite(uint8_t pin, boolean onoff) {
ATtinyExtender::write8(16+((pin-1)&7), onoff);
}
uint16_t ATtinyExtender::analogRead(uint8_t pin) {
return ATtinyExtender::read16(23+pin);//24+pin-1
}
void ATtinyExtender::analogWrite(uint8_t pin, uint8_t value) {
uint8_t reg;
switch (pin) {
case 8: reg=29;break;
case TA4: reg=30;break;
case TA5: reg=31;break;
default: return;
}
ATtinyExtender::pinMode(pin, OUTPUT);
ATtinyExtender::write8(reg, value);
}
void ATtinyExtender::analogReference(uint8_t mode) {
ATtinyExtender::write8(32, mode);
}
ATtinyExtender Tiny = ATtinyExtender();