/*
 * hex_encoder.c
 *
 *  Created on: 2016/11/17
 *      Author: takayun
 */

#include "hex_encoder.h"
#include <ctype.h>

void encodeNumHex(int hex_i, int num) {
	char encoded = 0;
	switch (num) {
	case 0:
		encoded = (char)0x40;	// 100 0000
		break;
	case 1:
		encoded = (char)0xF9;	// 111 1001
		break;
	case 2:
		encoded = (char)0x24;	// 010 0100
		break;
	case 3:
		encoded = (char)0x30;	// 011 0000
		break;
	case 4:
		encoded = (char)0x19;	// 001 1001
		break;
	case 5:
		encoded = (char)0x12;	// 001 0010
		break;
	case 6:
		encoded = (char)0x02;	// 000 0010
		break;
	case 7:
		encoded = (char)0x58;	// 101 1000
		break;
	case 8:
		encoded = (char)0x00;	// 000 0000
		break;
	case 9:
		encoded = (char)0x10;	// 001 0000
		break;
	default:
		encoded = 0;
		break;
	}

	switch (hex_i) {
	case 0:
		*hex0 = encoded;
		break;
	case 1:
		*hex1 = encoded;
		break;
	case 2:
		*hex2 = encoded;
		break;
	case 3:
		*hex3 = encoded;
		break;
	case 4:
		*hex4 = encoded;
		break;
	case 5:
		*hex5 = encoded;
		break;
	case 6:
		*hex6 = encoded;
		break;
	case 7:
		*hex7 = encoded;
		break;
	default:
		break;
	}
}

void encodeLatHex(int hex_i, char c) {
	char encoded = 0;

	if (isdigit(c)) {
		encodeNumHex(hex_i, c-'0');
		return;
	}

	switch (c) {
	case ' ':
		encoded = (char)0xFF;	// 111 1111
		break;
	case '-':
		encoded = (char)0x3F;	// 011 1111
		break;
	case 'a':
		encoded = (char)0x08;	// 000 1000
		break;
	case 'b':
		encoded = (char)0x03;	// 000 0011
		break;
	case 'c':
		encoded = (char)0x27;	// 010 0111
		break;
	case 'd':
		encoded = (char)0x21;	// 010 0001
		break;
	case 'e':
		encoded = (char)0x06;	// 000 0110
		break;
	case 'f':
		encoded = (char)0x0E;	// 000 1110
		break;
	case 'g':
		encoded = (char)0x42;	// 100 0010
		break;
	case 'h':
		encoded = (char)0x0B;	// 000 1011
		break;
	case 'i':
		encoded = (char)0xFB;	// 111 1011
		break;
	case 'j':
		encoded = (char)0x61;	// 110 0001
		break;
	case 'k':
		encoded = (char)0x0A;	// 000 1010
		break;
	case 'l':
		encoded = (char)0x47;	// 100 0111
		break;
	case 'm':
		encoded = (char)0x48;	// 100 1000
		break;
	case 'n':
		encoded = (char)0x2B;	// 010 1011
		break;
	case 'o':
		encoded = (char)0x23;	// 010 0011
		break;
	case 'p':
		encoded = (char)0x0C;	// 000 1100
		break;
	case 'q':
		encoded = (char)0x04;	// 000 0100
		break;
	case 'r':
		encoded = (char)0x2F;	// 010 1111
		break;
	case 's':
		encoded = (char)0x13;	// 001 0011
		break;
	case 't':
		encoded = (char)0x07;	// 000 0111
		break;
	case 'u':
		encoded = (char)0x63;	// 110 0011
		break;
	case 'v':
		encoded = (char)0x41;	// 100 0001
		break;
	case 'w':
		encoded = (char)0x01;	// 000 0001
		break;
	case 'x':
		encoded = (char)0x09;	// 000 1001
		break;
	case 'y':
		encoded = (char)0x11;	// 001 0001
		break;
	case 'z':
		encoded = (char)0x64;	// 110 0100
		break;
	default:
		encoded = 0;
		break;
	}

	switch (hex_i) {
	case 0:
		*hex0 = encoded;
		break;
	case 1:
		*hex1 = encoded;
		break;
	case 2:
		*hex2 = encoded;
		break;
	case 3:
		*hex3 = encoded;
		break;
	case 4:
		*hex4 = encoded;
		break;
	case 5:
		*hex5 = encoded;
		break;
	case 6:
		*hex6 = encoded;
		break;
	case 7:
		*hex7 = encoded;
		break;
	default:
		break;
	}
}
