Skip to content

Instantly share code, notes, and snippets.

@hpsaturn
Created November 1, 2016 23:19
Show Gist options
  • Save hpsaturn/2606cd25e1ed74e448270a18eb7ea1b4 to your computer and use it in GitHub Desktop.
Save hpsaturn/2606cd25e1ed74e448270a18eb7ea1b4 to your computer and use it in GitHub Desktop.
#include <unistd.h>
#include <iostream>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <cstring>
#include <arpa/inet.h>
#include "../cpp/driver/everloop_image.h"
#include "../cpp/driver/everloop.h"
#include "../cpp/driver/wishbone_bus.h"
namespace hal = matrix_hal;
struct sockaddr_in sa;
char str[INET_ADDRSTRLEN];
int last_integer = 0;
void setNumber(int position, int value, hal::EverloopImage *image) {
image->leds[position].blue = 0;
image->leds[position].green = 20;
image->leds[position].red = 40;
for (int i = 0; i < value; i++) {
image->leds[i + position + 1].blue = 0;
image->leds[i + position + 1].blue = 30;
}
}
int main() {
hal::WishboneBus bus;
bus.SpiInit();
hal::Everloop everloop;
hal::EverloopImage image;
everloop.Setup(&bus);
struct ifaddrs *ifAddrStruct = NULL;
struct ifaddrs *ifa = NULL;
void *tmpAddrPtr = NULL;
getifaddrs(&ifAddrStruct);
for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
if (!ifa->ifa_addr) {
continue;
}
if (ifa->ifa_addr->sa_family == AF_INET) { // check it is IP4
// is a valid IP4 Address
tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
printf("%s IP Address %s\n", ifa->ifa_name, addressBuffer);
if (std::strcmp(ifa->ifa_name, "wlan0") == 0) {
in_addr_t address = inet_addr(addressBuffer);
last_integer = (address >> 24) & 0xFF;
printf("show last integer: %i\n", last_integer);
}
}
}
if (ifAddrStruct != NULL)
freeifaddrs(ifAddrStruct);
for (hal::LedValue &led : image.leds) {
led.red = 0;
led.green = 0;
led.blue = 0;
led.white = 0;
}
everloop.Write(&image);
int first_digit = (int)last_integer / 100;
int second_digit = (int)(last_integer % 100) / 10;
int third_digit = (int)(last_integer % 100) % 10;
setNumber(0, first_digit, &image);
everloop.Write(&image);
setNumber(first_digit + 1, second_digit, &image);
everloop.Write(&image);
setNumber(first_digit + second_digit + 2, third_digit, &image);
everloop.Write(&image);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment