completed
This commit is contained in:
commit
285d34c72d
21
Makefile
Normal file
21
Makefile
Normal file
@ -0,0 +1,21 @@
|
||||
CC = gcc
|
||||
CFLAGS := -std=c99 -Wall -Wextra -Werror -pedantic -Wvla
|
||||
OBJ_RUN := src/student.c src/main.o src/pin_handler.o
|
||||
|
||||
all: run
|
||||
|
||||
prepare: src/main.o src/pin_handler.o
|
||||
|
||||
parser: src/parser.o
|
||||
@$(CC) $(CFLAGS) -o $@ $^
|
||||
@./parser ./student.ino
|
||||
|
||||
run: parser $(OBJ_RUN)
|
||||
@$(CC) -o main $(OBJ_RUN)
|
||||
@./main
|
||||
|
||||
%.o: %.c
|
||||
@$(CC) $(LFLAGS) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) src/*.o main parser
|
18
src/default_student.c
Normal file
18
src/default_student.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include "pin_handler.h"
|
||||
#include <stddef.h>
|
||||
|
||||
#define L1 1
|
||||
#define L2 2
|
||||
#define L3 3
|
||||
#define L4 4
|
||||
#define C1 5
|
||||
#define C2 6
|
||||
#define C3 7
|
||||
#define C4 8
|
||||
|
||||
|
||||
|
||||
void student(char *sequence, size_t *pointer)
|
||||
{
|
||||
size_t row = 0;
|
||||
size_t col = 0;
|
13
src/main.c
Normal file
13
src/main.c
Normal file
@ -0,0 +1,13 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "student.h"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *sequence = malloc(sizeof(char) * 20);
|
||||
size_t pointer = 0;
|
||||
student(sequence, &pointer);
|
||||
free(sequence);
|
||||
printf("The code was not found!\n");
|
||||
return 1;
|
||||
}
|
179
src/parser.c
Normal file
179
src/parser.c
Normal file
@ -0,0 +1,179 @@
|
||||
#define _GNU_SOURCE
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <err.h>
|
||||
|
||||
void parser(FILE *fd);
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc <= 1)
|
||||
{
|
||||
fprintf(stderr, "Usage: ./parser FILE\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
FILE *fd = fopen(argv[1], "r");
|
||||
if (!fd)
|
||||
{
|
||||
fprintf(stderr, "Error opening file '%s'\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
parser(fd);
|
||||
|
||||
fclose(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *get_id(char *buf, size_t buf_size, size_t *i)
|
||||
{
|
||||
size_t start_nb = 0;
|
||||
short previous_number = 0;
|
||||
for (; *i < buf_size; *i += 1)
|
||||
{
|
||||
if ((buf[*i] >= '0' && buf[*i] <= '9') || buf[*i] == 'L' || buf[*i] == 'C')
|
||||
{
|
||||
if (!start_nb)
|
||||
start_nb = *i;
|
||||
previous_number = 1;
|
||||
}
|
||||
else if (previous_number)
|
||||
{
|
||||
buf[*i] = 0;
|
||||
return buf + start_nb;
|
||||
}
|
||||
}
|
||||
errx(2, "Error when compiling, pin number not found in a digitalWrite");
|
||||
}
|
||||
|
||||
char *get_state(char *buf, size_t buf_size, size_t *i)
|
||||
{
|
||||
for (; *i < buf_size - 3; *i += 1)
|
||||
{
|
||||
if (*i < buf_size - 4 && !strncmp("HIGH", buf + *i, 4))
|
||||
{
|
||||
buf[*i + 4] = 0;
|
||||
return buf + *i;
|
||||
}
|
||||
if (!strncmp("LOW", buf + *i, 3))
|
||||
{
|
||||
buf[*i + 3] = 0;
|
||||
return buf + *i;
|
||||
}
|
||||
}
|
||||
errx(2, "Error when compiling, HIGH or LOW not found in a digitalWrite");
|
||||
}
|
||||
|
||||
void skip_end(char *buf, size_t buf_size, size_t *i)
|
||||
{
|
||||
while (*i < buf_size && buf[*i] != ';')
|
||||
*i += 1;
|
||||
*i += 1;
|
||||
}
|
||||
|
||||
size_t convert_fct(char *buf, size_t start, size_t buf_size, FILE *dst)
|
||||
{
|
||||
const size_t size_str = strlen("digitalWrite");
|
||||
if (buf_size - start < size_str)
|
||||
return start;
|
||||
size_t index = start;
|
||||
if (!strncmp("digitalWrite", (buf + start), size_str))
|
||||
{
|
||||
index += size_str + 1;
|
||||
char *id = get_id(buf, buf_size, &index);
|
||||
char *state = get_state(buf, buf_size, &index);
|
||||
state[0] = (state[0] == 'H') ? '1' : '0';
|
||||
fwrite("set_pin(", 1, 8, dst);
|
||||
fwrite(id, 1, strlen(id), dst);
|
||||
fwrite(", ", 1, 2, dst);
|
||||
fwrite(state, 1, 1, dst);
|
||||
fwrite(", &row, &col, sequence, pointer);", 1, 33, dst);
|
||||
skip_end(buf, buf_size, &index);
|
||||
}
|
||||
else if (!strncmp("delay", (buf + start), 5))
|
||||
{
|
||||
index = buf_size;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
void write_buf(char *buf, size_t start, size_t buf_size, FILE *dst, size_t *nb_bracket)
|
||||
{
|
||||
for (size_t i = start; i < buf_size; i++)
|
||||
{
|
||||
if (buf[i] == '}')
|
||||
*nb_bracket -= 1;
|
||||
else if (buf[i] == '{')
|
||||
*nb_bracket += 1;
|
||||
|
||||
if (nb_bracket != 0)
|
||||
{
|
||||
size_t index = convert_fct(buf, i, buf_size, dst);
|
||||
if (index < buf_size)
|
||||
fwrite(buf + index, 1, 1, dst);
|
||||
i = index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t find_loop(char *buf, size_t buf_size)
|
||||
{
|
||||
for (size_t i = 0; i < buf_size - 6; i++)
|
||||
{
|
||||
if (!strncmp("loop()", (buf + i), 6))
|
||||
{
|
||||
return i + 6;
|
||||
}
|
||||
}
|
||||
return buf_size + 1;
|
||||
}
|
||||
|
||||
FILE *init_student()
|
||||
{
|
||||
FILE *student = fopen("./src/student.c", "w+");
|
||||
FILE *default_student = fopen("./src/default_student.c", "r");
|
||||
if (!student)
|
||||
errx(1, "Failed to create file 'student.c'");
|
||||
if (!default_student)
|
||||
{
|
||||
fclose(student);
|
||||
errx(1, "Failed to open file 'default_student.c'");
|
||||
}
|
||||
int c;
|
||||
while ((c = fgetc(default_student)) != EOF)
|
||||
fputc(c, student);
|
||||
fclose (default_student);
|
||||
return student;
|
||||
}
|
||||
|
||||
void parser(FILE *fd)
|
||||
{
|
||||
char *buf = NULL;
|
||||
size_t buf_size = 0;
|
||||
ssize_t bytes_read = 0;
|
||||
size_t nb_bracket = -1;
|
||||
short in_loop = 0;
|
||||
FILE *student = init_student();
|
||||
while ((bytes_read = getline(&buf, &buf_size, fd)) != -1)
|
||||
{
|
||||
if (in_loop)
|
||||
{
|
||||
write_buf(buf, 0, bytes_read, student, &nb_bracket);
|
||||
}
|
||||
else
|
||||
{
|
||||
size_t index_loop = find_loop(buf, buf_size);
|
||||
if (index_loop != buf_size + 1)
|
||||
{
|
||||
in_loop = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (buf)
|
||||
free(buf);
|
||||
fclose(student);
|
||||
if (!in_loop)
|
||||
errx(2, "Could not find the main loop function");
|
||||
}
|
60
src/pin_handler.c
Normal file
60
src/pin_handler.c
Normal file
@ -0,0 +1,60 @@
|
||||
#include "pin_handler.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <err.h>
|
||||
|
||||
static void set_data(size_t *wire, size_t id_mat, short state, size_t *pointer)
|
||||
{
|
||||
// set multiple rows or cols at the same time resulting in a undefined
|
||||
// behavior, the result is then voided
|
||||
if (state && *wire != 0 && *wire != id_mat)
|
||||
{
|
||||
*pointer = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (state)
|
||||
*wire = id_mat;
|
||||
else
|
||||
*wire = 0;
|
||||
}
|
||||
|
||||
static void read_digit(char *sequence, size_t *pointer, size_t *row, size_t *col)
|
||||
{
|
||||
if (*row == 0 || *col == 0)
|
||||
return;
|
||||
|
||||
const char keyboard[4][4] = {
|
||||
{ '1', '2', '3', 'A'},
|
||||
{ '4', '5', '6', 'B'},
|
||||
{ '7', '8', '9', 'C'},
|
||||
{ '*', '0', '#', 'D'},
|
||||
};
|
||||
|
||||
sequence[*pointer] = keyboard[*row - 1][*col - 1];
|
||||
*pointer += 1;
|
||||
sequence[*pointer + 1] = 0;
|
||||
|
||||
if (keyboard[*row - 1][*col - 1] == '#')
|
||||
{
|
||||
if (!strcmp(CODE, sequence))
|
||||
{
|
||||
errx(0, "Code found: %s\n", sequence);
|
||||
}
|
||||
else
|
||||
*pointer = 0;
|
||||
}
|
||||
}
|
||||
|
||||
void set_pin(int id, short state, size_t *row, size_t *col, char *sequence, size_t *pointer)
|
||||
{
|
||||
if (id <= 4)
|
||||
{
|
||||
set_data(row, id, state, pointer);
|
||||
}
|
||||
else
|
||||
{
|
||||
set_data(col, id - 4, state, pointer);
|
||||
}
|
||||
read_digit(sequence, pointer, row, col);
|
||||
}
|
9
src/pin_handler.h
Normal file
9
src/pin_handler.h
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef PIN_HANDLER_H
|
||||
#define PIN_HANDLER_H
|
||||
|
||||
#define CODE "1#"
|
||||
|
||||
#include <stddef.h>
|
||||
void set_pin(int id, short state, size_t *row, size_t *col, char *sequence, size_t *pointer);
|
||||
|
||||
#endif // PIN_HANDLER_H
|
37
src/student.c
Normal file
37
src/student.c
Normal file
@ -0,0 +1,37 @@
|
||||
#include "pin_handler.h"
|
||||
#include <stddef.h>
|
||||
|
||||
#define L1 1
|
||||
#define L2 2
|
||||
#define L3 3
|
||||
#define L4 4
|
||||
#define C1 5
|
||||
#define C2 6
|
||||
#define C3 7
|
||||
#define C4 8
|
||||
|
||||
|
||||
|
||||
void student(char *sequence, size_t *pointer)
|
||||
{
|
||||
size_t row = 0;
|
||||
size_t col = 0;
|
||||
//set_pin(13, 1, &row, &col, sequence, pointer); // sets the digital pin 13 on
|
||||
// //set_pin(13, 0, &row, &col, sequence, pointer); // sets the digital pin 13 off
|
||||
// //for (size_t i = 0; i < 5; i++)
|
||||
//{
|
||||
// set_pin(13, 1, &row, &col, sequence, pointer);
|
||||
//}
|
||||
|
||||
// 1
|
||||
set_pin(C1, 1, &row, &col, sequence, pointer);
|
||||
set_pin(L1, 1, &row, &col, sequence, pointer);
|
||||
set_pin(C1, 0, &row, &col, sequence, pointer);
|
||||
set_pin(L1, 0, &row, &col, sequence, pointer);
|
||||
|
||||
// #
|
||||
set_pin(C3, 1, &row, &col, sequence, pointer);
|
||||
set_pin(L4, 1, &row, &col, sequence, pointer);
|
||||
|
||||
|
||||
}
|
7
src/student.h
Normal file
7
src/student.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef STUDENT_H
|
||||
#define STUDENT_H
|
||||
|
||||
#include <stddef.h>
|
||||
void student(char *sequence, size_t *pointer);
|
||||
|
||||
#endif // STUDENT_H
|
29
student.ino
Normal file
29
student.ino
Normal file
@ -0,0 +1,29 @@
|
||||
#define L1 2
|
||||
#define L2 3
|
||||
#define L3 4
|
||||
#define L4 5
|
||||
#define C1 6
|
||||
#define C2 7
|
||||
#define C3 8
|
||||
#define C4 9
|
||||
|
||||
void setup() {
|
||||
// to complete
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// 1
|
||||
digitalWrite(C1, HIGH);
|
||||
digitalWrite(L1, HIGH);
|
||||
|
||||
delay(100);
|
||||
|
||||
digitalWrite(C1, LOW);
|
||||
digitalWrite(L1, LOW);
|
||||
|
||||
delay(100);
|
||||
|
||||
// #
|
||||
digitalWrite(C3, HIGH);
|
||||
digitalWrite(L4, HIGH);
|
||||
}
|
Loading…
Reference in New Issue
Block a user