main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*---------------------------------------------------------------*
  2. * Automotive GPS logger (C)ChaN, 2014 *
  3. * Modified k4be, 2022 *
  4. *---------------------------------------------------------------*/
  5. #include <avr/io.h>
  6. #include <avr/interrupt.h>
  7. #include <avr/sleep.h>
  8. #include <util/delay.h>
  9. #include <avr/pgmspace.h>
  10. #include <avr/wdt.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "main.h"
  14. #include "ff.h"
  15. #include "diskio.h"
  16. #include "uart0.h"
  17. #include "uart1.h"
  18. #include "xprintf.h"
  19. #include "stime.h"
  20. #include "ds18b20.h"
  21. #include "I2C.h"
  22. #include "expander.h"
  23. #include "HD44780-I2C.h"
  24. #include "gpx.h"
  25. #include "display.h"
  26. /*FUSES = {0xFF, 0x11, 0xFE};*/ /* ATmega644PA fuses: Low, High, Extended.
  27. This is the fuse settings for this project. The fuse bits will be included
  28. in the output hex file with program code. However some old flash programmers
  29. cannot load the fuse bits from hex file. If it is the case, remove this line
  30. and use these values to program the fuse bits. */
  31. volatile struct system_s System;
  32. FATFS Fatfs; /* File system object for each logical drive */
  33. FIL gps_log; /* File object */
  34. FIL system_log; /* System log file */
  35. char Line[100]; /* Line buffer */
  36. time_t utc; /* current time */
  37. struct location_s location;
  38. void start_bootloader(void) {
  39. typedef void (*do_reboot_t)(void);
  40. const do_reboot_t do_reboot = (do_reboot_t)((FLASHEND - 1023) >> 1);
  41. cli();
  42. // close_files(0); // FIXME not working
  43. LCD_Clear(); /* Do not call display.c here! */
  44. LCD_GoTo(0,0);
  45. LCD_WriteTextP(PSTR("Aktualizacja"));
  46. LCD_GoTo(8,1);
  47. LCD_WriteTextP(PSTR("softu..."));
  48. TCCR0A = TCCR1A = TCCR2A = 0; // make sure interrupts are off and timers are reset.
  49. do_reboot();
  50. wdt_enable(WDTO_15MS);
  51. while(1);
  52. }
  53. /*---------------------------------------------------------*/
  54. /* 100Hz timer interrupt generated by OC1A */
  55. /*---------------------------------------------------------*/
  56. ISR(TIMER1_COMPA_vect)
  57. {
  58. static WORD ivt_sync;
  59. // static BYTE led;
  60. static unsigned int power_sw;
  61. unsigned int *volatile ctimer;
  62. unsigned char i;
  63. for(i=0; i<sizeof(System.timers)/sizeof(unsigned int); i++){ // decrement every variable from timers struct unless it's already zero
  64. ctimer = ((unsigned int *)&System.timers) + i;
  65. if(*ctimer)
  66. (*ctimer)--;
  67. }
  68. /* Sync interval */
  69. if (IVT_SYNC && ++ivt_sync >= IVT_SYNC * 100) {
  70. ivt_sync = 0;
  71. FLAGS |= F_SYNC;
  72. }
  73. /* Green LED drive */
  74. /* if (FLAGS & F_POW) {
  75. if ((FLAGS & F_GPSOK) || (++led & 0x20)) {
  76. LEDG_ON();
  77. } else {
  78. LEDG_OFF();
  79. }
  80. } else {
  81. LEDG_OFF();
  82. }*/
  83. /* MMC/SD timer procedure */
  84. disk_timerproc();
  85. /* Switch off */
  86. if (POWER_SW_PRESSED()) {
  87. if (++power_sw == POWER_SW_TIME){
  88. FLAGS |= F_POWEROFF;
  89. power_sw++;
  90. }
  91. } else {
  92. power_sw = 0;
  93. }
  94. if (uart1_test() && uart1_get() == '0' && uart1_get() == ' '){
  95. LEDB_ON();
  96. start_bootloader();
  97. LEDR_ON();
  98. }
  99. }
  100. /* Power supply monitor */
  101. ISR(ADC_vect)
  102. {
  103. float bat_volt;
  104. static unsigned int adc_buf;
  105. static unsigned char adc_cnt;
  106. static BYTE lvt;
  107. adc_buf += ADC;
  108. if(++adc_cnt < 15)
  109. return;
  110. bat_volt = (float)adc_buf/(float)(adc_cnt)/VI_MULT;
  111. adc_buf = 0;
  112. adc_cnt = 0;
  113. System.bat_volt = bat_volt;
  114. if (FLAGS & F_LVD) {
  115. if (bat_volt > VI_LVH) {
  116. FLAGS &= ~F_LVD;
  117. lvt = 0;
  118. }
  119. } else {
  120. if (bat_volt < VI_LVL) {
  121. if (++lvt >= 3)
  122. FLAGS |= F_LVD;
  123. } else {
  124. lvt = 0;
  125. }
  126. }
  127. }
  128. /*---------------------------------------------------------*/
  129. /* User Provided Timer Function for FatFs module */
  130. /*---------------------------------------------------------*/
  131. DWORD get_fattime (void)
  132. {
  133. struct tm *stm;
  134. time_t localtime = utc + LOCALDIFF;
  135. stm = gmtime(&localtime);
  136. return (DWORD)(stm->tm_year - 80) << 25 |
  137. (DWORD)(stm->tm_mon + 1) << 21 |
  138. (DWORD)stm->tm_mday << 16 |
  139. (DWORD)stm->tm_hour << 11 |
  140. (DWORD)stm->tm_min << 5 |
  141. (DWORD)stm->tm_sec >> 1;
  142. }
  143. static void sleep(void) {
  144. set_sleep_mode(SLEEP_MODE_IDLE);
  145. sleep_enable();
  146. sleep_cpu();
  147. sleep_disable();
  148. }
  149. /*----------------------------------------------------*/
  150. /* Get a line received from GPS module */
  151. /*----------------------------------------------------*/
  152. UINT get_line ( /* 0:Brownout or timeout, >0: Number of bytes received. */
  153. char *buff,
  154. UINT sz_buf
  155. )
  156. {
  157. char c;
  158. UINT i = 0;
  159. set_timer(recv_timeout, 1000);
  160. for (;;) {
  161. wdt_reset();
  162. if (FLAGS & (F_LVD | F_POWEROFF))
  163. return 0; /* A brownout is detected */
  164. if (timer_expired(recv_timeout))
  165. return 0; /* timeout; continue the main loop */
  166. if (!uart0_test()) {
  167. sleep();
  168. continue;
  169. }
  170. c = (char)uart0_get();
  171. uart1_put(c);
  172. if (i == 0 && c != '$')
  173. continue; /* Find start of line */
  174. buff[i++] = c;
  175. if (c == '\n')
  176. break; /* EOL */
  177. if (i >= sz_buf)
  178. i = 0; /* Buffer overflow (abort this line) */
  179. }
  180. return i;
  181. }
  182. /*--------------------------------------------------------------------------*/
  183. /* Controls */
  184. void beep (UINT len, BYTE cnt)
  185. {
  186. while (cnt--) {
  187. BEEP_ON();
  188. set_timer(beep, len);
  189. while(!timer_expired(beep)) {};
  190. BEEP_OFF();
  191. set_timer(beep, len);
  192. while(!timer_expired(beep)) {};
  193. }
  194. }
  195. /* Make ISO time string */
  196. const char *get_iso_time(time_t time) {
  197. static char output[32];
  198. struct tm *ct = gmtime(&time);
  199. xsprintf(output, PSTR("%4u-%02u-%02uT%02u:%02u:%02u.000Z"), ct->tm_year+1900, ct->tm_mon+1, ct->tm_mday, ct->tm_hour, ct->tm_min, ct->tm_sec);
  200. return output;
  201. }
  202. /* Compare sentence header string */
  203. BYTE gp_comp (const char *str1, __flash const char *str2)
  204. {
  205. char c;
  206. do {
  207. c = pgm_read_byte(str2++);
  208. } while (c && c == *str1++);
  209. return c;
  210. }
  211. #define FIELD_BUF_LEN 32
  212. /* Get a column item */
  213. static
  214. const char* gp_col ( /* Returns pointer to the item (returns a NULL when not found) */
  215. const char* buf, /* Pointer to the sentence */
  216. BYTE col /* Column number (0 is the 1st item) */
  217. ) {
  218. BYTE c;
  219. static char field_buf[FIELD_BUF_LEN];
  220. unsigned char length = 0;
  221. while (col) {
  222. do {
  223. c = *buf++;
  224. if (c <= ' ') return NULL;
  225. } while (c != ',');
  226. col--;
  227. }
  228. while (*buf && *buf != ',' && length < FIELD_BUF_LEN-1) {
  229. field_buf[length++] = *buf++;
  230. }
  231. field_buf[length] = '\0';
  232. return field_buf;
  233. }
  234. static
  235. BYTE gp_val2 (
  236. const char *db
  237. )
  238. {
  239. BYTE n, m;
  240. n = db[0] - '0';
  241. if (n >= 10)
  242. return 0;
  243. m = db[1] - '0';
  244. if (m >= 10)
  245. return 0;
  246. return n * 10 + m;
  247. }
  248. static
  249. UINT gp_val3 (
  250. const char *db
  251. )
  252. {
  253. BYTE n, m, l;
  254. n = db[0] - '0';
  255. if (n >= 10)
  256. return 0;
  257. m = db[1] - '0';
  258. if (m >= 10)
  259. return 0;
  260. l = db[2] - '0';
  261. if (l >= 10)
  262. return 0;
  263. return n * 100 + m * 10 + l;
  264. }
  265. static time_t gp_rmc_parse(const char *str) {
  266. const char *p;
  267. struct tm tmc;
  268. p = gp_col(str, 1); /* Get h:m:s */
  269. if (!p)
  270. return 0;
  271. tmc.tm_hour = gp_val2(p);
  272. tmc.tm_min = gp_val2(p+2);
  273. tmc.tm_sec = gp_val2(p+4);
  274. p = gp_col(str, 9); /* Get y:m:d */
  275. if (!p)
  276. return 0;
  277. tmc.tm_mday = gp_val2(p);
  278. tmc.tm_mon = gp_val2(p+2) - 1;
  279. tmc.tm_year = gp_val2(p+4) + 100;
  280. utc = mktime(&tmc); /* Check time validity */
  281. if (utc == -1)
  282. return 0;
  283. p = gp_col(str, 2); /* Get status */
  284. if (!p || *p != 'A') {
  285. FLAGS &= ~F_GPSOK;
  286. return 0; /* Return 0 even is time is valid (comes from module's internal RTC) */
  287. }
  288. FLAGS |= F_GPSOK;
  289. return utc;
  290. }
  291. static void gp_gga_parse(const char *str) {
  292. const char *p;
  293. double tmp;
  294. /* check validity */
  295. p = gp_col(str, 6);
  296. if (*p == '0')
  297. return;
  298. System.location_valid = 1;
  299. /* parse location */
  300. p = gp_col(str, 2); /* latitude */
  301. location.lat = gp_val2(p); /* degrees */
  302. p += 2;
  303. xatof(&p, &tmp); /* minutes */
  304. tmp /= 60; /* convert minutes to degrees */
  305. location.lat += tmp;
  306. p = gp_col(str, 3); /* N/S */
  307. if (*p != 'N')
  308. location.lat = -location.lat;
  309. p = gp_col(str, 4); /* longitude */
  310. location.lon = gp_val3(p); /* degrees */
  311. p += 3;
  312. xatof(&p, &tmp); /* minutes */
  313. tmp /= 60; /* convert minutes to degrees */
  314. location.lon += tmp;
  315. p = gp_col(str, 5); /* E/W */
  316. if (*p != 'E')
  317. location.lon = -location.lon;
  318. p = gp_col(str, 7); /* satellites used */
  319. System.satellites_used = atoi(p);
  320. p = gp_col(str, 9); /* MSL altitude */
  321. xatof(&p, &tmp);
  322. location.alt = tmp;
  323. location.time = utc; /* parsed from RMC */
  324. }
  325. static time_t gps_parse(const char *str) { /* Get all required data from NMEA sentences */
  326. if (!gp_comp(str, PSTR("$GPRMC"))) {
  327. return gp_rmc_parse(str);
  328. }
  329. if (!gp_comp(str, PSTR("$GPGGA"))) {
  330. gp_gga_parse(str);
  331. return 0;
  332. }
  333. return 0;
  334. }
  335. #define LOG_SIZE 300
  336. struct {
  337. char buf[LOG_SIZE+1];
  338. unsigned int len;
  339. } logbuf;
  340. void log_put(char c){
  341. UINT bw;
  342. uart1_put(c);
  343. logbuf.buf[logbuf.len++] = c;
  344. if (logbuf.len >= LOG_SIZE && (FLAGS & F_FILEOPEN)) {
  345. if(!f_write(&system_log, logbuf.buf, logbuf.len, &bw))
  346. logbuf.len = 0;
  347. }
  348. if (logbuf.len > LOG_SIZE) {
  349. logbuf.len--;
  350. }
  351. }
  352. static
  353. void ioinit (void)
  354. {
  355. wdt_enable(WDTO_4S);
  356. MCUSR = 0;
  357. POWER_ON_DDR |= POWER_ON;
  358. PORTA |= POWER_ON;
  359. BUZZER_DDR |= BUZZER;
  360. GPS_DIS_DDR |= GPS_DIS;
  361. LEDR_DDR |= LEDR;
  362. OCR1A = F_CPU/8/100-1; /* Timer1: 100Hz interval (OC1A) */
  363. TCCR1B = _BV(WGM12) | _BV(CS11);
  364. TIMSK1 = _BV(OCIE1A); /* Enable TC1.oca interrupt */
  365. /* ADC */
  366. // ADMUX = 0;
  367. ADMUX = 1; // FIXME only testing battery voltage
  368. ADCSRA = _BV(ADEN)|_BV(ADATE)|_BV(ADIE)|_BV(ADPS2)|_BV(ADPS1)|_BV(ADPS0);
  369. /* uart1 (debug) */
  370. uart1_init();
  371. I2C_init();
  372. expander_init(0, 0x00, 0x00); /* all as outputs */
  373. LCD_Initialize();
  374. LCD_Clear();
  375. sei();
  376. ADCSRA |= _BV(ADSC);
  377. /* unused pins */
  378. DDRA |= _BV(PA2) | _BV(PA4) | _BV(PA5);
  379. PRR0 |= _BV(PRTIM2) | _BV(PRTIM0);
  380. ACSR = _BV(ACD); /* Disable analog comp */
  381. }
  382. void close_files(unsigned char flush_logs) {
  383. UINT bw;
  384. if (FLAGS & F_FILEOPEN) {
  385. if (f_close(&gps_log))
  386. System.status = STATUS_FILE_CLOSE_ERROR;
  387. if (gpx_close(&gpx_file))
  388. System.status = STATUS_FILE_CLOSE_ERROR;
  389. if (flush_logs && logbuf.len && !f_write(&system_log, logbuf.buf, logbuf.len, &bw)) {
  390. logbuf.len = 0;
  391. }
  392. if (f_close(&system_log))
  393. System.status = STATUS_FILE_CLOSE_ERROR;
  394. xputs_P(PSTR("File closed\r\n"));
  395. display_state(DISPLAY_STATE_FILE_CLOSED);
  396. }
  397. FLAGS &= ~F_FILEOPEN;
  398. disk_ioctl(0, CTRL_POWER, 0);
  399. }
  400. __flash const char __open_msg[] = "Open %s\r\n";
  401. /*-----------------------------------------------------------------------*/
  402. /* Main */
  403. /*-----------------------------------------------------------------------*/
  404. int main (void)
  405. {
  406. UINT bw, len;
  407. static struct tm ct;
  408. time_t tmp_utc, localtime;
  409. FRESULT res;
  410. unsigned char prev_status;
  411. ioinit();
  412. xdev_out(log_put);
  413. xputs_P(PSTR("STARTUP\r\n"));
  414. display_state(DISPLAY_STATE_STARTUP);
  415. for (;;) {
  416. wdt_reset();
  417. if (FLAGS & (F_POWEROFF | F_LVD)) {
  418. xputs_P(PSTR("POWEROFF\r\n"));
  419. display_state(DISPLAY_STATE_POWEROFF);
  420. if (FLAGS & F_LVD) {
  421. display_state(DISPLAY_STATE_POWEROFF_LOWBAT);
  422. _delay_ms(500);
  423. }
  424. POWEROFF();
  425. while (POWER_SW_PRESSED());
  426. _delay_ms(2000); /* wait for switch off */
  427. FLAGS &= ~F_POWEROFF; /* restart if power is not lost */
  428. POWERON();
  429. xputs_P(PSTR("RESTART\r\n"));
  430. }
  431. display_state(DISPLAY_STATE_START_MESSAGE);
  432. xprintf(PSTR("LOOP err=%u\r\n"), (unsigned int)System.status);
  433. utc = 0;
  434. localtime = 0;
  435. prev_status = System.status;
  436. System.status = STATUS_NO_POWER;
  437. beep(250, prev_status); /* Error beep */
  438. /* Wait for supply voltage stabled */
  439. _delay_ms(500);
  440. if (FLAGS & F_LVD)
  441. continue;
  442. /* report error here ( prev_status) */
  443. if (disk_status(0) & STA_NODISK) {
  444. System.status = STATUS_NO_DISK;
  445. continue;
  446. }
  447. res = f_mount(&Fatfs, "", 1);
  448. if (res != FR_OK) {
  449. xprintf(PSTR("FS error %u\r\n"), res);
  450. System.status = STATUS_DISK_ERROR;
  451. continue;
  452. }
  453. System.status = STATUS_NO_GPS;
  454. xputs(PSTR("FS Ok\r\n"));
  455. display_state(DISPLAY_STATE_CARD_OK);
  456. beep(50, 1); /* 1 beep */
  457. /* Initialize GPS receiver */
  458. GPS_ON(); /* GPS power on */
  459. FLAGS |= F_POW;
  460. _delay_ms(300); /* Delay */
  461. uart0_init(); /* Enable UART */
  462. // xfprintf(uart0_put, PSTR("$PSRF106,21*0F\r\n")); /* Send initialization command (depends on the receiver) */
  463. for (;;) { /* main loop */
  464. wdt_reset();
  465. display_refresh(DISPLAY_STATE_NO_CHANGE);
  466. gettemp();
  467. if (!(FLAGS & F_GPSOK))
  468. xputs_P(PSTR("Waiting for GPS\r\n"));
  469. len = get_line(Line, sizeof Line); /* Receive a line from GPS receiver */
  470. if (!len){
  471. if (FLAGS & (F_LVD | F_POWEROFF))
  472. break; /* brownout */
  473. continue;
  474. }
  475. tmp_utc = gps_parse(Line);
  476. if (tmp_utc) {
  477. localtime = tmp_utc + LOCALDIFF * 3600L; /* Local time */
  478. ct = *gmtime(&localtime);
  479. if (timer_expired(system_log)) {
  480. set_timer(system_log, 5000);
  481. xprintf(PSTR("Time: %u.%02u.%04u %u:%02u:%02u\r\n"), ct.tm_mday, ct.tm_mon + 1, ct.tm_year+1900, ct.tm_hour, ct.tm_min, ct.tm_sec);
  482. xprintf(PSTR("Bat volt: %.3f\r\n"), System.bat_volt);
  483. if (System.temperature_ok)
  484. xprintf(PSTR("Temp: %.2f\r\n"), System.temperature);
  485. else
  486. xputs_P(PSTR("Temperature unknown\r\n"));
  487. }
  488. LEDG_ON();
  489. _delay_ms(2);
  490. LEDG_OFF();
  491. }
  492. if (FLAGS & F_FILEOPEN) {
  493. f_write(&gps_log, Line, len, &bw);
  494. if (bw != len) {
  495. System.status = STATUS_FILE_WRITE_ERROR;
  496. break;
  497. }
  498. if (System.location_valid) /* a new point */
  499. gpx_process_point(&location, &gpx_file);
  500. wdt_reset();
  501. if (FLAGS & F_SYNC) {
  502. if (f_sync(&gps_log)) {
  503. System.status = STATUS_FILE_SYNC_ERROR;
  504. break;
  505. }
  506. FLAGS &= ~F_SYNC;
  507. }
  508. }
  509. System.location_valid = 0;
  510. if (localtime && !(FLAGS & F_FILEOPEN)) {
  511. xsprintf(Line, PSTR("%04u-%02u-%02u_%02u-%02u-%02u.LOG"), ct.tm_year+1900, ct.tm_mon + 1, ct.tm_mday, ct.tm_hour, ct.tm_min, ct.tm_sec);
  512. xprintf(__open_msg, Line);
  513. if (f_open(&gps_log, Line, FA_WRITE | FA_OPEN_ALWAYS) /* Open log file */
  514. || f_lseek(&gps_log, f_size(&gps_log)) /* Append mode */
  515. || f_write(&gps_log, "\r\n", 2, &bw)) /* Put a blank line as start marker */
  516. {
  517. System.status = STATUS_FILE_OPEN_ERROR;
  518. break; /* Failed to start logging */
  519. }
  520. wdt_reset();
  521. xsprintf(Line, PSTR("%04u-%02u-%02u_%02u-%02u-%02u.GPX"), ct.tm_year+1900, ct.tm_mon + 1, ct.tm_mday, ct.tm_hour, ct.tm_min, ct.tm_sec);
  522. xprintf(__open_msg, Line);
  523. if (f_open(&gpx_file, Line, FA_WRITE | FA_OPEN_ALWAYS) /* Open log file */
  524. || f_lseek(&gpx_file, f_size(&gpx_file)) /* Append mode */
  525. || gpx_init(&gpx_file)) /* Put a blank line as start marker */
  526. {
  527. f_close(&gpx_file);
  528. System.status = STATUS_FILE_OPEN_ERROR;
  529. break; /* Failed to start logging */
  530. }
  531. wdt_reset();
  532. xsprintf(Line, PSTR("%04u-%02u-%02u_%02u-%02u-%02u-SYSTEM.LOG"), ct.tm_year+1900, ct.tm_mon + 1, ct.tm_mday, ct.tm_hour, ct.tm_min, ct.tm_sec);
  533. xprintf(__open_msg, Line);
  534. if (f_open(&system_log, Line, FA_WRITE | FA_OPEN_ALWAYS) /* Open log file */
  535. || f_lseek(&system_log, f_size(&system_log)) /* Append mode */
  536. || f_write(&system_log, "\r\n", 2, &bw)) /* Put a blank line as start marker */
  537. {
  538. f_close(&gpx_file);
  539. f_close(&gps_log);
  540. System.status = STATUS_FILE_OPEN_ERROR;
  541. break; /* Failed to start logging */
  542. }
  543. wdt_reset();
  544. FLAGS |= F_FILEOPEN;
  545. System.status = STATUS_OK;
  546. beep(50, 2); /* Two beeps. Start logging. */
  547. display_state(DISPLAY_STATE_FILE_OPEN);
  548. continue;
  549. }
  550. }
  551. /* Stop GPS receiver */
  552. uart0_deinit();
  553. GPS_OFF();
  554. FLAGS &= ~F_POW;
  555. /* Close file */
  556. close_files(1);
  557. if (System.status != STATUS_FILE_CLOSE_ERROR)
  558. beep(500, 1); /* Long beep on file close succeeded */
  559. }
  560. }