init
This commit is contained in:
commit
d8ad2c205a
12 changed files with 253 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
build/
|
||||
cmake-build-debug/
|
19
CMakeLists.txt
Normal file
19
CMakeLists.txt
Normal file
|
@ -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})
|
10
config.h
Normal file
10
config.h
Normal file
|
@ -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
|
44
display.c
Normal file
44
display.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <wiringPi.h>
|
||||
|
||||
#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);
|
||||
}
|
4
display.h
Normal file
4
display.h
Normal file
|
@ -0,0 +1,4 @@
|
|||
#include <stdint.h>
|
||||
|
||||
void
|
||||
display_setup(void);
|
6
macros.h
Normal file
6
macros.h
Normal file
|
@ -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
|
12
main.c
Normal file
12
main.c
Normal file
|
@ -0,0 +1,12 @@
|
|||
#include <models/controller.h>
|
||||
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
controller *this = controller_read();
|
||||
controller_save(this);
|
||||
|
||||
return 0;
|
||||
}
|
94
models/controller.c
Normal file
94
models/controller.c
Normal file
|
@ -0,0 +1,94 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <uuid/uuid.h>
|
||||
|
||||
#include "controller.h"
|
||||
#include <macros.h>
|
||||
|
||||
|
||||
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;
|
||||
}
|
28
models/controller.h
Normal file
28
models/controller.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef CONTROLLER_CONTROLLER_H
|
||||
#define CONTROLLER_CONTROLLER_H
|
||||
|
||||
#include <uuid/uuid.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <config.h>
|
||||
#include <models/relay.h>
|
||||
|
||||
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
|
10
models/relay.c
Normal file
10
models/relay.c
Normal file
|
@ -0,0 +1,10 @@
|
|||
#include <stdlib.h>
|
||||
|
||||
#include "relay.h"
|
||||
|
||||
relay*
|
||||
relay_init(uint8_t index)
|
||||
{
|
||||
(void)index;
|
||||
return NULL;
|
||||
}
|
17
models/relay.h
Normal file
17
models/relay.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifndef CONTROLLER_RELAY_H
|
||||
#define CONTROLLER_RELAY_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <config.h>
|
||||
|
||||
typedef struct relay {
|
||||
uint8_t index;
|
||||
char name[128];
|
||||
uint16_t *schedule;
|
||||
} relay;
|
||||
|
||||
relay*
|
||||
relay_init(uint8_t index);
|
||||
|
||||
#endif //CONTROLLER_RELAY_H
|
7
wiring_debug.h
Normal file
7
wiring_debug.h
Normal file
|
@ -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
|
Loading…
Reference in a new issue