Xemu [doxygen]  hyppo 0a42be3a057156924bc1b626a687bd6e27349c45 @ Sat 19 Mar 02:15:11 CET 2022
printer.c
Go to the documentation of this file.
1 /* Xep128: Minimalistic Enterprise-128 emulator with focus on "exotic" hardware
2  Copyright (C)2015,2016 LGB (Gábor Lénárt) <lgblgblgb@gmail.com>
3  http://xep128.lgb.hu/
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18 
19 #include "xep128.h"
20 #include "printer.h"
21 #include "dave.h"
22 #include "configuration.h"
23 
24 #include "main.h"
25 
26 
27 static FILE *fp = NULL;
28 static int fp_to_open = 1;
29 
30 #define BUFFER_SIZE 1024
31 #define COVOX_ACTIVATION_LIMIT 0x100
32 
33 static Uint8 buffer[BUFFER_SIZE];
34 static int buffer_pos;
35 static int strobes_missed = 0;
37 static int printer_is_covox = 0;
38 static int covox_to_warn = 1;
39 static int old_strobe_level = 0;
40 
41 
42 
43 
44 static void write_printer_buffer ( void )
45 {
46  if (buffer_pos && fp != NULL) {
47  if (fwrite(buffer, buffer_pos, 1, fp) != 1) {
48  WARNING_WINDOW("Cannot write printer output: %s\nFurther printer I/O has been disabled.", ERRSTR());
49  fclose(fp);
50  fp = NULL;
51  }
52  }
53  buffer_pos = 0;
54 }
55 
56 
57 
58 void printer_close ( void )
59 {
60  if (fp) {
61  write_printer_buffer();
62  fclose(fp);
63  DEBUG("Closing printer output file." NL);
64  fp_to_open = 1;
65  fp = NULL;
66  }
67 }
68 
69 
70 
72 {
74  if (strobes_missed > COVOX_ACTIVATION_LIMIT) {
75  if (!printer_is_covox) {
76  DEBUG("PRINTER: COVOX: covox mode has been activated on more than %d writes without STROBE" NL, COVOX_ACTIVATION_LIMIT);
77  printer_is_covox = 1;
78  if (covox_to_warn) {
79  covox_to_warn = 0;
80  INFO_WINDOW("COVOX on printer port has been activated. There will be no further messages on this.");
81  }
83  }
84  } else
85  strobes_missed++;
86 }
87 
88 
89 
90 static void send_data_to_printer ( Uint8 data )
91 {
92  //DEBUG("PRINTER GOT DATA: %d" NL, data);
93  if (fp_to_open) {
94  const char *printfile = config_getopt_str("printfile");
95  char path[PATH_MAX + 1];
96  fp = open_emu_file(printfile, "ab", path);
97  if (fp == NULL)
98  WARNING_WINDOW("Cannot create/append printer output file \"%s\": %s.\nYou can use Xep128 but printer output will not be logged!", path, ERRSTR());
99  else
100  INFO_WINDOW("Printer event, file \"%s\" has been opened for the output.", path);
101  fp_to_open = 0;
102  buffer_pos = 0;
103  }
104  if (fp != NULL) {
105  buffer[buffer_pos++] = data;
106  if (buffer_pos == BUFFER_SIZE)
107  write_printer_buffer();
108  // fprintf(fp, "%c", data);
109  }
110 }
111 
112 
113 
114 void printer_port_check_strobe ( int level )
115 {
116  if (old_strobe_level && !level) {
117  DEBUG("PRINTER: strobe!" NL);
118  //old_strobe_level = level;
119  strobes_missed = 0;
120  if (printer_is_covox) {
121  DEBUG("PRINTER: COVOX: covox mode has been disabled on STROBE, data byte %02Xh is sent for printing" NL, printer_data_byte);
122  printer_is_covox = 0;
124  }
125  send_data_to_printer(printer_data_byte);
126  }/* else
127  DEBUG("PRINTER: NOT strobe: %d -> %d" NL, old_strobe_level, level);*/
128  old_strobe_level = level;
129 }
130 
131 
132 
134 {
135  if (printer_is_covox) {
136  printer_is_covox = 0;
137  strobes_missed = 0;
138  DEBUG("PRINTER: COVOX: covox mode has been disabled on emulator event." NL);
139  }
140 }
141 
printer_port_check_strobe
void printer_port_check_strobe(int level)
Definition: printer.c:117
audio_source
int audio_source
Definition: dave.c:40
WARNING_WINDOW
#define WARNING_WINDOW(...)
Definition: xep128.h:115
INFO_WINDOW
#define INFO_WINDOW(...)
Definition: xep128.h:114
printer_close
void printer_close(void)
Definition: printer.c:61
dave.h
m65-memcontent-generator.data
data
Definition: m65-memcontent-generator.py:119
Uint8
uint8_t Uint8
Definition: fat32.c:51
BUFFER_SIZE
#define BUFFER_SIZE
Definition: printer.c:30
open_emu_file
FILE * open_emu_file(const char *name, const char *mode, char *pathbuffer)
Definition: configuration.c:109
printer.h
NL
#define NL
Definition: fat32.c:37
printer_data_byte
Uint8 printer_data_byte
Definition: printer.c:39
printer_disable_covox
void printer_disable_covox(void)
Definition: printer.c:136
COVOX_ACTIVATION_LIMIT
#define COVOX_ACTIVATION_LIMIT
Definition: printer.c:31
xep128.h
AUDIO_SOURCE_PRINTER_COVOX
#define AUDIO_SOURCE_PRINTER_COVOX
Definition: dave.h:23
main.h
configuration.h
AUDIO_SOURCE_DAVE
#define AUDIO_SOURCE_DAVE
Definition: dave.h:22
ERRSTR
#define ERRSTR()
Definition: enterprise128.h:56
printer_port_set_data
void printer_port_set_data(Uint8 data)
Definition: printer.c:74
DEBUG
#define DEBUG(...)
Definition: emutools_basicdefs.h:167