-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeeper.c
91 lines (75 loc) · 1.84 KB
/
beeper.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
89
90
91
#include <stddef.h>
#include <stdint.h>
#include "beeper.h"
#include "clib/stdio.h"
#include "ports.h"
#include "timers.h"
#include "shell.h"
#include "clib/string.h"
#include "clib/stdlib.h"
// Funkcje statyczne
static void beeper_command_beep(const char* tokens, uint32_t tokens_count);
// Inicjalizacja
void beeper_initialize(void)
{
register_command("beep", "Just beep", beeper_command_beep);
}
// Wydaje dzwięk o podanej częstotliwości
void beep_start(uint32_t frequency)
{
if(frequency==0)
{
silent();
return;
}
uint32_t div;
uint8_t tmp;
div = 1193180 / frequency;
outportb(0x43, 0xb6);
outportb(0x42, (uint8_t)div);
outportb(0x42, (uint8_t)(div >> 8));
uint8_t port = inportb(BEEP_PORT);
outportb(BEEP_PORT, port | BEEP_ON);
}
// Wydaje jednokrotny dzwięk
void beep(void)
{
beep_start(BEEP_FREQ);
timer_add(silent, BEEP_LENGTH, 0);
}
// Po prostu cisza
void silent(void)
{
uint8_t beep_off_mask = ~BEEP_ON;
uint8_t tmp = inportb(BEEP_PORT) & beep_off_mask;
outportb(BEEP_PORT, tmp);
}
// Komenda wywołująca dzwięk
static void beeper_command_beep(const char* tokens, uint32_t tokens_count)
{
if(tokens_count==1)
beep();
else if(tokens_count==3)
{
char *freq_a = (char*)get_token(tokens, 1);
char *len_a = (char*)get_token(tokens, 2);
int freq = atoi(freq_a);
int len = atoi(len_a);
if(freq<0 || freq > 20000 || len<0)
{
terminal_setcolor(VGA_COLOR_LIGHT_RED);
printf("Invalid arguments!\n");
return;
}
beep_start(freq);
timer_add(silent, len, 0);
}
else
{
terminal_setcolor(VGA_COLOR_LIGHT_RED);
printf("Invalid arguments count!\n");
return;
}
terminal_setcolor(VGA_COLOR_LIGHT_MAGENTA);
printf("BEEP\n");
}