Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/plugins/rfid/rfidutils.py
blob: a00e518e904471ea4a9183f1ef3c076f8a1647d4 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# utils.py - Helper functions for tis2000.py
# Copyright (C) 2010 Emiliano Pastorino <epastorino@plan.ceibal.edu.uy>
#
# 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, see <http://www.gnu.org/licenses/>.

import os
import logging

def find_device(path=None):
    """
    Search for devices.
    Return a device instance or None.
    """
    device = None
    if path is not None:
        path = os.path.join(path, 'plugins/rfid')
    else:
        path = os.path.join('.', 'plugins/rfid')
    for i in os.listdir(path):
        if not os.path.isdir(os.path.join(path, i)):
            try:
                _tempmod = __import__('rfid.%s'%i.split('.')[0], globals(),
                                      locals(), ['RFIDReader'], -1)
                devtemp = _tempmod.RFIDReader()
                if devtemp.get_present() == True:
                    device = devtemp
            except Exception, e:
                # logging.error("FIND_DEVICE: %s: %s"%(i, e))
                pass
    if device is None:
        logging.debug("No RFID device found")
    return device


def strhex2bin(strhex):
    """
    Convert a string representing an hex value into a 
    string representing the same value in binary format.
    """
    dic = { '0':"0000",
            '1':"0001",
            '2':"0010",
            '3':"0011",
            '4':"0100",
            '5':"0101",
            '6':"0110",
            '7':"0111",
            '8':"1000",
            '9':"1001",
            'A':"1010",
            'B':"1011",
            'C':"1100",
            'D':"1101",
            'E':"1110",
            'F':"1111"
    }
    binstr = ""
    for i in strhex:
        binstr = binstr + dic[i.upper()]
    return binstr

def strbin2dec(strbin):
    """
    Convert a string representing a binary value into a 
    string representing the same value in decimal format.
    """
    strdec = "0"
    for i in range(1, strbin.__len__()+1):
        strdec = str(int(strdec)+int(strbin[-i])*int(pow(2, i-1)))
    return strdec

def dec2bin(ndec):
    """
    Convert a decimal number into a string representing
    the same value in binary format.
    """
    if ndec < 1:
        return "0"
    binary = []
    while ndec != 0:
        binary.append(ndec%2)
        ndec = ndec/2
    strbin = ""
    binary.reverse()
    for i in binary:
        strbin = strbin+str(i)
    return strbin

def bin2hex(strbin):
    """
    Convert a string representing a binary number into a string
    representing the same value in hexadecimal format.
    """
    dic = { "0000":"0",
            "0001":"1",
            "0010":"2",
            "0011":"3",
            "0100":"4",
            "0101":"5",
            "0110":"6",
            "0111":"7",
            "1000":"8",
            "1001":"9",
            "1010":"A",
            "1011":"B",
            "1100":"C",
            "1101":"D",
            "1110":"E",
            "1111":"F"
    }
    while strbin.__len__()%4 != 0:
        strbin = '0' + strbin
    strh = ""
    for i in range(0, strbin.__len__()/4):
        strh = strh + dic[str(strbin[i*4:i*4+4])]
    return strh