Skip to content

Commit

Permalink
Bugfix hk310-filter firmware, now working
Browse files Browse the repository at this point in the history
Both binary counter and keyboard are working fine
  • Loading branch information
laneboysrc committed Mar 12, 2014
1 parent 53c7663 commit c3679b3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 25 deletions.
75 changes: 53 additions & 22 deletions firmware/hk310-filter/hk310-filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdint.h>

#include "uart.h"
#include "keyboard-matrix.h"


static __code uint16_t __at (_CONFIG1) configword1 = _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF;
Expand All @@ -27,6 +28,7 @@ static __code uint16_t __at (_CONFIG2) configword2 = _WRT_OFF & _PLLEN_OFF & _ST
#define STATE_CHECKSUM_L 14
#define STATE_SKIP 15

uint16_t payload;

/*****************************************************************************
Init_hardware()
Expand All @@ -40,9 +42,8 @@ static void Init_hardware(void) {

//-----------------------------
// IO Port initialization
PORTA = 0;
LATA = 0;
ANSELA = 0;
ANSELC = 0;
APFCON0 = 0b00000000; // Use RC4/RC5 for UART TX/RX; RA4 for T1G
APFCON1 = 0;
TRISA = 0b11111111; // Make all ports A input
Expand Down Expand Up @@ -73,34 +74,64 @@ uint16_t crc16_ccitt(uint16_t crc16, uint8_t b)
}


uint8_t maximizeDifference(uint8_t old, uint8_t new)
{
uint8_t diff1;
uint8_t diff2;
uint8_t new2;

new2 = new | (0x400 >> 5);

diff1 = (old >= new) ? old - new : new - old;
diff2 = (old >= new2) ? old - new2 : new2 - old;

if (diff2 > diff1) {
return new2;
}
return new;

}

static uint16_t c = 0;
/*****************************************************************************
Returns the next 12-bit value to transmit over CH3
****************************************************************************/
uint16_t nextValue()
{
static uint8_t index = 0;
uint16_t rx = SYNC_VALUE_NOMINAL;
uint8_t values[4];

++index;
switch (index) {
case 0:
return SYNC_VALUE_NOMINAL;

case 1:
return 0x020;
payload = c;
payload = Scan_keyboard();

case 2:
return 0x420;
values[0] = 0;
values[1] = (payload >> 0) & 0x3f;
values[2] = maximizeDifference(values[1], (payload >> 6) & 0x1f);
values[3] = maximizeDifference(values[2], (payload >> 11) & 0x1f);

if (index == 0) {
rx = SYNC_VALUE_NOMINAL;
++c;
}
else {
rx = values[index] << 5;
rx += 0x20; // Add 0x20 to avoid tiny pulse durations
rx += (rx >> 8);
}

case 3:
index = 0;
return 0x810;
++index;
if (index > 3) {
index = 0;
}

index = 0;
return SYNC_VALUE_NOMINAL;

return (2720 - 1 - rx) * 16 / 17;
}


static uint8_t state = STATE_WAIT_FOR_SYNC;
static uint16_t new_ch3 = (2720 - 1 - SYNC_VALUE_NOMINAL) * 16 / 17;
static uint8_t count = 0;
/*****************************************************************************
filter()
Expand All @@ -115,12 +146,11 @@ uint16_t nextValue()
****************************************************************************/
uint8_t filter(uint8_t b)
{
static uint8_t state = STATE_WAIT_FOR_SYNC;
static uint8_t skip;
static uint16_t new_checksum;
static uint16_t new_crc16;
static uint8_t count = 0;
static uint16_t new_ch3 = SYNC_VALUE_NOMINAL;



switch (state) {
case STATE_WAIT_FOR_SYNC:
Expand Down Expand Up @@ -239,11 +269,11 @@ uint8_t filter(uint8_t b)
state = STATE_CHECKSUM_L;
break;

case STATE_CHECKSUM_L:
case STATE_CHECKSUM_L:
b = new_checksum & 0xff;
state = STATE_WAIT_FOR_SYNC;
++count;
if (count >= 2) {
if (count > 2) {
count = 0;
new_ch3 = nextValue();
}
Expand All @@ -269,6 +299,7 @@ uint8_t filter(uint8_t b)
void main(void) {
Init_hardware();
Init_UART();
Init_keyboard();

while (1) {
uint8_t b;
Expand Down
7 changes: 4 additions & 3 deletions firmware/hk310-filter/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
all: hk310-filter hw-test


CFLAGS:=--opt-code-size --use-non-free -mpic14 -p16f1825
LDFLAGS:=--use-non-free -mpic14 -p16f1825
#CFLAGS:=--opt-code-size --use-non-free -mpic14 -p16f1825
CFLAGS:=--no-peep --use-non-free -mpic14 -p16f1825
LDFLAGS:=--no-peep --use-non-free -mpic14 -p16f1825

#############################################################################
.PHONY: hk310-filter
hk310-filter: hk310-filter.hex

hk310-filter.hex: hk310-filter.o uart.o
hk310-filter.hex: hk310-filter.o uart.o keyboard-matrix.o
sdcc $(LDFLAGS) -o $@ $^

#############################################################################
Expand Down

0 comments on commit c3679b3

Please sign in to comment.