-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
88 lines (67 loc) · 2.23 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-License-Identifier: MIT
// Copyright 2024 Algovoid
#include "wokwi-api.h"
#include <stdio.h>
#include <stdlib.h>
typedef struct {
pin_t pin_1A;
pin_t pin_1B;
pin_t pin_1Y;
pin_t pin_2A;
pin_t pin_2B;
pin_t pin_2Y;
pin_t pin_3Y;
pin_t pin_3A;
pin_t pin_3B;
pin_t pin_4Y;
pin_t pin_4A;
pin_t pin_4B;
} chip_state_t;
static void set_logic(chip_state_t *chip) {
pin_write(chip->pin_1Y, !( pin_read(chip->pin_1A) ^ pin_read(chip->pin_1B) ) );
pin_write(chip->pin_2Y, !( pin_read(chip->pin_2A) ^ pin_read(chip->pin_2B) ) );
pin_write(chip->pin_3Y, !( pin_read(chip->pin_3A) ^ pin_read(chip->pin_3B) ) );
pin_write(chip->pin_4Y, !( pin_read(chip->pin_4A) ^ pin_read(chip->pin_4B) ) );
}
static void chip_pin_change(void *user_data, pin_t pin, uint32_t value) {
chip_state_t *chip = (chip_state_t*)user_data;
set_logic(chip);
}
void chip_init() {
printf("Initialize sn74hc266 chip!\n");
chip_state_t *chip = malloc(sizeof(chip_state_t));
chip->pin_1A = pin_init("1A", INPUT);
chip->pin_1B = pin_init("1B", INPUT);
chip->pin_1Y = pin_init("1Y", OUTPUT);
chip->pin_2A = pin_init("2A", INPUT);
chip->pin_2B = pin_init("2B", INPUT);
chip->pin_2Y = pin_init("2Y", OUTPUT);
chip->pin_3A = pin_init("3A", INPUT);
chip->pin_3B = pin_init("3B", INPUT);
chip->pin_3Y = pin_init("3Y", OUTPUT);
chip->pin_4A = pin_init("4A", INPUT);
chip->pin_4B = pin_init("4B", INPUT);
chip->pin_4Y = pin_init("4Y", OUTPUT);
pin_write(chip->pin_1A, LOW);
pin_write(chip->pin_1B, LOW);
pin_write(chip->pin_2A, LOW);
pin_write(chip->pin_2B, LOW);
pin_write(chip->pin_3A, LOW);
pin_write(chip->pin_3B, LOW);
pin_write(chip->pin_4A, LOW);
pin_write(chip->pin_4B, LOW);
const pin_watch_config_t config = {
.edge = BOTH,
.pin_change = chip_pin_change,
.user_data = chip,
};
pin_watch(chip->pin_1A, &config);
pin_watch(chip->pin_1B, &config);
pin_watch(chip->pin_2A, &config);
pin_watch(chip->pin_2B, &config);
pin_watch(chip->pin_3A, &config);
pin_watch(chip->pin_3B, &config);
pin_watch(chip->pin_4A, &config);
pin_watch(chip->pin_4B, &config);
set_logic(chip);
}