gcodeblock

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2022 License: MIT Imports: 9 Imported by: 3

Documentation

Overview

gcodeblock is an implementation of block package.

This package define GcodeBlock struct as a implemention of block.Blocker interface.

Furemore, it defines two package functions that allows create new instances of GcodeBlock. These functions can be used to a any instances that implement block.BlockerFactory

This file defines a Blockconfigurer as an object that implement block.BlockerConfigurer interface to allow the caller to configure the new blocks.

Improve self-reference function to design options pattern providing the BlockConfigurer struct to set configs.

Index

Examples

Constants

View Source
const (
	// BLOC_SEPARATOR defines a string used to separate the sections of the block when is exported as line string format
	BLOCK_SEPARATOR = " "
)

Variables

This section is empty.

Functions

This section is empty.

Types

type GcodeBlock

type GcodeBlock struct {
	// contains filtered or unexported fields
}

GcodeBlock struct represents a single gcode block.

Stores data and gcode expressions of each section of the block.

His methods allow export of the block as a line string format or check of the integrity.

To be constructed using the Parse function from a line of the gcode file.

func New

New return a new block instance with the configurations wishes.

command is a gcode with address or not that define the block command. options are a series of configuration callbacks to allow set different aspects of the block. each option provides a config object that can be used to load the values that define the block.

Example
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block"
	"github.com/mauroalderete/gcode-core/block/gcodeblock"
	"github.com/mauroalderete/gcode-core/gcode"
	"github.com/mauroalderete/gcode-core/gcode/addressablegcode"
)

func main() {

	// create a new command
	command, err := addressablegcode.New[int32]('G', 1)
	if err != nil {
		fmt.Printf("got error not nil, want error nil: %v", err)
		return
	}

	// try create a new block with command G1
	b, err := gcodeblock.New(command, func(config block.BlockConstructorConfigurer) error {

		// set line number N7
		lineNumber, err := addressablegcode.New[uint32]('N', 7)
		if err != nil {
			return fmt.Errorf("got error not nil, want error nil: %v", err)
		}
		config.SetLineNumber(lineNumber)

		// set all parameters X2 Y2 F3000
		p1, err := addressablegcode.New[float32]('X', 2)
		if err != nil {
			return fmt.Errorf("got error not nil, want error nil: %v", err)
		}
		p2, err := addressablegcode.New[float32]('Y', 2)
		if err != nil {
			return fmt.Errorf("got error not nil, want error nil: %v", err)
		}
		p3, err := addressablegcode.New[float32]('F', 3000)
		if err != nil {
			return fmt.Errorf("got error not nil, want error nil: %v", err)
		}
		params := []gcode.Gcoder{p1, p2, p3}
		config.SetParameters(params)

		// set a comment
		config.SetComment(";lorem ipsum")

		return nil
	})
	if err != nil {
		fmt.Printf("failed to make a new gcodeblock: %v", err)
		return
	}

	// calculate the checksum and update the block with the new value
	err = b.UpdateChecksum()
	if err != nil {
		fmt.Printf("failed to update: %v", err)
		return
	}

	fmt.Println(b.ToLine("%l %c %p%k %m"))

}
Output:

N7 G1 X2.000 Y2.000 F3000.000*85 ;lorem ipsum

func Parse

Parse returns a new block instance with the configurations we wish, from a single block line. The block line must contain the correct format, on the contrary, the parsing process will end with an error.

source is the string line to parse. options are a series of configuration callbacks to allow set different aspects of the block. each option provides a config object that can be used to load the values that define the block.

Example
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Printf("line is: %s\n", b.String())

}
Output:

line is: N7 G1 X2.000 Y2.000 F3000.000

func (*GcodeBlock) CalculateChecksum

func (b *GcodeBlock) CalculateChecksum() (gcode.AddressableGcoder[uint32], error)

CalculateChecksum calculates a checksum from the block and returns a new GcodeAddressable[uint32] with the value computed.

Example
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	gc, err := b.CalculateChecksum()
	if err != nil {
		fmt.Printf("%s\n", err.Error())
		return
	}

	fmt.Printf("the gcode checksum is: %s\n", gc)

}
Output:

the gcode checksum is: *85

func (*GcodeBlock) Checksum

func (b *GcodeBlock) Checksum() gcode.AddressableGcoder[uint32]

Checksum returns a GcodeAddressable[uint32] if the line of the block contained, else returns nil.

The value of the address is calculated using the hash instances stored.

func (*GcodeBlock) Command

func (b *GcodeBlock) Command() gcode.Gcoder

Command returns a gcoder struct. Can be addressable or not.

Represent the first gcode expression and main significance of the block. Always is present.

Example
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	if b.Command() == nil {
		fmt.Println("command isn't available")
		return
	}

	fmt.Printf("command is: %s\n", b.Command().String())

}
Output:

command is: G1

func (*GcodeBlock) Comment

func (b *GcodeBlock) Comment() string

Comment returns the string with the comment of the block. Or nil if there isn't one.

Is an expression attached at the block with some comment. Can be empty.

Example
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Printf("comment len is: %d\n", len(b.Comment()))

}
Output:

comment len is: 0

func (*GcodeBlock) LineNumber

func (b *GcodeBlock) LineNumber() gcode.AddressableGcoder[uint32]

LineNumber returns a gcode addressable of the int32 type.

Represent the line number of the block. It can be null. Always has an int32 type address.

Example
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	if b.LineNumber() == nil {
		fmt.Println("line number isn't available")
		return
	}

	fmt.Printf("line number is: %v\n", b.LineNumber().Address())

}
Output:

line number is: 7

func (*GcodeBlock) Parameters

func (b *GcodeBlock) Parameters() []gcode.Gcoder

Parameters return a list of gcoder structs. Each gcoder can be addressable or not.

Parameters is a list of the rest of the gcode expression that adds information to the command. Can be empty.

Example
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	if b.Parameters() == nil {
		fmt.Println("parameters aren't available")
		return
	}

	for i, p := range b.Parameters() {
		fmt.Printf("[%v]: %s\n", i, p.String())
	}

}
Output:

[0]: X2.000
[1]: Y2.000
[2]: F3000.000

func (*GcodeBlock) String

func (b *GcodeBlock) String() string

String returns the block exported as single-line string format including check and comments section.

It is the same invoke ToLine method

Example
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Printf("line is: %s\n", b.String())

}
Output:

line is: N7 G1 X2.000 Y2.000 F3000.000

func (*GcodeBlock) ToLine

func (b *GcodeBlock) ToLine(format string) string

ToLine export the block as a single-line string format

format is a string that contain verbs to define the place of each element of the block. The verbs available are:

%l: linenumber gcode of the block

%c: command gcode of the block

%p: series of parameters gcode of the command gcode

%k: checksum gcode of the useful part of the block

%m: comments of the block

We can used the format string to determine how each element is showing. For example:

The line generated depends on the available of elements contained in the block. If any element isn't available then is ignored.

Example
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Printf("line is: %s\n", b.ToLine("%c %p"))

}
Output:

line is: G1 X2.000 Y2.000 F3000.000
Example (Second)
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Printf("line is: %s\n", b.ToLine("%l %c %p%k"))

}
Output:

line is: N7 G1 X2.000 Y2.000 F3000.000
Example (Third)
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	fmt.Printf("line is: %s\n", b.ToLine("%m"))

}
Output:

line is:

func (*GcodeBlock) UpdateChecksum

func (b *GcodeBlock) UpdateChecksum() error

UpdateChecksum calculates a checksum from the block and stores him in as a new checksum gcode.

Example
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	err = b.UpdateChecksum()
	if err != nil {
		fmt.Printf("%s\n", err.Error())
		return
	}

	fmt.Printf("the block with checksum is: %s\n", b.ToLine("%l %c %p%k"))

}
Output:

the block with checksum is: N7 G1 X2.000 Y2.000 F3000.000*85

func (*GcodeBlock) VerifyChecksum

func (b *GcodeBlock) VerifyChecksum() (bool, error)

VerifyChecksum calculates a checksum and compare him with the checksum stored in the block, it returns true if both matches.

Example
package main

import (
	"fmt"

	"github.com/mauroalderete/gcode-core/block/gcodeblock"
)

func main() {
	const source = "N7 G1 X2.0 Y2.0 F3000.0"

	b, err := gcodeblock.Parse(source)
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	checked, err := b.VerifyChecksum()

	if err != nil {
		fmt.Printf("%s\n", err.Error())
		return
	}

	fmt.Printf("the block is verified: %v\n", checked)

}
Output:

the block 'N7 G1 X2.000 Y2.000 F3000.000' hasn't check section

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL