Skip to content
Snippets Groups Projects
Unverified Commit 6d2baf31 authored by Eunhak Lee's avatar Eunhak Lee
Browse files

impl: bump code

parents
Branches master
No related tags found
No related merge requests found
.idea/
cmake-build-debug/
build/
*.o
*.obj
cmake_minimum_required(VERSION 3.29)
project(hci_double_click C)
set(CMAKE_C_STANDARD 17)
include_directories("./include")
add_executable(hci_double_click
include/timer.c
include/timer.h
include/types.h
main.c
)
#include "types.h"
#include "timer.h"
#include <stdlib.h>
#include <string.h>
const timer_spec_t *TIMERS;
timer_spec_t *timer_ptr;
void init_timer() {
TIMERS = malloc(100 * sizeof(timer_spec_t));
timer_ptr = TIMERS;
memset(TIMERS, 0, 100 * sizeof(timer_spec_t));
}
timer_spec_t *start_timer(const timestamp_t expires_at, const device_id_t device_id, const timer_handler_t callback) {
timer_ptr->expires_at = expires_at;
timer_ptr->device_id = device_id;
timer_ptr->handler = callback;
return timer_ptr++;
}
//
// Created by enak on 2024-11-09.
//
#pragma once
#ifndef TIMER_H
#define TIMER_H
typedef void(*timer_handler_t)(void*);
typedef struct TimerSpec {
timestamp_t expires_at;
device_id_t device_id;
timer_handler_t handler;
} timer_spec_t;
extern const timer_spec_t *TIMERS;
void init_timer();
timer_spec_t *start_timer(const timestamp_t expires_at, const device_id_t device_id, const timer_handler_t callback);
#endif //TIMER_H
//
// Created by enak on 2024-11-09.
//
#pragma once
#ifndef TYPES_H
#define TYPES_H
typedef int device_id_t;
typedef long timestamp_t;
typedef enum EventType {
DOWN_UP = 0,
UP_DOWN = 1,
} event_type_t;
typedef struct {
device_id_t device_id;
timestamp_t timestamp;
event_type_t event_type;
} event_t;
typedef enum ClickType {
SINGLE_CLICK = 1,
DOUBLE_CLICK = 2,
} click_type_t;
typedef char bool;
#define true 1
#define false 0
#define MAX_DEVICES 4
#endif //TYPES_H
main.c 0 → 100644
#include <stdio.h>
#include <stdlib.h>
#include "types.h"
#include "timer.h"
timestamp_t now = 0;
int DOUBLE_CLICK_LIMIT = 500;
void on_click(const event_t *event);
void on_timer_expired();
void trigger_ui_event(device_id_t button_id, click_type_t click_type, timestamp_t timestamp);
void simulate(event_t events[]);
timer_spec_t *DEVICE_TO_TIMER[MAX_DEVICES] = {NULL};
int main(void) {
int N;
scanf_s("%d", &N);
event_t *events = malloc(sizeof(event_t) * N);
event_t *event_ptr = events;
int d, t, e;
for (int i = 0; i < N; i++) {
scanf_s("%d %d %d", &d, &t, &e);
event_ptr->device_id = d;
event_ptr->timestamp = t;
event_ptr->event_type = (e == 0) ? DOWN_UP : UP_DOWN;
event_ptr++;
}
simulate(events);
return 0;
}
void on_click(const event_t *event) {
if (event->event_type != DOWN_UP) return;
// first click
if (NULL == DEVICE_TO_TIMER[event->device_id]) {
DEVICE_TO_TIMER[event->device_id] = start_timer(now + DOUBLE_CLICK_LIMIT, event->device_id, on_timer_expired);
}
// double click
else {
trigger_ui_event(event->device_id, DOUBLE_CLICK, now);
DEVICE_TO_TIMER[event->device_id] = NULL;
}
}
void on_timer_expired(const void *arg) {
const timer_spec_t *spec = arg;
// timer 가 중단된 경우
if (spec == NULL || spec != DEVICE_TO_TIMER[spec->device_id]) return;
DEVICE_TO_TIMER[spec->device_id] = NULL;
trigger_ui_event(spec->device_id, SINGLE_CLICK, now);
}
void trigger_ui_event(device_id_t button_id, click_type_t click_type, timestamp_t timestamp) {
printf("[%6d] (/dev/%d) click_type=%s\n", timestamp, button_id,
(click_type == SINGLE_CLICK) ? "SINGLE" : "DOUBLE");
}
void simulate(event_t events[]) {
init_timer();
event_t *event_ptr = events;
timer_spec_t *timer_ptr = TIMERS;
for (
now = 0;
(event_ptr->event_type == DOWN_UP) || (event_ptr->event_type == UP_DOWN) || (timer_ptr->handler != 0);
now++
) {
while (event_ptr->timestamp == now) {
printf("[%6d] (/dev/%d) %s\n", now, event_ptr->device_id, (event_ptr->event_type == UP_DOWN) ? "UP" : "DOWN");
on_click(event_ptr);
event_ptr++;
}
while (timer_ptr->expires_at == now && timer_ptr->handler != NULL) {
printf("[%6d] (/dev/%d) timer_expired\n", now, timer_ptr->device_id);
timer_ptr->handler(timer_ptr);
timer_ptr++;
}
}
}
2
0 0 0
0 100 1
\ No newline at end of file
4
0 0 0
1 0 0
0 100 1
1 100 1
\ No newline at end of file
4
0 0 0
1 100 0
1 300 1
0 400 1
\ No newline at end of file
4
0 0 0
0 100 1
0 300 0
0 400 1
\ No newline at end of file
8
0 100 0
0 200 1
0 500 0
0 900 1
0 1100 0
0 1400 1
0 1500 0
0 1600 1
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment