// IPアドレス表示
// eth0のアドレスを7セグメントLED一桁に一文字ずつ表示します。
// 7セグメントLEDは以下の様になっている。
//
// SEG WrIO pin
// --- a --- a 0 17
// | | b 1 12
// f b c 2 13
// | | d 3 15
// --- g --- e 4 16
// | | f 5 18
// e c g 6 22
// | | dot 7 7
// --- d ---
// dot
// 各セグメントをONする事で数字を表示させる。
// 例えば数字1はb,cセグメントなので、pin1,2をONさせれば良い。
#include < stdio.h >
#include < string.h >
#include < unistd.h >
#include < sys/types.h >
#include < sys/socket.h >
#include < arpa/inet.h >
#include < netdb.h >
#include < errno.h >
#include < linux/if.h >
#include < sys/ioctl.h >
#include < wiringPi.h >
const char* GetMyIpAddr(const char* device_name);
/**
* 自マシンのIPアドレスを取得します。
* @param デバイス名(e.g. "eth0")
* @return 「数値とドット」記法のアドレス
*/
const char* GetMyIpAddr(const char* device_name) {
//指定したデバイス名のIPアドレスを取得します。
int s = socket(AF_INET, SOCK_STREAM, 0);
struct ifreq ifr;
ifr.ifr_addr.sa_family = AF_INET;
strcpy(ifr.ifr_name, device_name);
ioctl(s, SIOCGIFADDR, &ifr);
close(s);
struct sockaddr_in addr;
memcpy( &addr, &ifr.ifr_ifru.ifru_addr, sizeof(struct sockaddr_in) );
return inet_ntoa(addr.sin_addr);
}
// 出力初期設定
void out_init(void){
pinMode (0, OUTPUT) ;
pinMode (1, OUTPUT) ;
pinMode (2, OUTPUT) ;
pinMode (3, OUTPUT) ;
pinMode (4, OUTPUT) ;
pinMode (5, OUTPUT) ;
pinMode (6, OUTPUT) ;
pinMode (7, OUTPUT) ;
}
// セグメント表示
void out_0(void){
digitalWrite (0, HIGH);
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, HIGH);
digitalWrite (4, HIGH);
digitalWrite (5, HIGH);
digitalWrite (6, LOW);
digitalWrite (7, LOW);
}
void out_1(void){
digitalWrite (0, LOW);
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, LOW);
digitalWrite (6, LOW);
digitalWrite (7, LOW);
}
void out_2(void){
digitalWrite (0, HIGH);
digitalWrite (1, HIGH);
digitalWrite (2, LOW);
digitalWrite (3, HIGH);
digitalWrite (4, HIGH);
digitalWrite (5, LOW);
digitalWrite (6, HIGH);
digitalWrite (7, LOW);
}
void out_3(void){
digitalWrite (0, HIGH);
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, HIGH);
digitalWrite (4, LOW);
digitalWrite (5, LOW);
digitalWrite (6, HIGH);
digitalWrite (7, LOW);
}
void out_4(void){
digitalWrite (0, LOW);
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, HIGH);
digitalWrite (6, HIGH);
digitalWrite (7, LOW);
}
void out_5(void){
digitalWrite (0, HIGH);
digitalWrite (1, LOW);
digitalWrite (2, HIGH);
digitalWrite (3, HIGH);
digitalWrite (4, LOW);
digitalWrite (5, HIGH);
digitalWrite (6, LOW);
digitalWrite (7, LOW);
}
void out_6(void){
digitalWrite (0, HIGH);
digitalWrite (1, LOW);
digitalWrite (2, HIGH);
digitalWrite (3, HIGH);
digitalWrite (4, HIGH);
digitalWrite (5, HIGH);
digitalWrite (6, HIGH);
digitalWrite (7, LOW);
}
void out_7(void){
digitalWrite (0, HIGH);
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, LOW);
digitalWrite (6, LOW);
digitalWrite (7, LOW);
}
void out_8(void){
digitalWrite (0, HIGH);
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, HIGH);
digitalWrite (4, HIGH);
digitalWrite (5, HIGH);
digitalWrite (6, HIGH);
digitalWrite (7, LOW);
}
void out_9(void){
digitalWrite (0, HIGH);
digitalWrite (1, HIGH);
digitalWrite (2, HIGH);
digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, HIGH);
digitalWrite (6, HIGH);
digitalWrite (7, LOW);
}
void out_d(void){
digitalWrite (0, LOW);
digitalWrite (1, LOW);
digitalWrite (2, LOW);
digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, LOW);
digitalWrite (6, LOW);
digitalWrite (7, HIGH);
}
void out_bar(void){
digitalWrite (0, LOW);
digitalWrite (1, LOW);
digitalWrite (2, LOW);
digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, LOW);
digitalWrite (6, HIGH);
digitalWrite (7, LOW);
}
void out_off(void){
digitalWrite (0, LOW);
digitalWrite (1, LOW);
digitalWrite (2, LOW);
digitalWrite (3, LOW);
digitalWrite (4, LOW);
digitalWrite (5, LOW);
digitalWrite (6, LOW);
digitalWrite (7, LOW);
}
void led_Out(char adrs_char){
switch(adrs_char){
case '0':
printf("0\n");
out_0();
break;
case '1':
printf("1\n");
out_1();
break;
case '2':
printf("2\n");
out_2();
break;
case '3':
printf("3\n");
out_3();
break;
case '4':
printf("4\n");
out_4();
break;
case '5':
printf("5\n");
out_5();
break;
case '6':
printf("6\n");
out_6();
break;
case '7':
printf("7\n");
out_7();
break;
case '8':
printf("8\n");
out_8();
break;
case '9':
printf("9\n");
out_9();
break;
case '.':
printf(".\n");
out_d();
break;
}
}
int main(){
wiringPiSetup () ;
out_init();
const char* r = GetMyIpAddr("eth0"); // ipアドレスの取得
const int a = strlen(r); // 文字長さ取得
for(;;){
int i; // ループカウンタ
for(i = 0; i <= a; i++) { // ループ
//printf("%c\n", r[i]); // 一文字ずつ表示
led_Out(r[i]);
delay(500);
out_off();
delay(250);
}
printf("\n");
out_off();
for(i = 0; i <= 10; i++){
delay(50);
out_bar();
delay(50);
out_off();
}
delay(500);
}
return 0;
}