From d8ad2c205a0fcd72a13c96e26bb923dea9b806db Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Fri, 15 Nov 2019 01:23:43 +0100 Subject: [PATCH] init --- .gitignore | 2 + CMakeLists.txt | 19 +++++++++ config.h | 10 +++++ display.c | 44 +++++++++++++++++++++ display.h | 4 ++ macros.h | 6 +++ main.c | 12 ++++++ models/controller.c | 94 +++++++++++++++++++++++++++++++++++++++++++++ models/controller.h | 28 ++++++++++++++ models/relay.c | 10 +++++ models/relay.h | 17 ++++++++ wiring_debug.h | 7 ++++ 12 files changed, 253 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 config.h create mode 100644 display.c create mode 100644 display.h create mode 100644 macros.h create mode 100644 main.c create mode 100644 models/controller.c create mode 100644 models/controller.h create mode 100644 models/relay.c create mode 100644 models/relay.h create mode 100644 wiring_debug.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99e7714 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +cmake-build-debug/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..2dd0332 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required (VERSION 3.7) +project(controller) + +add_executable(controller main.c) + +option(WIRING_PI_DEBUG "Use WiringPi Debugging Tool" OFF) + +SET(CMAKE_C_FLAGS "-Wall -Wextra -lwiringPi -luuid -g") + +if(WIRING_PI_DEBUG) + add_compile_definitions("WIRING_PI_DEBUG") +endif(WIRING_PI_DEBUG) + + +aux_source_directory(. SRC_DIR) +aux_source_directory(models MODEL_SRC) + +target_sources(controller PRIVATE ${SRC_DIR} ${MODEL_SRC}) +target_include_directories(controller PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/config.h b/config.h new file mode 100644 index 0000000..c56f139 --- /dev/null +++ b/config.h @@ -0,0 +1,10 @@ +#ifndef CONTROLLER_CONFIG_H +#define CONTROLLER_CONFIG_H + +#define CONTROLLER_FILE_NAME "emgauwa-controller.dat" +#define CONTROLLER_FILE_VERSION 1 +#define CONTROLLER_FILE_HEADER_SIZE 4 + +#define CONTROLLER_NAME_LENGTH 128 + +#endif //CONTROLLER_CONFIG_H diff --git a/display.c b/display.c new file mode 100644 index 0000000..b98dea3 --- /dev/null +++ b/display.c @@ -0,0 +1,44 @@ +#include +#include +#include + +#ifdef WIRING_PI_DEBUG + #include "wiring_debug.h" +#endif + +#include "display.h" + +#define INPUT_A 22 +#define INPUT_B 23 +#define LOAD 24 +#define CLOCK 25 +#define PAUSE 1 + +#define BOARD_SIZE_X 64 +#define BOARD_SIZE_Y 40 +#define BOARD_PIXEL_COUNT BOARD_SIZE_X * BOARD_SIZE_Y + +#define BLOCK_SIZE_X 4 +#define BLOCK_SIZE_Y 8 +#define BLOCK_PIXEL_COUNT BLOCK_SIZE_X * BLOCK_SIZE_Y +#define BOARD_BLOCK_COUNT 80 + +uint32_t display_block_array[BOARD_BLOCK_COUNT]; + +void +display_setup(void) +{ + wiringPiSetup(); + + pinMode(INPUT_A, OUTPUT); + digitalWrite(INPUT_A, LOW); + + pinMode(INPUT_B, OUTPUT); + digitalWrite(INPUT_B, LOW); + + pinMode(LOAD, OUTPUT); + digitalWrite(LOAD, LOW); + + pinMode(CLOCK, OUTPUT); + digitalWrite(CLOCK, LOW); +} diff --git a/display.h b/display.h new file mode 100644 index 0000000..8dc80ff --- /dev/null +++ b/display.h @@ -0,0 +1,4 @@ +#include + +void +display_setup(void); diff --git a/macros.h b/macros.h new file mode 100644 index 0000000..6f6ca14 --- /dev/null +++ b/macros.h @@ -0,0 +1,6 @@ +#ifndef CONTROLLER_MACROS_H +#define CONTROLLER_MACROS_H + +#define LOG_ERROR(msg) printf("ERROR: \"%s\" in function %s in file %s:%d\n", msg, __func__, __FILE__, __LINE__) + +#endif //CONTROLLER_MACROS_H diff --git a/main.c b/main.c new file mode 100644 index 0000000..9e3cc83 --- /dev/null +++ b/main.c @@ -0,0 +1,12 @@ +#include + +int +main(int argc, char** argv) +{ + (void)argc; + (void)argv; + controller *this = controller_read(); + controller_save(this); + + return 0; +} diff --git a/models/controller.c b/models/controller.c new file mode 100644 index 0000000..f04008c --- /dev/null +++ b/models/controller.c @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include + +#include "controller.h" +#include + + +controller* +controller_create(void) +{ + controller *result = malloc(sizeof(*result)); + uuid_generate(result->uuid); + + strcpy(result->name, "new emgauwa device"); + result->port = 0; + result->relay_count = 10; + + result->relays = malloc(sizeof(*result->relays) * result->relay_count); + uint8_t i; + for(i = 0; i < result->relay_count; i++) + { + result->relays[i] = relay_init(i); + } + + return result; +} + +controller* +controller_read(void) +{ + FILE *fp; + controller *result;; + + fp = fopen(CONTROLLER_FILE_NAME, "rb"); + + if(fp == NULL) + { + LOG_ERROR("Error opening file"); + result = controller_create(); + + return result; + } + + uint8_t header[CONTROLLER_FILE_HEADER_SIZE]; + size_t read_size; + + read_size = fread(header, sizeof(*header), CONTROLLER_FILE_HEADER_SIZE, fp); + if(ferror(fp) || read_size != CONTROLLER_FILE_HEADER_SIZE) + { + LOG_ERROR("Error reading header"); + result = controller_create(); + + return result; + } + + uint8_t version = header[0]; + printf("%d\n", version); + + + // size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file); + + fclose(fp); + + result = controller_create(); + + return result; +} + +int +controller_save(controller* ci) +{ + FILE *fp; + + fp = fopen(CONTROLLER_FILE_NAME, "w+b"); + + if(fp == NULL) + { + LOG_ERROR("Error creating or opening file"); + return 1; + } + + uint8_t header[CONTROLLER_FILE_HEADER_SIZE]; + header[0] = CONTROLLER_FILE_VERSION; + + fwrite(header, sizeof(*header), CONTROLLER_FILE_HEADER_SIZE, fp); + fwrite(ci->name, sizeof(*ci->name), CONTROLLER_NAME_LENGTH, fp); + + fclose(fp); + + return 0; +} \ No newline at end of file diff --git a/models/controller.h b/models/controller.h new file mode 100644 index 0000000..e89f0fd --- /dev/null +++ b/models/controller.h @@ -0,0 +1,28 @@ +#ifndef CONTROLLER_CONTROLLER_H +#define CONTROLLER_CONTROLLER_H + +#include +#include + +#include +#include + +typedef struct controller { + uuid_t uuid; + char name[CONTROLLER_NAME_LENGTH]; + uint16_t port; + uint8_t relay_count; + relay **relays; + +} controller; + +controller* +controller_create(void); + +controller* +controller_read(void); + +int +controller_save(controller* ci); + +#endif //CONTROLLER_CONTROLLER_H \ No newline at end of file diff --git a/models/relay.c b/models/relay.c new file mode 100644 index 0000000..141cc11 --- /dev/null +++ b/models/relay.c @@ -0,0 +1,10 @@ +#include + +#include "relay.h" + +relay* +relay_init(uint8_t index) +{ + (void)index; + return NULL; +} diff --git a/models/relay.h b/models/relay.h new file mode 100644 index 0000000..2d8b710 --- /dev/null +++ b/models/relay.h @@ -0,0 +1,17 @@ +#ifndef CONTROLLER_RELAY_H +#define CONTROLLER_RELAY_H + +#include + +#include + +typedef struct relay { + uint8_t index; + char name[128]; + uint16_t *schedule; +} relay; + +relay* +relay_init(uint8_t index); + +#endif //CONTROLLER_RELAY_H diff --git a/wiring_debug.h b/wiring_debug.h new file mode 100644 index 0000000..6aaf85d --- /dev/null +++ b/wiring_debug.h @@ -0,0 +1,7 @@ +#ifdef WIRING_PI_DEBUG + +#define wiringPiSetup() printf("Setting up wiringPi....\n") +#define pinMode(x,y) printf("pinMode(%d, %d)\n",x,y) +#define digitalWrite(x,y) printf("digitalWrite(%d, %d)\n",x,y) + +#endif