Skip to content

Commit

Permalink
feat: adds "gcode" Klipper module status support
Browse files Browse the repository at this point in the history
Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
  • Loading branch information
pedrolamas committed Dec 12, 2023
1 parent ea7d103 commit fee7afd
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 47 deletions.
4 changes: 0 additions & 4 deletions src/components/widgets/console/Console.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ export default class Console extends Mixins(StateMixin) {
_pauseScroll = false
get availableCommands () {
return this.$store.getters['console/getAllGcodeCommands']
}
get currentCommand () {
return this.$store.state.console.consoleCommand
}
Expand Down
16 changes: 10 additions & 6 deletions src/components/widgets/console/ConsoleCommand.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
</template>

<script lang="ts">
import type { GcodeCommands } from '@/store/console/types'
import { Vue, Component, Prop, Watch, Ref } from 'vue-property-decorator'
import { Globals } from '@/globals'
import type { VInput } from '@/types'
import type { GcodeCommands } from '@/store/printer/types'
@Component({})
export default class ConsoleCommand extends Vue {
Expand Down Expand Up @@ -118,18 +118,22 @@ export default class ConsoleCommand extends Vue {
}
}
get availableCommands (): GcodeCommands {
return this.$store.getters['printer/getAvailableCommands'] as GcodeCommands
}
autoComplete () {
const gcodeCommands: GcodeCommands = this.$store.getters['console/getAllGcodeCommands']
const availableCommands = this.availableCommands
if (this.newValue.length) {
const commands = Object.keys(gcodeCommands)
.filter(command => command.toLowerCase().startsWith(this.newValue.toLowerCase()))
const commands = Object.keys(availableCommands)
.filter(command => command.startsWith(this.newValue.toUpperCase()))
if (commands.length === 1) {
this.emitChange(commands[0])
} else {
commands.forEach((c) => {
const message = `// ${c}: ${gcodeCommands[c]}`
commands.forEach(command => {
const message = `// ${command}: ${availableCommands[command].help ?? ''}`
this.$store.dispatch('console/onAddConsoleEntry', { message, type: 'response' })
})
}
Expand Down
7 changes: 3 additions & 4 deletions src/components/widgets/console/ConsoleItem.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator'
import { Globals } from '@/globals'
import type { ConsoleEntry } from '@/store/console/types'
import type { ConsoleEntry, GcodeHelp } from '@/store/console/types'
@Component({})
export default class ConsoleItem extends Vue {
@Prop({ type: Object, default: () => {} })
readonly value!: ConsoleEntry
get knownCommands () {
const availableCommands = this.$store.getters['console/getAllGcodeCommands']
return new Set(Object.keys(availableCommands))
return this.$store.getters['console/getAllKnownCommands'] as GcodeHelp
}
get itemMessage () {
let message = this.value.message
if (this.value.type === 'response') {
message = this.value.message.replace(/([A-Z0-9_]{2,})/gm, (match, command) => {
if (this.knownCommands.has(command)) return `<a class="primary--text text--lighten-1">${command.toUpperCase()}</a>`
if (command in this.knownCommands) return `<a class="primary--text text--lighten-1">${command.toUpperCase()}</a>`
return match
})
}
Expand Down
15 changes: 11 additions & 4 deletions src/components/widgets/toolhead/ToolChangeCommands.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,31 @@
<script lang="ts">
import { Component, Mixins } from 'vue-property-decorator'
import StateMixin from '@/mixins/state'
import type { GcodeCommands } from '@/store/printer/types'
import type { TranslateResult } from 'vue-i18n'
type ToolChangeCommand = {
name: string,
description: string,
description: string | TranslateResult,
color?: string,
active?: boolean
}
@Component({})
export default class ToolChangeCommands extends Mixins(StateMixin) {
get availableCommands (): GcodeCommands {
return this.$store.getters['printer/getAvailableCommands'] as GcodeCommands
}
get toolChangeCommands (): ToolChangeCommand[] {
const availableCommands = this.$store.state.console.availableCommands
const availableCommands = this.availableCommands
return Object.keys(availableCommands)
.filter(command => /^t\d+$/i.test(command))
.map(command => {
const description = availableCommands[command] !== 'G-Code macro'
? availableCommands[command]
const { help } = availableCommands[command]
const description = help && help !== 'G-Code macro'
? help
: this.$t('app.tool.tooltip.select_tool', { tool: command.substring(1) })
const macro = this.$store.getters['macros/getMacroByName'](command.toLowerCase())
Expand Down
15 changes: 4 additions & 11 deletions src/components/widgets/toolhead/ZHeightAdjust.vue
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
<script lang="ts">
import { Component, Mixins } from 'vue-property-decorator'
import StateMixin from '@/mixins/state'
import type { GcodeCommands } from '@/store/console/types'
import type { GcodeCommands } from '@/store/printer/types'
@Component({})
export default class ZHeightAdjust extends Mixins(StateMixin) {
Expand Down Expand Up @@ -173,22 +173,15 @@ export default class ZHeightAdjust extends Mixins(StateMixin) {
}
get availableCommands (): GcodeCommands {
return this.$store.state.console.availableCommands as GcodeCommands
}
get availableCommandNames (): Set<string> {
const availableCommandNames = new Set(Object.keys(this.availableCommands)
.map(commandName => commandName.toUpperCase()))
return availableCommandNames
return this.$store.getters['printer/getAvailableCommands'] as GcodeCommands
}
get hasZOffsetApplyProbe (): boolean {
return this.availableCommandNames.has('Z_OFFSET_APPLY_PROBE')
return 'Z_OFFSET_APPLY_PROBE' in this.availableCommands
}
get hasZOffsetApplyEndstop (): boolean {
return this.availableCommandNames.has('Z_OFFSET_APPLY_ENDSTOP')
return 'Z_OFFSET_APPLY_ENDSTOP' in this.availableCommands
}
/**
Expand Down
21 changes: 8 additions & 13 deletions src/store/console/getters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { GetterTree } from 'vuex'
import type { ConsoleState } from './types'
import type { ConsoleState, GcodeHelp } from './types'
import type { RootState } from '../types'

const _tempWaitExpr = /^(?:ok\s+)?(b|t\d+):\d+\.\d+ \/\d+\.+\d+/i
Expand All @@ -24,19 +24,14 @@ export const getters: GetterTree<ConsoleState, RootState> = {
return state.consoleFilters
},

getAllGcodeCommands: (state) => {
const commands = state.availableCommands
const additional = [
'TESTZ',
'ABORT',
'ACCEPT',
'ADJUSTED'
]
additional.forEach(command => {
if (command in commands !== true) {
commands[command] = ''
getAllKnownCommands: (state): GcodeHelp => {
const commands = state.gcodeHelp

for (const extraCommand of ['TESTZ', 'ABORT', 'ACCEPT', 'ADJUSTED']) {
if (extraCommand in commands !== true) {
commands[extraCommand] = ''
}
})
}

return commands
}
Expand Down
2 changes: 1 addition & 1 deletion src/store/console/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const mutations: MutationTree<ConsoleState> = {
* Defines the list of available commands
*/
setGcodeHelp (state, payload) {
Vue.set(state, 'availableCommands', payload)
Vue.set(state, 'gcodeHelp', payload)
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/store/console/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const defaultState = (): ConsoleState => {
consoleCommand: '',
consoleEntryCount: 0,
console: [],
availableCommands: {},
gcodeHelp: {},
commandHistory: [],
autoScroll: true,
lastCleared: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/store/console/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface ConsoleState {
// [key: string]: string;
consoleCommand: string;
console: ConsoleEntry[]; // console stream
availableCommands: GcodeCommands; // available gcode commands
gcodeHelp: GcodeHelp; // known commands
consoleEntryCount: number; // give each console entry a unique id.
commandHistory: string[];
autoScroll: boolean;
Expand All @@ -19,7 +19,7 @@ export interface ConsoleEntry {
time?: number;
}

export interface GcodeCommands {
export interface GcodeHelp {
[key: string]: string;
}

Expand Down
20 changes: 19 additions & 1 deletion src/store/printer/getters.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Vue from 'vue'
import type { GetterTree } from 'vuex'
import type { RootState } from '../types'
import type { PrinterState, Heater, Fan, Led, OutputPin, Sensor, RunoutSensor, KnownExtruder, MCU, Endstop, Probe, ExtruderStepper, Extruder, ExtruderConfig, ProbeName, Stepper, ScrewsTiltAdjustScrew, ScrewsTiltAdjust, BedScrews, BedSize } from './types'
import type { PrinterState, Heater, Fan, Led, OutputPin, Sensor, RunoutSensor, KnownExtruder, MCU, Endstop, Probe, ExtruderStepper, Extruder, ExtruderConfig, ProbeName, Stepper, ScrewsTiltAdjustScrew, ScrewsTiltAdjust, BedScrews, BedSize, GcodeCommands } from './types'
import { get } from 'lodash-es'
import getKlipperType from '@/util/get-klipper-type'
import i18n from '@/plugins/i18n'
import type { GcodeHelp } from '../console/types'

export const getters: GetterTree<PrinterState, RootState> = {

Expand Down Expand Up @@ -933,6 +934,23 @@ export const getters: GetterTree<PrinterState, RootState> = {
}
},

getAvailableCommands: (state, getters, rootState, rootGetters): GcodeCommands => {
const availableCommands = state.printer.gcode.commands as GcodeCommands | null

if (availableCommands) {
return availableCommands
}

const knownCommands = rootGetters['console/getAllKnownCommands'] as GcodeHelp

return Object.entries(knownCommands)
.reduce((availableCommands, [key, help]) => {
availableCommands[key] = { help }

return availableCommands
}, {} as GcodeCommands)
},

getIsManualProbeActive: (state) => {
return state.printer.manual_probe?.is_active || false
},
Expand Down
3 changes: 3 additions & 0 deletions src/store/printer/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ export const defaultState = (): PrinterState => {
filament_total: 0,
thumbnails: []
},
gcode: {
commands: null
},
gcode_move: {
gcode_position: [0, 0, 0, 0],
homing_origin: [],
Expand Down
8 changes: 8 additions & 0 deletions src/store/printer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,11 @@ export interface BedSize {
maxX: number;
maxY: number;
}

export interface GcodeCommands {
[key: string]: GcodeCommand
}

export interface GcodeCommand {
help?: string
}

0 comments on commit fee7afd

Please sign in to comment.