Did a few exersises, most of the way through serial_logger.
This commit is contained in:
parent
1f4cc71b96
commit
97fb8de5da
8 changed files with 331 additions and 20 deletions
65
function_pointer/Makefile
Normal file
65
function_pointer/Makefile
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
CC := gcc
|
||||
CXX := g++
|
||||
OPTIMIZATION := -O3
|
||||
CFLAGS = ${OPTIMIZATION} -Werror -Wextra -Wall -Wpedantic
|
||||
# all: helloworld
|
||||
#
|
||||
# #automatically built from implicit rules.
|
||||
# helloworld: helloworld.c
|
||||
#
|
||||
# clean:
|
||||
# rm -f helloworld
|
||||
#
|
||||
TARGET_EXEC := functionpointer
|
||||
|
||||
BUILD_DIR := ./build
|
||||
SRC_DIRS := ./src
|
||||
|
||||
# Find all the C and C++ files we want to compile
|
||||
# Note the single quotes around the * expressions. The shell will incorrectly expand these otherwise, but we want to send the * directly to the find command.
|
||||
SRCS := $(shell find $(SRC_DIRS) -name '*.cpp' -or -name '*.c' -or -name '*.s')
|
||||
|
||||
# Prepends BUILD_DIR and appends .o to every src file
|
||||
# As an example, ./your_dir/hello.cpp turns into ./build/./your_dir/hello.cpp.o
|
||||
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
|
||||
|
||||
# String substitution (suffix version without %).
|
||||
# As an example, ./build/hello.cpp.o turns into ./build/hello.cpp.d
|
||||
DEPS := $(OBJS:.o=.d)
|
||||
|
||||
# Every folder in ./src will need to be passed to GCC so that it can find header files
|
||||
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
|
||||
# Add a prefix to INC_DIRS. So moduleA would become -ImoduleA. GCC understands this -I flag
|
||||
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
|
||||
|
||||
# The -MMD and -MP flags together generate Makefiles for us!
|
||||
# These files will have .d instead of .o as the output.
|
||||
DEPFLAGS := $(INC_FLAGS) -MMD -MP
|
||||
|
||||
# The final build step.
|
||||
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
|
||||
$(CXX) $(OBJS) -o $@ $(LDFLAGS)
|
||||
|
||||
# Build step for C source
|
||||
$(BUILD_DIR)/%.c.o: %.c
|
||||
mkdir -p $(dir $@)
|
||||
$(CC) $(DEPFLAGS) $(CFLAGS) -c $< -o $@
|
||||
|
||||
# Build step for C++ source
|
||||
$(BUILD_DIR)/%.cpp.o: %.cpp
|
||||
mkdir -p $(dir $@)
|
||||
$(CXX) $(DEPFLAGS) $(CXXFLAGS) -c $< -o $@
|
||||
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -r $(BUILD_DIR)
|
||||
|
||||
.PHONY: run
|
||||
run: $(BUILD_DIR)/$(TARGET_EXEC)
|
||||
$(BUILD_DIR)/$(TARGET_EXEC)
|
||||
|
||||
# Include the .d makefiles. The - at the front suppresses the errors of missing
|
||||
# Makefiles. Initially, all the .d files will be missing, and we don't want those
|
||||
# errors to show up.
|
||||
-include $(DEPS)
|
||||
26
function_pointer/src/function_pointer.c
Normal file
26
function_pointer/src/function_pointer.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void function0(void) { printf("function 0\n"); }
|
||||
void function1(void) { printf("function 1\n"); }
|
||||
void function2(void) { printf("function 2\n"); }
|
||||
typedef void (*VoidToVoid)(void);
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
VoidToVoid functions[3] = {function0, function1, function2};
|
||||
if (argc != 2) {
|
||||
printf("one argument only\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
unsigned char index = strtol(argv[1], NULL, 10);
|
||||
|
||||
if (index > 2) {
|
||||
printf("argument must be between 0 and 2\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
functions[index]();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue