1
+ #include < Wire . h>
2
+ #include < Sensing . h>
3
+ #include < Packet . h>
4
+ /*
5
+ PreDefines| PacketType | DataType
6
+ TYPEBOOL 1 Bool
7
+ TYPEINT 2 Int
8
+ TYPEFLOAT 3 Float
9
+ TYPELONG 4 Long int
10
+ TYPEBYTE 5 Byte
11
+ TYPETIME 6 unsigned long
12
+ */
13
+
14
+ // User Section: Defines ---------- ---------- ---------- ----------
15
+ #define PACKET_TYPE 2 // Use the above table as reference
16
+ #define DATA_TYPE int
17
+
18
+ #define DATA_UNIT " knock" // 5 char max
19
+
20
+ #define ADDRESS_TYPE false // True = Preset, False = pin based
21
+ #define ADDRESS 2 // Preset Value
22
+ #define ADDRESS_PINS 8 ,9 ,10 // Digital Pins to Address off of
23
+ // ---------- ---------- ---------- ---------- ---------- ----------
24
+
25
+ Packet<DATA_TYPE > g_packet;
26
+
27
+ unsigned long g_timeout;
28
+ int g_stage;
29
+
30
+ // User Section: Sensor Setup ---------- ---------- ----------
31
+ void input()
32
+ {
33
+ int tmp = analogRead(0 );
34
+ if (tmp > 0 )
35
+ {
36
+ if (g_stage == 3 )
37
+ g_stage = 0 ;
38
+ g_packet. SetReading (tmp);
39
+ g_packet. SetStatus (true );
40
+ }
41
+ else if ( g_stage == 0 )
42
+ {
43
+ g_stage = 3 ;
44
+ }
45
+ }
46
+ // ---------- ---------- ---------- ---------- ---------- ----------
47
+
48
+ void setup ()
49
+ {
50
+ #if ADDRESS_TYPE
51
+ SensingNodePreset (ADDRESS );
52
+ #else
53
+ SensingNodeAddress (ADDRESS_PINS );
54
+ #endif
55
+ g_packet. SetUnit (DATA_UNIT );
56
+
57
+ Serial . begin(9600 );
58
+ }
59
+
60
+ void loop ()
61
+ {
62
+ input();
63
+ }
64
+
65
+ // ////////////////////////////////////////////////////////////////////////
66
+ // Packet Sender
67
+ void SendPacket ()
68
+ {
69
+ // Tell hub the datatype
70
+ if (g_stage == 0 ){
71
+ Wire . send( PACKET_TYPE );
72
+ g_timeout = millis () + 100 ;
73
+
74
+ // IF TYPENUL, do not stage
75
+ if (PACKET_TYPE != TYPENULL )
76
+ g_stage = 1 ;
77
+ }
78
+ // Tell hub data
79
+ else if (g_stage == 1 )
80
+ {
81
+ if (g_timeout > millis ())
82
+ Wire . send((uint8_t * )& g_packet, sizeof(g_packet));
83
+ g_stage = 0 ;
84
+ }
85
+ // Tell Hub no data
86
+ else if (g_stage == 3 )
87
+ {
88
+ Wire . send( TYPENULL );
89
+ }
90
+ }
91
+ // Initializer: Preset Address
92
+ void SensingNodePreset (unsigned int addr)
93
+ {
94
+ g_stage = 0 ;
95
+ Wire . begin((int )addr);
96
+ Wire . onRequest( SendPacket );
97
+ }
98
+ // Initializer: Pin Based Address
99
+ void SensingNodeAddress (unsigned int pin0, unsigned int pin1, unsigned int pin2)
100
+ {
101
+ g_stage = 0 ;
102
+ // Activate Pins
103
+ pinMode(pin0, INPUT );
104
+ pinMode(pin1, INPUT );
105
+ pinMode(pin2, INPUT );
106
+
107
+ // activate internal pullup resistors
108
+ digitalWrite(pin0, HIGH );
109
+ digitalWrite(pin1, HIGH );
110
+ digitalWrite(pin2, HIGH );
111
+
112
+ bool p0 = digitalRead(pin0), p1 = digitalRead(pin1), p2 = digitalRead(pin2);
113
+
114
+ unsigned int plugAddr= 0 ;
115
+
116
+ if (! digitalRead(pin0))
117
+ plugAddr += 1 ;
118
+
119
+ if (! digitalRead(pin1))
120
+ plugAddr += 2 ;
121
+
122
+ if (! digitalRead(pin2))
123
+ plugAddr += 4 ;
124
+
125
+ Serial . print(" [pinAddr]: " );
126
+ Serial . println(plugAddr);
127
+
128
+ Wire . begin((int )plugAddr + I2C_ADDRESS_BASE );
129
+ Wire . onRequest( SendPacket );
130
+ }
0 commit comments