-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_1.ino
194 lines (170 loc) · 5.27 KB
/
client_1.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "handle_data.h"
#define bus_pin 2
#define button_pin A1
uint8_t client_stt = 0b1111;
byte client_key = 3;
byte client_recive, work_recive;
uint16_t time_recive;
byte first_range, second_range;
bool have_click_button = false;
int time_pressent = millis(), time_current = millis();
int Time_change;
uint16_t Time_press = 0;
byte Work_send[4] = {
0b1010, // reset
0b1110, // checking client
0b1001, // finnal winner
0b1101 //start new loop
};
//test
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(bus_pin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(A1, INPUT_PULLUP);
digitalWrite(bus_pin, LOW);
}
void loop() {
// Serial.println(digitalRead(3));
// take_time_change();
// recive_mess(time_recive, client_recive, work_recive);
// handle_mess(client_recive, work_recive, time_recive);
// //Serial.println("here");
// time_recive = 0;
// client_recive = 0;
// work_recive = 0;
take_time_change();
if (!have_click_button && !digitalRead(button_pin)){
Time_press = Time_change;
have_click_button = true;
}
recive_mess(&client_recive, &work_recive, &time_recive, bus_pin);
//int start = millis();
//if (client_recive != 0){Serial.println(work_recive);}
reset_interrupt();
handle_mess(&client_recive, &work_recive, &time_recive);
}
uint16_t adjust_time(uint16_t time_change, uint16_t time_change_benchmark, uint16_t time_adjust){
return time_adjust;
}
void take_time_change(){
time_current = millis();
Time_change = time_current - time_pressent;
}
void light_me_up(byte pin, int delay_mili){
digitalWrite(pin, HIGH);
delay(delay_mili);
digitalWrite(pin, LOW);
}
void handle_mess(uint8_t *client, uint8_t *work, uint16_t *time){
if (*work == Work_send[3]){
time_pressent = millis(); // reset the time
Time_press = 0;
have_click_button = false;
}
if (*client == 0b1000 || *client == client_stt){
if (*work == Work_send[0]){
light_me_up(LED_BUILTIN, 50);
time_pressent = millis(); // reset the time
}
if (*work == Work_send[1]){
//light_me_up(LED_BUILTIN, 200);
// uint16_t need_send = convert_to_byte(send);
if (Time_press == 0){sending_mess(client_stt, Work_send[1], *time, bus_pin, true, true);}
else {sending_mess(client_stt, Work_send[1], Time_press, bus_pin, true, true); }
delayMicroseconds(10);
}
if (*work == Work_send[2]){
// Serial.println(*work);
light_me_up(LED_BUILTIN, 2000);
}
}
*client = 0;
*work = 0;
*time = 0;
}
uint16_t convert_to_byte(int value_converted){
uint16_t bitMask;
uint16_t value_return;
for (bitMask = 0x01; bitMask; bitMask <<= 1){
if (bitMask & value_converted){value_return |= bitMask;}
}
// Serial.println(value_return);
// Serial.println(sizeof(value_return));
return value_return;
}
void reset_interrupt(){
noInterrupts();
delayMicroseconds(5);
interrupts();
}
// void handle_mess(uint8_t &client, uint8_t &work, uint16_t &time){
// // for client board
// if (client == 0b1000 || client == client_stt){
// if (work == Work_send[0]){
// Is_in_work = true;
// time_pressent = millis(); // reset the time
// Time_press = 0;
// }
// if (work == Work_send[1]){
// uint16_t time_sending = adjust_time(Time_change, time, Time_press);
// sending_mess(3210, 1);
// //light_me_up(LED_BUILTIN, 1000);
// }
// }
// }
// void sending_mess(uint16_t time_sent, byte work_key){
// // time_set <= 2^12
// uint8_t information_send = client_stt << 4 ;
// information_send |= Work_send[work_key];
// split_argument(time_sent, first_range, second_range);
// //send work and client key
// write(information_send, bus_pin, true);
// //send time for 2 time
// delayMicroseconds(15);
// write(first_range, bus_pin, true);
// delayMicroseconds(12);
// write(second_range, bus_pin, true);
// // special after sending that must put it into recive wait
// pinMode(bus_pin, INPUT);
// digitalWrite(bus_pin, HIGH);
// Serial.println(first_range);
// Serial.println(second_range);
// }
// void recive_mess(uint16_t &time_rc, uint8_t &client_rc, uint8_t &work_rc){
// while (1){
// if (!digitalRead(bus_pin)){
// uint8_t information = read(bus_pin);
// uint8_t first_time = read(bus_pin);
// uint8_t second_time = read(bus_pin);
// time_rc = second_time << 8;
// time_rc |= first_time;
// split_byte(information, work_rc, client_rc);
// // Serial.println(information);
// break;
// }
// }
// }
// void split_argument(uint16_t value, uint8_t &first_byte, uint8_t &second_byte){
// uint8_t bitMask;
// first_byte = 0;
// second_byte = 0;
// for (bitMask = 0x01; bitMask; bitMask <<= 1){
// if (bitMask & value){first_byte |= bitMask;}
// }
// for (bitMask = 0x01; bitMask; bitMask <<= 1){
// if (bitMask<<8 & value){second_byte |= bitMask;}
// }
// }
// void split_byte(uint8_t byte_split, uint8_t &first_4b, uint8_t &second_4b){
// uint8_t bitMask;
// first_4b = 0;
// second_4b = 0;
// for (bitMask = 0x01; bitMask < 0x01<<4; bitMask <<= 1){
// if (bitMask & byte_split){first_4b |= bitMask;}
// }
// for (bitMask = 0x01; bitMask < 0x01<<4; bitMask <<= 1){
// if (bitMask<<4 & byte_split){second_4b |= bitMask;}
// }
// }