comparison src/cs/services/buzm/buzm_process.c @ 297:8dfdf88d632f

BUZM SWE initial implementation
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 29 Mar 2022 03:45:41 +0000
parents
children
comparison
equal deleted inserted replaced
296:a927f030a4e0 297:8dfdf88d632f
1 /*
2 * In this module we are going to implement the main process functions
3 * for BUZM.
4 */
5
6 #include "buzm/buzm_env.h"
7 #include "buzm/buzm_func_i.h"
8 #include "rv/rv_general.h"
9 #include "rvf/rvf_api.h"
10 #include "rvm/rvm_use_id_list.h"
11 #include "ffs/ffs_api.h"
12 #include "main/sys_types.h"
13 #include "buzzer/pwt.h"
14
15 static void stop_current_melody(void)
16 {
17 PWT_stop_tone();
18 rvf_stop_timer(BUZM_TIMER);
19 PWT_block_off();
20 ffs_close(buzm_env->ffs_fd);
21 buzm_env->melody_running = FALSE;
22 }
23
24 void buzm_process_start_req(struct buzm_start_msg *msg)
25 {
26 T_FFS_SIZE rdsize;
27
28 /* stop any previous melody first */
29 if (buzm_env->melody_running) {
30 stop_current_melody();
31 rvf_send_trace("Melody play interrupted by new melody start",43,
32 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_MEDIUM,
33 BUZM_USE_ID);
34 }
35 rdsize = ffs_read(msg->fd, (void *) buzm_env->chunk_buf,
36 sizeof(struct melody_entry) * MELODY_CHUNK_SIZE);
37 if (rdsize < sizeof(struct melody_entry)) {
38 ffs_close(msg->fd);
39 rvf_send_trace("Error: invalid melody file", 26, NULL_PARAM,
40 RV_TRACE_LEVEL_ERROR, BUZM_USE_ID);
41 return;
42 }
43 /* start this melody */
44 rvf_send_trace("Starting PWT melody play", 24, NULL_PARAM,
45 RV_TRACE_LEVEL_DEBUG_LOW, BUZM_USE_ID);
46 PWT_block_on();
47 buzm_env->melody_running = TRUE;
48 buzm_env->ffs_fd = msg->fd;
49 buzm_env->play_volume = msg->volume;
50 buzm_env->loop_mode = msg->loop;
51 buzm_env->chunk_play_ptr = 0;
52 buzm_env->chunk_end_ptr = rdsize / sizeof(struct melody_entry);
53 buzm_env->melody_end_flag = FALSE;
54 rvf_start_timer(BUZM_TIMER, 1, FALSE);
55 }
56
57 void buzm_process_stop_req(struct buzm_stop_msg *msg)
58 {
59 if (buzm_env->melody_running) {
60 stop_current_melody();
61 rvf_send_trace("Melody play stopped by command", 30,
62 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW,
63 BUZM_USE_ID);
64 } else {
65 rvf_send_trace("Redundant melody play stop command", 34,
66 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_MEDIUM,
67 BUZM_USE_ID);
68 }
69 }
70
71 static void melody_loop_restart(void)
72 {
73 T_FFS_SIZE rdsize;
74
75 ffs_seek(buzm_env->ffs_fd, 0, FFS_SEEK_SET);
76 rdsize = ffs_read(buzm_env->ffs_fd, (void *) buzm_env->chunk_buf,
77 sizeof(struct melody_entry) * MELODY_CHUNK_SIZE);
78 if (rdsize < sizeof(struct melody_entry)) {
79 rvf_send_trace("Bad melody file on loop restart!", 32,
80 NULL_PARAM, RV_TRACE_LEVEL_ERROR, BUZM_USE_ID);
81 PWT_block_off();
82 ffs_close(buzm_env->ffs_fd);
83 buzm_env->melody_running = FALSE;
84 return;
85 }
86 buzm_env->chunk_play_ptr = 0;
87 buzm_env->chunk_end_ptr = rdsize / sizeof(struct melody_entry);
88 buzm_env->melody_end_flag = FALSE;
89 rvf_start_timer(BUZM_TIMER, 1, FALSE);
90 }
91
92 static void melody_normal_finish(void)
93 {
94 PWT_block_off();
95 ffs_close(buzm_env->ffs_fd);
96 buzm_env->melody_running = FALSE;
97 rvf_send_trace("Melody play finished", 20, NULL_PARAM,
98 RV_TRACE_LEVEL_DEBUG_LOW, BUZM_USE_ID);
99 }
100
101 static void melody_end_process(void)
102 {
103 if (buzm_env->loop_mode)
104 melody_loop_restart();
105 else
106 melody_normal_finish();
107 }
108
109 void buzm_handle_timer(void)
110 {
111 struct melody_entry *curnote;
112 UINT8 pwt_vol;
113 T_FFS_SIZE rdsize;
114
115 /* weed out any spurious timer hits */
116 if (!buzm_env->melody_running)
117 return;
118 /* real end of previous note or rest */
119 PWT_stop_tone();
120 if (buzm_env->melody_end_flag) {
121 melody_end_process();
122 return;
123 }
124 curnote = buzm_env->chunk_buf + buzm_env->chunk_play_ptr;
125 if (curnote->note_volume) {
126 pwt_vol = ((buzm_env->play_volume * curnote->note_volume + 63)
127 >> 6) - 1;
128 PWT_play_tone(curnote->pwt_note, pwt_vol);
129 }
130 rvf_start_timer(BUZM_TIMER, curnote->duration, FALSE);
131 buzm_env->chunk_play_ptr++;
132 if (buzm_env->chunk_play_ptr < buzm_env->chunk_end_ptr)
133 return;
134 /* do we have more? */
135 rdsize = ffs_read(buzm_env->ffs_fd, (void *) buzm_env->chunk_buf,
136 sizeof(struct melody_entry) * MELODY_CHUNK_SIZE);
137 if (rdsize < sizeof(struct melody_entry))
138 buzm_env->melody_end_flag = TRUE;
139 else {
140 buzm_env->chunk_play_ptr = 0;
141 buzm_env->chunk_end_ptr = rdsize / sizeof(struct melody_entry);
142 }
143 }