From ee1d24f77344c0a11b8c1fbaa050c0a99b69dd69 Mon Sep 17 00:00:00 2001 From: Nick Date: Sun, 1 Sep 2019 19:18:56 +0300 Subject: [PATCH] Initial commit --- .gitignore | 33 ++++++++++++++++++ Makefile | 40 ++++++++++++++++++++++ README.md | 25 ++++++++++++++ include/led.h | 4 +++ src/led.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 include/led.h create mode 100644 src/led.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7f04d67 --- /dev/null +++ b/.gitignore @@ -0,0 +1,33 @@ +*.o +dist + +# Object files +*.o +*.ko +*.obj +*.elf + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex +/.idea diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ccc891f --- /dev/null +++ b/Makefile @@ -0,0 +1,40 @@ +CC=arm-hisiv300-linux-gcc +USER_CFLAGS=-march=armv5te -mcpu=arm926ej-s -I/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/usr/include -L/opt/hisi-linux/x86-arm/arm-hisiv300-linux/target/usr/lib +USER_LDFLAGS= +AR=arm-hisiv300-linux-ar +RANLIB=arm-hisiv300-linux-ranlib +STRIP=arm-hisiv300-linux-strip + +TARGET = led + +BUILD_DIR = dist +SRC_DIR = src +INCLUDE_DIR = include + +CFLAGS = -Os +LIBS = + +.PHONY: default all clean directories + +default: directories $(TARGET) +all: default + +OBJECTS = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(wildcard $(SRC_DIR)/*.c)) +SOURCES = $(wildcard $(SRC_DIR)/*.c) +HEADERS = $(wildcard $(INCLUDE_DIR)/*.h) + +$(OBJECTS): $(SOURCES) $(HEADERS) + $(CC) $(CFLAGS) -c $< -o $@ + +.PRECIOUS: $(TARGET) $(OBJECTS) + +$(TARGET): $(OBJECTS) + $(CC) $(OBJECTS) -Wall $(LIBS) -o $(BUILD_DIR)/$@ + $(STRIP) $(BUILD_DIR)/$(TARGET) + +directories: + mkdir -p $(BUILD_DIR) + +clean: + rm -f $(BUILD_DIR)/*.o + rm -f $(BUILD_DIR)/$(TARGET) diff --git a/README.md b/README.md new file mode 100644 index 0000000..25fb1c9 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +## Led Control for Xiaomi's YI Home cameras + +Simple LED (light-emitting diode) control interface on `cpld_periph` kernel module for YI Home camera + +Supported cams: +* 720p 47US + +## Build + +```sh +$ cd yi-home-led +$ make +``` + +### Usage +```sh +Usage: led + +Options: + -b[fast|slow|pulse|on|off] Blue led mode + -y[fast|slow|on|off] Yellow led mode + -l[on|off] Light on/off + +Example: led -bon -lon +``` diff --git a/include/led.h b/include/led.h new file mode 100644 index 0000000..fba42de --- /dev/null +++ b/include/led.h @@ -0,0 +1,4 @@ +int32_t DEVICE_NUM = 0x70; + +int32_t open_cpld(); +void run2(int32_t fd, int offset); \ No newline at end of file diff --git a/src/led.c b/src/led.c new file mode 100644 index 0000000..79a1738 --- /dev/null +++ b/src/led.c @@ -0,0 +1,95 @@ +#include +#include +#include + +#include "../include/led.h" + +int32_t open_cpld() { + int32_t fd; + if (access("/dev/cpld_periph", F_OK) == 0) { + fd = open("/dev/cpld_periph", O_RDWR); + } else { + fd = -1; + } + return fd; +} + +void usage() { + puts("Led Control v0.1\n\nUsage: led \n\nOptions: \n\t-b[fast|slow|pulse|on|off] Blue led mode\n\t-y[fast|slow|on|off] Yellow led mode\n\t-l[on|off] Light on/off \n\nExample: led -bon -lon\n"); +} + +void run2(int32_t fd, int offset) { + ioctl(fd, (DEVICE_NUM << 8) + offset ); +} + +int main(int argc, char *argv[]) { + if (argc <= 1) { + usage(); + return 1; + } + + int32_t fd = open_cpld(); + if (fd < 0) { + puts("Error: cannot open /dev/cpld_periph"); + return 1; + } + + int opt; + while((opt = getopt(argc, argv, ":b:y:l:")) != -1) + { + switch (opt) { + case 'b': + if (strcmp(optarg, "on") == 0) { + run2(fd, 0x0a); + run2(fd, 0x01); + } + + if (strcmp(optarg, "off") == 0) { + run2(fd, 0x02); + } + + if (strcmp(optarg, "fast") == 0) { + run2(fd, 0x03); + } + + if (strcmp(optarg, "slow") == 0) { + run2(fd, 0x04); + } + + if (strcmp(optarg, "pulse") == 0) { + run2(fd, 0x05); + } + break; + case 'y': + if (strcmp(optarg, "on") == 0) { + run2(fd, 0x09); + run2(fd, 0x02); + } + if (strcmp(optarg, "fast") == 0) { + run2(fd, 0x0b); + } + + if (strcmp(optarg, "slow") == 0) { + run2(fd, 0x0c); + } + + if (strcmp(optarg, "off") == 0) { + run2(fd, 0x0a); + } + break; + case 'l': + if (strcmp(optarg, "on") == 0) { + run2(fd, 0x1c); + } + if (strcmp(optarg, "off") == 0) { + run2(fd, 0x1b); + } + break; + case '?': + usage(); + break; + } + } + + return 0; +} \ No newline at end of file