Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cyfral.c - github.com/ClusterM/ibutton.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9faec5d2bbf909505055ffc3f86ea41788179e87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <avr/io.h>
#include "defines.h"
#include "ibutton.h"
#include "bits.h"
#include "cyfral.h"
#include "onewire.h"

inline void cyfral_send_nibble(uint8_t data)
{
	uint8_t b;
	for (b = 0; b < 4; b++)
	{
		if (data & (1UL<<b))
		{
			ONEWIRE_MASTER_TX(105);			
			ONEWIRE_WAIT(36);
		} else {
			ONEWIRE_MASTER_TX(53);			
			ONEWIRE_WAIT(80);
		}
	}
}

void cyfral_send(uint16_t key)
{
	cyfral_send_nibble(0b0111);
	uint8_t b;
	for (b = 0; b < 8; b++)
	{
		speedup_leds();
		update_leds();
		cyfral_send_nibble(1UL << ((key >> (b*2)) & 0b11));
	}
}

long int read_cyfral()
{
	uint16_t buffer[100];
	int pos = 0;
	do 
	{
		TCNT1 = 0;
		while (!CYFRAL_SIGNAL && TCNT1 < 0x400);
		if (TCNT1 >= 0x400) return -1;
		//buffer[pos++] = TCNT1;
		TCNT1 = 0;
		while (CYFRAL_SIGNAL && TCNT1 < 0x400);
		if (TCNT1 >= 0x400) return -1;
		buffer[pos++] = TCNT1;
		
	} while (pos < sizeof(buffer)/2);
	int i, j;
	int startpos = -1;
	for (i = 0; i < pos-9*4; i++)
	{
		if ((buffer[i] > 70) && (buffer[i+1] > 70)  && (buffer[i+2] > 70) && (buffer[i+3] < 70))
		{
			startpos = i;
			break;
		}
	}
	uint16_t code = 0;
	int b = 0;
	if (startpos >= 0)
	{
		for (i = startpos+4; i < startpos+9*4; i+=4)
		{
			for (j = 0; j < 4; j++)
			{
				//send_num(buffer[i+j]);
				if (buffer[i+j] > 70)
					code |= j << (b*2);
			}
			b++;
		}
	} else return -1;
	return code;	
}

long int read_cyfral_with_check()	
{
	long int code = 0, tries = 0, i;
	for (i = 0; i < 10; i++)
	{
		long int code2 = read_cyfral();
		if ((code2 >= 0) && (code == code2))
		{
			tries++;
		} else {
			tries = 0;
			code = code2;
		}
		if (tries >= 3) 
		{
			return code2;
		}
	}	
	return -1;
}