aldl_pi/aldl.c

114 lines
2.7 KiB
C

/*
* ALDL Dashboard, core 160 baud handling code
*
* (c) 2019 Solomon Peachy <pizza@shaftnet.org>
*
* The latest version of this program can be found at:
*
* http://git.shaftnet.org/cgit/users/pizza/public/aldl_pi.git
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* [http://www.gnu.org/licenses/gpl-3.0.html]
*
* SPDX-License-Identifier: GPL-3.0+
*
*/
#include <stdio.h>
#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include "aldl.h"
/* shift register & sync word */
#define SYNC_WORD 0x1ff
#define MAX_STREAM_WORDS 255
static uint16_t ALDLSR = 0;
static uint8_t bits = 0;
static int datalen = 0;
static uint8_t data[MAX_STREAM_WORDS];
int gotsync = 0;
static FILE *datalog_handle;
struct stream_common decoded = { 0 };
void (*aldl_callback)(uint8_t *data, int datalen) = NULL;
/* Datalogging */
void datalog_init(char *fname)
{
datalog_handle = fopen(fname, "a");
if (!datalog_handle)
perror("Can't open datalog file!\n");
fprintf(datalog_handle, "# New session\n");
}
void datalog_close(void)
{
if (datalog_handle)
fclose(datalog_handle);
}
static void aldl_datalog(uint8_t *data, int datalen)
{
int i;
if (!datalog_handle) return;
fprintf(datalog_handle, "%ld", time(NULL));
for (i = 0 ; i < datalen ; i++) {
fprintf(datalog_handle, ",%02x", *data++);
}
fprintf(datalog_handle, "\n");
}
/* ISR */
void isrfn(void)
{
int val;
delayfn(1); // To handle both variations, minimum of 0.5, max of 1.8
val = readbit();
ALDLSR <<= 1;
ALDLSR |= (val & 1);
/* Wait for initial sync. */
if (!gotsync) {
if ((ALDLSR & SYNC_WORD) == SYNC_WORD) {
gotsync = 1;
}
return; /* No sync, drop everything */
}
if ((ALDLSR & SYNC_WORD) == SYNC_WORD) {
/* New sync word, we've finished a sequence */
if (aldl_callback)
aldl_callback(data, datalen);
if (datalog_handle)
aldl_datalog(data, datalen);
bits = 0;
datalen = 0;
} else if (++bits == 9) {
/* Just latch in the word */
data[datalen++] = ALDLSR & 0xff;
bits = 0;
}
}