]> git.donarmstrong.com Git - kiibohd-kll.git/blob - backends/kiibohd.py
Adding list to tuple conversion and USBCode to Capabiltiy conversion.
[kiibohd-kll.git] / backends / kiibohd.py
1 #!/usr/bin/env python3
2 # KLL Compiler - Kiibohd Backend
3 #
4 # Backend code generator for the Kiibohd Controller firmware.
5 #
6 # Copyright (C) 2014 by Jacob Alexander
7 #
8 # This file is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This file is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this file.  If not, see <http://www.gnu.org/licenses/>.
20
21 ### Imports ###
22
23 import os
24 import sys
25 import re
26
27 # Modifying Python Path, which is dumb, but the only way to import up one directory...
28 sys.path.append( os.path.expanduser('..') )
29
30 from kll_lib.containers import *
31
32
33 ### Decorators ###
34
35  ## Print Decorator Variables
36 ERROR = '\033[5;1;31mERROR\033[0m:'
37
38
39
40 ### Classes ###
41
42 class Backend:
43         # Initializes backend
44         # Looks for template file and builds list of fill tags
45         def __init__( self, templatePath ):
46                 # Does template exist?
47                 if not os.path.isfile( templatePath ):
48                         print ( "{0} '{1}' does not exist...".format( ERROR, templatePath ) )
49                         sys.exit( 1 )
50
51                 self.templatePath = templatePath
52                 self.fill_dict = dict()
53
54                 # Generate list of fill tags
55                 self.tagList = []
56                 with open( templatePath, 'r' ) as openFile:
57                         for line in openFile:
58                                 match = re.findall( '<\|([^|>]+)\|>', line )
59                                 for item in match:
60                                         self.tagList.append( item )
61
62
63         # USB Code Capability Name
64         def usbCodeCapability( self ):
65                 return "usbKeyOut";
66
67
68         # Processes content for fill tags and does any needed dataset calculations
69         def process( self, capabilities ):
70                 ## Capabilities ##
71                 self.fill_dict['CapabilitiesList'] = "const Capability CapabilitiesList[] = {\n"
72
73                 # Keys are pre-sorted
74                 for key in capabilities.keys():
75                         funcName = capabilities.funcName( key )
76                         argByteWidth = capabilities.totalArgBytes( key )
77                         self.fill_dict['CapabilitiesList'] += "\t{{ {0}, {1} }},\n".format( funcName, argByteWidth )
78
79                 self.fill_dict['CapabilitiesList'] += "};"
80
81                 print( self.fill_dict['CapabilitiesList'] )
82
83
84         # Generates the output keymap with fill tags filled
85         def generate( self, filepath ):
86                 print("My path: {0}".format( filepath) )
87