113 lines
2.8 KiB
Arduino
113 lines
2.8 KiB
Arduino
|
/*
|
||
|
* worked on: arduino1.8.3 - arduino nano 328
|
||
|
* libs:
|
||
|
* https://github.com/avishorp/TM1637
|
||
|
* https://github.com/UIPEthernet/UIPEthernet
|
||
|
*
|
||
|
*/
|
||
|
#define l 3
|
||
|
#define r 4
|
||
|
#define CLK 5
|
||
|
#define DIO 6
|
||
|
#include <UIPEthernet.h>
|
||
|
#include <TM1637Display.h>
|
||
|
|
||
|
EthernetServer server = EthernetServer(1234);
|
||
|
TM1637Display display(CLK, DIO);
|
||
|
|
||
|
unsigned char step = 0;
|
||
|
const uint8_t empty[] = {0x00,0x00,0x00,0x00};
|
||
|
const uint8_t connected[] = {
|
||
|
SEG_D | SEG_E | SEG_G, //c
|
||
|
SEG_C | SEG_D | SEG_E | SEG_G,//o
|
||
|
SEG_C | SEG_E | SEG_G, //n
|
||
|
SEG_C | SEG_E | SEG_G //n
|
||
|
};
|
||
|
|
||
|
void setup() {
|
||
|
display.setBrightness(0, false); // Turn off
|
||
|
display.setSegments(empty);
|
||
|
|
||
|
Serial.begin(9600);
|
||
|
|
||
|
uint8_t mac[6] = {0x24, 0xb6, 0xfd, 0x20, 0x69, 0xed };
|
||
|
Ethernet.begin(mac);
|
||
|
|
||
|
pinMode(l, INPUT);
|
||
|
pinMode(r, INPUT);
|
||
|
|
||
|
Serial.println(Ethernet.localIP());
|
||
|
server.begin();
|
||
|
}
|
||
|
|
||
|
void loop() {
|
||
|
size_t size;
|
||
|
Serial.println("Listening port 1234 ");
|
||
|
Serial.println(Ethernet.localIP());
|
||
|
if (EthernetClient client = server.available()) {
|
||
|
client.write('h');
|
||
|
Serial.println("Connected");
|
||
|
bool leftp = digitalRead(l) == HIGH;
|
||
|
bool rightp = digitalRead(r) == HIGH;
|
||
|
bool conn = true;
|
||
|
while(client.connected()) {
|
||
|
if(conn) {
|
||
|
conn = false;
|
||
|
display.setBrightness(2, true);
|
||
|
display.setSegments(connected);
|
||
|
}
|
||
|
if(client.available() > 0) {
|
||
|
char data = client.read();
|
||
|
Serial.print(data);
|
||
|
if(data == 'd') {
|
||
|
Serial.println("Disconnect");
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
if(digitalRead(l) == HIGH) {
|
||
|
if(!leftp) {
|
||
|
leftp = true;
|
||
|
client.write('l');
|
||
|
client.write('<');
|
||
|
client.flush();
|
||
|
}
|
||
|
} else if(leftp) {
|
||
|
leftp = false;
|
||
|
client.write('l');
|
||
|
client.write('>');
|
||
|
client.flush();
|
||
|
}
|
||
|
|
||
|
if(digitalRead(r) == HIGH) {
|
||
|
if(!rightp) {
|
||
|
rightp = true;
|
||
|
client.write('r');
|
||
|
client.write('<');
|
||
|
client.flush();
|
||
|
}
|
||
|
} else if(rightp) {
|
||
|
rightp = false;
|
||
|
client.write('r');
|
||
|
client.write('>');
|
||
|
client.flush();
|
||
|
}
|
||
|
delay(2);
|
||
|
//Serial.print(digitalRead(l));
|
||
|
//Serial.println(digitalRead(r));
|
||
|
}
|
||
|
client.stop();
|
||
|
}
|
||
|
delay(1000);
|
||
|
if(digitalRead(l) == HIGH && digitalRead(r) == HIGH) { // both pressed
|
||
|
display.setBrightness(7, true); // Turn on
|
||
|
display.showNumberDec(Ethernet.localIP()[step++], step == 0);//to make the first byte have leading zeros
|
||
|
step%=4;
|
||
|
} else {
|
||
|
display.setBrightness(0, false); // Turn off
|
||
|
display.setSegments(empty);
|
||
|
step = 0;
|
||
|
}
|
||
|
}
|