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

Filament.cpp « Tools « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5e2e8ff491a56c092360dd4eaa42c6857c135fd2 (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
/*
 * Filament.cpp
 *
 *  Created on: 13 Jun 2017
 *      Author: Christian
 */

#include "Tool.h"
#include "Filament.h"

#include "RepRap.h"
#include "Platform.h"

#include <ctime>

const char * const Filament::FilamentAssignmentFile = "filaments.csv";
const char * const Filament::FilamentAssignmentFileComment = "RepRapFirmware filament assignment file v1";

Filament *Filament::filamentList = nullptr;


Filament::Filament(int extr) : extruder(extr)
{
	strcpy(name, "");

	next = filamentList;
	filamentList = this;
}

void Filament::Load(const char *filamentName)
{
	SafeStrncpy(name, filamentName, ARRAY_SIZE(name));
	Filament::SaveAssignments();
}

void Filament::Unload()
{
	strcpy(name, "");
	Filament::SaveAssignments();
}

void Filament::LoadAssignment()
{
#if HAS_MASS_STORAGE
	FileStore *file = reprap.GetPlatform().OpenSysFile(FilamentAssignmentFile, OpenMode::read);
	if (file == nullptr)
	{
		// May happen, but not critical
		return;
	}

	char buffer[FilamentNameLength + 64];
	if (file->ReadLine(buffer, sizeof(buffer)) > 0 && StringStartsWith(buffer, FilamentAssignmentFileComment))
	{
		if (file->ReadLine(buffer, sizeof(buffer)) > 0)
		{
			while (file->ReadLine(buffer, sizeof(buffer)) > 0)
			{
				if (isdigit(buffer[0]) && atoi(buffer) == extruder)
				{
					const char *filament = buffer;
					while (*filament != 0)
					{
						if (*filament++ == ',')
						{
							break;
						}
					}

					SafeStrncpy(name, filament, ARRAY_SIZE(name));
					break;
				}
			}
		}
	}

	file->Close();
#endif
}

/*static*/ void Filament::SaveAssignments()
{
#if HAS_MASS_STORAGE
	FileStore * const file = reprap.GetPlatform().OpenSysFile(FilamentAssignmentFile, OpenMode::write);
	if (file == nullptr)
	{
		// Should never happen
		return;
	}

	char bufferSpace[FilamentNameLength + 64];
	StringRef buf(bufferSpace, ARRAY_SIZE(bufferSpace));

	// Write header
	buf.copy(FilamentAssignmentFileComment);
	if (reprap.GetPlatform().IsDateTimeSet())
	{
		time_t timeNow = reprap.GetPlatform().GetDateTime();
		const struct tm * const timeInfo = gmtime(&timeNow);
		buf.catf(" generated at %04u-%02u-%02u %02u:%02u",
						timeInfo->tm_year + 1900, timeInfo->tm_mon + 1, timeInfo->tm_mday, timeInfo->tm_hour, timeInfo->tm_min);
	}
	buf.cat('\n');
	file->Write(buf.c_str());

	// Write column headers and one row for each loaded filament
	file->Write("extruder,filament\n");
	for (Filament *f = filamentList; f != nullptr; f = f->next)
	{
		if (f->IsLoaded())
		{
			buf.printf("%d,%s\n", f->GetExtruder(), f->GetName());
			file->Write(buf.c_str());
		}
	}

	file->Close();
#endif
}

/*static*/ bool Filament::IsInUse(const char *filamentName)
{
	for (Filament *f = filamentList; f != nullptr; f = f->next)
	{
		if (StringEqualsIgnoreCase(f->name, filamentName))
		{
			return true;
		}
	}
	return false;
}

/*static*/ Filament *Filament::GetFilamentByExtruder(const int drive)
{
	for (Filament *f = filamentList; f != nullptr; f = f->next)
	{
		if (f->GetExtruder() == drive)
		{
			return f;
		}
	}
	return nullptr;
}

// End