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

BedLevelPanel.go « ui - github.com/Z-Bolt/OctoScreen.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b96290fc9ed88b3b561bf79c90ab260d8ae89a75 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package ui

import (
	"fmt"

	"github.com/gotk3/gotk3/gtk"
	"github.com/Z-Bolt/OctoScreen/interfaces"
	"github.com/Z-Bolt/OctoScreen/logger"
	"github.com/Z-Bolt/OctoScreen/octoprintApis"
	// "github.com/Z-Bolt/OctoScreen/octoprintApis/dataModels"
	"github.com/Z-Bolt/OctoScreen/utils"
)

var bedLevelPanelInstance *bedLevelPanel

type bedLevelPanel struct {
	CommonPanel

	innerGrid			*gtk.Grid
	points				map[string][]float64
	homed				bool
}

func BedLevelPanel(
	ui				*UI,
	parentPanel		interfaces.IPanel,
) *bedLevelPanel {
	if bedLevelPanelInstance == nil {
		instance := &bedLevelPanel {
			CommonPanel: NewCommonPanel(ui, parentPanel),
		}
		instance.initialize()
		bedLevelPanelInstance = instance
	}

	bedLevelPanelInstance.homed = false

	return bedLevelPanelInstance
}

func (this *bedLevelPanel) initialize() {
	defer this.Initialize()

	this.defineLevelingPoints()

	this.innerGrid = utils.MustGrid()
	this.innerGrid.SetRowHomogeneous(true)
	this.innerGrid.SetColumnHomogeneous(true)
	this.Grid().Attach(this.innerGrid, 1, 0, 2, 3)


	this.addBedLevelCornerButton(true, true)
	this.addBedLevelCornerButton(false, true)
	this.addBedLevelCornerButton(true, false)
	this.addBedLevelCornerButton(false, false)

	if this.UI.Settings != nil && this.UI.Settings.GCodes.AutoBedLevel != "" {
		autoLevelButton := this.createAutoLevelButton(this.UI.Settings.GCodes.AutoBedLevel)
		this.Grid().Attach(autoLevelButton, 3, 0, 1, 1)
	}
}

func (this *bedLevelPanel) addBedLevelCornerButton(isLeft, isTop bool) {
	x := 0
	y := 1
	placement := "b-"
	if isTop {
		placement = "t-"
		y = 0
	}

	if isLeft {
		placement += "l"
	} else {
		placement += "r"
		x = 1
	}

	button := this.createLevelButton(placement)
	if isLeft {
		button.SetHAlign(gtk.ALIGN_END)
	} else {
		button.SetHAlign(gtk.ALIGN_START)
	}

	if isTop {
		button.SetVAlign(gtk.ALIGN_END)
		button.SetImagePosition(gtk.POS_BOTTOM)
	} else {
		button.SetVAlign(gtk.ALIGN_START)
		button.SetImagePosition(gtk.POS_TOP)
	}

	styleContext, _ := button.GetStyleContext()
	styleContext.AddClass("no-margin")
	styleContext.AddClass("no-border")
	styleContext.AddClass("no-padding")

	if isLeft {
		styleContext.AddClass("padding-left-20")
	} else {
		styleContext.AddClass("padding-right-20")
	}

	this.innerGrid.Attach(button, x, y, 1, 1)
}

func (this *bedLevelPanel) defineLevelingPoints() {
	logger.TraceEnter("BedLevelPanel.defineLevelingPoints()")

	connectionResponse, err := (&octoprintApis.ConnectionRequest{}).Do(this.UI.Client)
	if err != nil {
		logger.LogError("BedLevelPanel.defineLevelingPoints()", "version.Get()", err)
		logger.TraceLeave("BedLevelPanel.defineLevelingPoints()")
		return
	}

	logger.Info(connectionResponse.Current.PrinterProfile)

	printerProfile, err := (&octoprintApis.PrinterProfilesRequest{Id: connectionResponse.Current.PrinterProfile}).Do(this.UI.Client)
	if err != nil {
		logger.LogError("BedLevelPanel.defineLevelingPoints()", "Do(PrinterProfilesRequest)", err)
		logger.TraceLeave("BedLevelPanel.defineLevelingPoints()")
		return
	}

	xMax := printerProfile.Volume.Width
	yMax := printerProfile.Volume.Depth
	xOffset := xMax * 0.1
	yOffset := yMax * 0.1

	this.points = map[string][]float64 {
		"t-l": {xOffset, yMax - yOffset},
		"t-r": {xMax - xOffset, yMax - yOffset},
		"b-l": {xOffset, yOffset},
		"b-r": {xMax - xOffset, yOffset},
	}

	logger.TraceLeave("BedLevelPanel.defineLevelingPoints()")
}

func (this *bedLevelPanel) createLevelButton(placement string) *gtk.Button {
	imageFileName := fmt.Sprintf("bed-level-parts/bed-corner-%s.svg", placement)
	noLabel := ""
	button := utils.MustButtonImage(noLabel, imageFileName, func() {
		this.goHomeIfRequired()

		cmd := &octoprintApis.CommandRequest{}
		cmd.Commands = []string {
			"G0 Z10 F2000",
			fmt.Sprintf("G0 X%f Y%f F10000", this.points[placement][0], this.points[placement][1]),
			"G0 Z0 F400",
		}

		if err := cmd.Do(this.UI.Client); err != nil {
			logger.LogError("BedLevelPanel.createLevelButton()", "Do(CommandRequest)", err)
			return
		}
	})

	return button
}

func (this *bedLevelPanel) goHomeIfRequired() {
	if this.homed {
		return
	}

	cmd := &octoprintApis.CommandRequest{}
	cmd.Commands = []string{
		"G28",
	}

	if err := cmd.Do(this.UI.Client); err != nil {
		logger.LogError("BedLevelPanel.goHomeIfRequire()", "Do(CommandRequest)", err)
		return
	}

	this.homed = true
}

func (this *bedLevelPanel) createAutoLevelButton(gcode string) *gtk.Button {
	button := utils.MustButtonImage("Auto Level", "bed-level.svg", func() {
		cmd := &octoprintApis.CommandRequest{}
		cmd.Commands = []string{
			gcode,
		}

		if err := cmd.Do(this.UI.Client); err != nil {
			logger.LogError("BedLevelPanel.createAutoLevelButton()", "Do(CommandRequest)", err)
			return
		}
	})

	return button
}