This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
qrcode_footprint_wizard.py
148 lines (132 loc) · 6.59 KB
/
qrcode_footprint_wizard.py
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
# last change: 2017, Jan 4.
import pcbnew
import FootprintWizardBase
# Additional import for QRCode
# see https://github.com/kazuhikoarase/qrcode-generator/blob/master/python/qrcode.py
import kicad_qrcode as qrcode # TODO: local qrcode package is prefered, so we renamed it
class QRCodeWizard(FootprintWizardBase.FootprintWizard):
GetName = lambda self: '2D Barcode QRCode'
GetDescription = lambda self: 'QR Code barcode generator'
GetReferencePrefix = lambda self: 'QR***'
GetValue = lambda self: self.module.Value().GetText()
def GenerateParameterList(self):
self.AddParam("Barcode", "Pixel Width", self.uMM, 0.5, min_value=0.4)
self.AddParam("Barcode", "Border", self.uInteger, 0)
self.AddParam("Barcode", "Contents", self.uString, 'Example')
self.AddParam("Barcode", "Negative", self.uBool, False)
self.AddParam("Barcode", "Use SilkS layer", self.uBool, False)
self.AddParam("Barcode", "Use Cu layer", self.uBool, True)
self.AddParam("Caption", "Enabled", self.uBool, True)
self.AddParam("Caption", "Height", self.uMM, 1.2)
self.AddParam("Caption", "Width", self.uMM, 1.2)
self.AddParam("Caption", "Thickness", self.uMM, 0.12)
def CheckParameters(self):
self.Barcode = str(self.parameters['Barcode']['Contents'])
self.X = self.parameters['Barcode']['Pixel Width']
self.negative = self.parameters['Barcode']['Negative']
self.UseSilkS = self.parameters['Barcode']['Use SilkS layer']
self.UseCu = self.parameters['Barcode']['Use Cu layer']
self.border = int(self.parameters['Barcode']['Border'])
self.textHeight = int(self.parameters['Caption']['Height'])
self.textThickness = int(self.parameters['Caption']['Thickness'])
self.textWidth = int(self.parameters['Caption']['Width'])
self.module.Value().SetText(str(self.Barcode))
# Build Qrcode
self.qr = qrcode.QRCode()
self.qr.setTypeNumber(4)
# ErrorCorrectLevel: L = 7%, M = 15% Q = 25% H = 30%
self.qr.setErrorCorrectLevel(qrcode.ErrorCorrectLevel.M)
self.qr.addData(str(self.Barcode))
self.qr.make()
def drawSquareArea( self, layer, size, xposition, yposition):
# creates a EDGE_MODULE of polygon type. The polygon is a square
polygon = pcbnew.EDGE_MODULE(self.module)
polygon.SetShape(pcbnew.S_POLYGON)
polygon.SetWidth( 0 )
polygon.SetLayer(layer)
halfsize = size/2
polygon.GetPolyShape().NewOutline();
polygon.GetPolyShape().Append( halfsize+xposition, halfsize+yposition )
polygon.GetPolyShape().Append( halfsize+xposition, -halfsize+yposition )
polygon.GetPolyShape().Append( -halfsize+xposition, -halfsize+yposition )
polygon.GetPolyShape().Append( -halfsize+xposition, halfsize+yposition )
return polygon
def _drawPixel(self, xposition, yposition):
# build a rectangular pad as a dot on copper layer,
# and a polygon (a square) on silkscreen
if self.UseCu:
pad = pcbnew.D_PAD(self.module)
pad.SetSize(pcbnew.wxSize(self.X, self.X))
pad.SetPosition(pcbnew.wxPoint(xposition,yposition))
pad.SetShape(pcbnew.PAD_SHAPE_RECT)
pad.SetAttribute(pcbnew.PAD_ATTRIB_SMD)
pad.SetName("")
layerset = pcbnew.LSET()
layerset.AddLayer(pcbnew.F_Cu)
layerset.AddLayer(pcbnew.F_Mask)
pad.SetLayerSet( layerset )
self.module.Add(pad)
if self.UseSilkS:
polygon=self.drawSquareArea(pcbnew.F_SilkS, self.X, xposition, yposition)
self.module.Add(polygon)
def BuildThisFootprint(self):
if self.border >= 0:
# Adding border: Create a new array larger than the self.qr.modules
sz = self.qr.modules.__len__() + (self.border * 2)
arrayToDraw = [ [ 0 for a in range(sz) ] for b in range(sz) ]
lineposition = self.border
for i in self.qr.modules:
columnposition = self.border
for j in i:
arrayToDraw[lineposition][columnposition] = j
columnposition += 1
lineposition += 1
else:
# No border: using array as is
arrayToDraw = self.qr.modules
# used many times...
half_number_of_elements = arrayToDraw.__len__() / 2
# Center position of QrCode
yposition = - int(half_number_of_elements * self.X)
for line in arrayToDraw:
xposition = - int(half_number_of_elements * self.X)
for pixel in line:
# Trust table for drawing a pixel
# Negative is a boolean;
# each pixel is a boolean (need to draw of not)
# Negative | Pixel | Result
# 0 | 0 | 0
# 0 | 1 | 1
# 1 | 0 | 1
# 1 | 1 | 0
# => Draw as Xor
if self.negative != pixel: # Xor...
self._drawPixel(xposition, yposition)
xposition += self.X
yposition += self.X
#int((5 + half_number_of_elements) * self.X))
textPosition = int((self.textHeight) + ((1 + half_number_of_elements) * self.X))
self.module.Value().SetPosition(pcbnew.wxPoint(0, - textPosition))
self.module.Value().SetTextHeight(self.textHeight)
self.module.Value().SetTextWidth(self.textWidth)
self.module.Value().SetThickness(self.textThickness)
self.module.Reference().SetPosition(pcbnew.wxPoint(0, textPosition))
self.module.Reference().SetTextHeight(self.textHeight)
self.module.Reference().SetTextWidth(self.textWidth)
self.module.Reference().SetThickness(self.textThickness)
self.module.Value().SetLayer(pcbnew.F_SilkS)
QRCodeWizard().register()