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

CanMessageGenericConstructor.h « CAN « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 50969ce73b0d85121057689ca392325e5376e9c6 (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
/*
 * CanMessageGenericConstructor.h
 *
 *  Created on: 23 Jul 2019
 *      Author: David
 */

#ifndef SRC_CANMESSAGEGENERICCONSTRUCTOR_H_
#define SRC_CANMESSAGEGENERICCONSTRUCTOR_H_

#include "RepRapFirmware.h"

#if SUPPORT_CAN_EXPANSION

#include <CanMessageFormats.h>
#include <GCodes/GCodeResult.h>
#include <GCodes/GCodeException.h>

class GCodeBuffer;

class CanMessageGenericConstructor
{
public:
	CanMessageGenericConstructor(const ParamDescriptor *p_param) noexcept;

	// Populate from a GCode message. Throws if an error occurs.
	void PopulateFromCommand(GCodeBuffer& gb) THROWS(GCodeException);

	// Methods to add parameters
	void AddU64Param(char c, uint64_t v) THROWS(GCodeException);
	void AddUParam(char c, uint32_t v) THROWS(GCodeException);
	void AddIParam(char c, int32_t v) THROWS(GCodeException);
	void AddFParam(char c, float v) THROWS(GCodeException);
	void AddCharParam(char c, char v) THROWS(GCodeException);
	void AddStringParam(char c, const char* v) THROWS(GCodeException);
	void AddDriverIdParam(char c, DriverId did) THROWS(GCodeException);

	GCodeResult SendAndGetResponse(CanMessageType msgType, CanAddress dest, const StringRef& reply) noexcept;

private:
	// Return the correct position in the data to insert a parameter. If successful, add the bit to the parameter map and pass back the expected parameter type and size; else throw.
	unsigned int FindInsertPoint(char c, ParamDescriptor::ParamType& t, size_t &sz) THROWS(GCodeException);

	// Append a value to the data, returning true if it wouldn't fit
	void StoreValue(const void *vp, size_t sz) THROWS(GCodeException);

	// Append a value to the data, returning true if it wouldn't fit
	template<class T> void StoreValue(const T& val) THROWS(GCodeException) { StoreValue(&val, sizeof(T)); }

	// Insert a value in the data, returning true if it wouldn't fit
	void InsertValue(const void *vp, size_t sz, size_t pos) THROWS(GCodeException);

	static GCodeException ConstructParseException(const char *errMsg) noexcept
	{
		return GCodeException(-1, -1, errMsg);
	}

	static GCodeException ConstructParseException(const char *errMsg, uint32_t param) noexcept
	{
		return GCodeException(-1, -1, errMsg, param);
	}

	const ParamDescriptor * const paramTable;
	size_t dataLen;
	CanMessageGeneric msg;
};

#endif

#endif /* SRC_CANMESSAGEGENERICCONSTRUCTOR_H_ */