Created
September 24, 2016 01:06
-
-
Save dries007/5639fa9fbcea691320fddf4b3b70f037 to your computer and use it in GitHub Desktop.
echo IP > leds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * ip_leds | |
| * | |
| * Copyright (c) 2016 Dries007 | |
| * License: MIT | |
| * | |
| * Display some random effects until an IP is got, | |
| * then show the IP in binary on the leds. | |
| * | |
| * Library: | |
| * wiringPi | |
| * | |
| * Pinout: | |
| * LEDS 0->7 are on wiringPi pins 0-7 EXCEPT led 6, its on pin 15. | |
| * | |
| * | |
| * Compile with gnu11 & lib wiringPi: | |
| * gcc -std=gnu11 -Wall -0 pi_leds ip_leds.c -lwiringPi | |
| */ | |
| #include <stdio.h> | |
| #include <wiringPi.h> | |
| #include <signal.h> | |
| #include <arpa/inet.h> | |
| #include <sys/socket.h> | |
| #include <netdb.h> | |
| #include <ifaddrs.h> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <unistd.h> | |
| #include <string.h> | |
| #define I_FACE "wlan0" | |
| #define TIME_BINARY 5000 | |
| #define TIME_POSITION 1000 | |
| #define EFFECTS (sizeof(effects) / sizeof(fn_effect_t)) | |
| typedef void (*fn_effect_t)(int delay_ms, uint8_t invert); | |
| fn_effect_t effects[]; | |
| void writeByteFix(uint8_t data, int wait_ms); | |
| void blink(uint8_t data, int time_on, int time_off, int count); | |
| void rnd_effect(); | |
| uint32_t getIpv4(const char *name); | |
| void intHandler(int dummy) | |
| { | |
| writeByteFix(0x00, 0); | |
| exit(0); | |
| } | |
| int main(void) | |
| { | |
| wiringPiSetup(); | |
| for (int i = 0; i < 8; i++) pinMode(i != 6 ? i : 15, OUTPUT); | |
| writeByteFix(0x00, 0); | |
| signal(SIGINT, intHandler); | |
| while (1) | |
| { | |
| rnd_effect(); | |
| uint32_t ip = getIpv4(I_FACE); | |
| if (ip == 0 || ip == -1) | |
| { | |
| printf("No ip.\n"); | |
| continue; | |
| } | |
| printf("IP: %d.%d.%d.%d\n", 0xFF & (ip >> 24), 0xFF & (ip >> 16), 0xFF & (ip >> 8), 0xFF & (ip >> 0)); | |
| blink(1 << 3, 1, 2, TIME_POSITION); writeByteFix(0xFF & (ip >> 24), TIME_BINARY); | |
| blink(1 << 2, 1, 2, TIME_POSITION); writeByteFix(0xFF & (ip >> 16), TIME_BINARY); | |
| blink(1 << 1, 1, 2, TIME_POSITION); writeByteFix(0xFF & (ip >> 8), TIME_BINARY); | |
| blink(1 << 0, 1, 2, TIME_POSITION); writeByteFix(0xFF & (ip >> 0), TIME_BINARY); | |
| } | |
| } | |
| // Implementations | |
| void writeByteFix(uint8_t data, int wait_ms) | |
| { | |
| // Really? What is this madness | |
| digitalWriteByte(data & 0b10111111); | |
| digitalWrite(15, data & 0b01000000); | |
| delay(wait_ms); | |
| } | |
| uint32_t getIpv4(const char *name) | |
| { | |
| struct ifaddrs *ifaddr; | |
| uint32_t out = 0; | |
| if (getifaddrs(&ifaddr) == -1) return -1; | |
| for (struct ifaddrs * ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) | |
| { | |
| if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET) continue; | |
| if (strcmp(ifa->ifa_name, "wlan0") == 0) | |
| { | |
| out = ntohl(((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr); | |
| break; | |
| } | |
| } | |
| freeifaddrs(ifaddr); | |
| return out; | |
| } | |
| void blink(uint8_t data, int time_on, int time_off, int count) | |
| { | |
| while (--count > 0) | |
| { | |
| writeByteFix(data, time_on); | |
| writeByteFix(0x00, time_off); | |
| } | |
| } | |
| void effect_shift_l(int delay_ms, uint8_t invert) | |
| { | |
| for (int i = 0; i < 8; i++) | |
| writeByteFix(invert ? ~(1 << i) : 1 << i, delay_ms); | |
| } | |
| void effect_shift_r(int delay_ms, uint8_t invert) | |
| { | |
| for (int i = 7; i >= 0; i--) | |
| writeByteFix(invert ? ~(1 << i) : 1 << i, delay_ms); | |
| } | |
| void effect_knight_rider(int delay_ms, uint8_t invert) | |
| { | |
| for (int i = 0; i < 6; i++) | |
| writeByteFix(invert ? ~(0b00000111 << i) : 0b00000111 << i, delay_ms); | |
| for (int i = 5; i >= 0; i--) | |
| writeByteFix(invert ? ~(0b00000111 << i) : 0b00000111 << i, delay_ms); | |
| } | |
| void effect_fill(int delay_ms, uint8_t invert) | |
| { | |
| for (int i = 0; i < 0x100; i = i * 2 + 1) | |
| writeByteFix(invert ? ~i : i, delay_ms); | |
| } | |
| void effect_empty(int delay_ms, uint8_t invert) | |
| { | |
| for (int i = 0xFF; i > 0; i >>= 1) | |
| writeByteFix(invert ? ~i : i, delay_ms); | |
| } | |
| void effect_toggle(int delay_ms, uint8_t invert) | |
| { | |
| writeByteFix(invert ? ~0xAA : 0xAA, delay_ms); | |
| writeByteFix(invert ? ~0x55 : 0x55, delay_ms); | |
| } | |
| fn_effect_t effects[] = {&effect_shift_l, &effect_shift_r, &effect_knight_rider, &effect_fill, &effect_empty, &effect_toggle}; | |
| void rnd_effect(void) | |
| { | |
| int effect_nr = rand() % EFFECTS; | |
| uint8_t invert = rand() % 2; | |
| for (int i = 2 + (rand() % 5); i >= 0; i--) effects[effect_nr](50, invert); | |
| writeByteFix(0x00, 500); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment