~tristan957/tllt-cp

ref: b5ebdb59e4b651db5ee8bd135f2a2a1cf0826c23 tllt-cp/driver/tllt-toaster.c -rw-r--r-- 11.2 KiB
b5ebdb59 — Tristan Partin Remove the UI elements that don't do anything 1 year, 6 months ago
                                                                                
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <gio/gio.h>
#include <glib-object.h>
#include <glib/gi18n.h>
#include <json-glib/json-glib.h>
#ifdef TLLT_WITH_WIRINGPI
#	include <wiringPi.h>
#endif

#include "components/tllt-heating-element.h"
#include "sensors/tllt-thermistor.h"
#include "tllt-config.h"
#include "tllt-error.h"
#include "tllt-powerable.h"
#include "tllt-toaster.h"

typedef struct TlltToasterOperationArgs
{
	TlltToaster *toaster;
	int temperature;
	int total_time_seconds;
	unsigned int ref_count;
	TlltToasterUpdateFunc update;
	gpointer user_data;
} TlltToasterOperationArgs;

typedef struct TlltToasterPrivate
{
	gboolean running;
	GCancellable *cancellable;
	GTimer *timer;
} TlltToasterPrivate;

G_DEFINE_TYPE_WITH_PRIVATE(TlltToaster, tllt_toaster, G_TYPE_OBJECT)

typedef enum TlltToasterProps
{
	PROP_THERMISTOR = 1,
	PROP_TOP_HEATING_ELEMENT,
	PROP_BOTTOM_HEATING_ELEMENT,
	N_PROPS,
} TlltToasterProps;

static GParamSpec *obj_properties[N_PROPS];

typedef enum TlltToasterSignals
{
	SIGNAL_PREPARING,
	SIGNAL_STARTED,
	SIGNAL_STOPPED,
	N_SIGNALS,
} TlltToasterSignals;

static guint obj_signals[N_SIGNALS];

static void
tllt_toaster_get_property(GObject *obj, guint prop_id, GValue *val, GParamSpec *pspec)
{
	TlltToaster *self = TLLT_TOASTER(obj);

	switch (prop_id) {
	case PROP_THERMISTOR:
		g_value_set_object(val, self->thermistor);
		break;
	case PROP_TOP_HEATING_ELEMENT:
		g_value_set_object(val, self->top_heating_element);
		break;
	case PROP_BOTTOM_HEATING_ELEMENT:
		g_value_set_object(val, self->bottom_heating_element);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void
tllt_toaster_set_property(GObject *obj, guint prop_id, const GValue *val, GParamSpec *pspec)
{
	TlltToaster *self = TLLT_TOASTER(obj);

	switch (prop_id) {
	case PROP_THERMISTOR:
		if (self->thermistor != NULL) {
			g_object_unref(self->thermistor);
		}
		self->thermistor = g_value_dup_object(val);
		break;
	case PROP_TOP_HEATING_ELEMENT:
		if (self->top_heating_element != NULL) {
			g_object_unref(self->top_heating_element);
		}
		self->top_heating_element = g_value_dup_object(val);
		break;
	case PROP_BOTTOM_HEATING_ELEMENT:
		if (self->bottom_heating_element != NULL) {
			g_object_unref(self->bottom_heating_element);
		}
		self->bottom_heating_element = g_value_dup_object(val);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, prop_id, pspec);
	}
}

static void
tllt_toaster_finalize(GObject *obj)
{
	TlltToaster *self		 = TLLT_TOASTER(obj);
	TlltToasterPrivate *priv = tllt_toaster_get_instance_private(self);

	g_cancellable_cancel(priv->cancellable);

	g_object_unref(priv->cancellable);
	g_object_unref(self->thermistor);
	g_object_unref(self->top_heating_element);
	g_object_unref(self->bottom_heating_element);
	g_timer_destroy(priv->timer);

	G_OBJECT_CLASS(tllt_toaster_parent_class)->finalize(obj);
}

static void
tllt_toaster_class_init(TlltToasterClass *klass)
{
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->finalize		= tllt_toaster_finalize;
	obj_class->get_property = tllt_toaster_get_property;
	obj_class->set_property = tllt_toaster_set_property;

	obj_properties[PROP_THERMISTOR] =
		g_param_spec_object("thermistor", _("Thermister"), _("Pointer to thermistor object"),
							TLLT_TYPE_THERMISTOR, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
	obj_properties[PROP_TOP_HEATING_ELEMENT] = g_param_spec_object(
		"top-heating-element", _("Top heating element"), _("Top heating element"),
		TLLT_TYPE_HEATING_ELEMENT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
	obj_properties[PROP_BOTTOM_HEATING_ELEMENT] = g_param_spec_object(
		"bottom-heating-element", _("Bottom heating element"), _("Bottom heating element"),
		TLLT_TYPE_HEATING_ELEMENT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

	g_object_class_install_properties(obj_class, N_PROPS, obj_properties);

	obj_signals[SIGNAL_PREPARING] =
		g_signal_new("preparing", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
					 G_TYPE_NONE, 0);
	obj_signals[SIGNAL_STARTED] =
		g_signal_new("started", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
					 G_TYPE_NONE, 0);
	obj_signals[SIGNAL_STOPPED] =
		g_signal_new("stopped", G_TYPE_FROM_CLASS(klass), G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL,
					 G_TYPE_NONE, 0);
}

static void
tllt_toaster_init(TlltToaster *self)
{
	TlltToasterPrivate *priv = tllt_toaster_get_instance_private(self);

	priv->timer = g_timer_new();
}

TlltToaster *
tllt_toaster_new_from_config_file(const char *file_path, GError **err)
{
	g_return_val_if_fail(err == NULL || *err == NULL, NULL);

	if (file_path == NULL) {
		g_set_error(err, PACKAGE_DOMAIN, ERROR_ENV, "No config file path set");
		return NULL;
	}

	char *buffer;
	if (!g_file_get_contents(file_path, &buffer, NULL, err)) {
		return NULL;
	}

	GObject *obj = json_gobject_from_data(TLLT_TYPE_TOASTER, buffer, -1, err);
	if (*err != NULL) {
		return NULL;
	}

	return TLLT_TOASTER(obj);
}

void
tllt_toaster_stop(TlltToaster *self)
{
	TlltToasterPrivate *priv = tllt_toaster_get_instance_private(self);

	g_cancellable_cancel(priv->cancellable);
}

static void
tllt_toaster_operations_args_unref(TlltToasterOperationArgs *args)
{
	args->ref_count--;
	if (args->ref_count == 0) {
		tllt_powerable_off(TLLT_POWERABLE(args->toaster->top_heating_element));
		tllt_powerable_off(TLLT_POWERABLE(args->toaster->bottom_heating_element));
		g_free(args);
	}
}

static gboolean
update_toaster(gpointer user_data)
{
	TlltToasterOperationArgs *args = user_data;
	TlltToasterPrivate *priv	   = tllt_toaster_get_instance_private(args->toaster);

	const int delta = args->total_time_seconds - g_timer_elapsed(priv->timer, NULL);
	if (delta > 0 && !g_cancellable_is_cancelled(priv->cancellable)) {
		args->update(delta, 1 - ((double) delta) / args->total_time_seconds, args->user_data);

		return TRUE;
	}

	return FALSE;
}

static void
update_toaster_destroy(gpointer user_data)
{
	TlltToasterOperationArgs *args = user_data;
	TlltToasterPrivate *priv	   = tllt_toaster_get_instance_private(args->toaster);

	priv->running = FALSE;
	g_signal_emit(args->toaster, obj_signals[SIGNAL_STOPPED], 0);

	// Cancel other timeouts if they exist
	g_cancellable_cancel(priv->cancellable);

	g_timer_reset(priv->timer);
	g_object_unref(priv->cancellable);
	g_object_unref(args->toaster);
	tllt_toaster_operations_args_unref(args);
}

/**
 * This is the function for controlling how the toaster will stay at a consistant temperature.
 * It is essentially a state machine. There are 3 states, which can be found in TlltThermistor.h.
 * Starting in a read state, the value is read from the thermistor. If it is higher than the
 * necessary temperature, the heating elements are turned off, and the state is set to read again,
 * so we always recheck every X seconds if the heating elements are off. In the case where the
 * sensor reading is lower than what we want, the heating elements are powered on and the state is
 * set to no read. On the no read state, the state then moves to a waiting state, so that the
 * heating elements can stay on for 2X seconds. The waiting state, then sets the state to the read
 * state. In the waiting state, the heating elements are turned off for X seconds to allow for a
 * thermistor reading.
 */
static gboolean
control_toaster(gpointer user_data)
{
	TlltToasterOperationArgs *args = user_data;
	TlltToasterPrivate *priv	   = tllt_toaster_get_instance_private(args->toaster);

	// Start in wait state because we are starting every operation with heating elements on
	static TlltThermistorState state = STATE_WAIT;

	if (g_cancellable_is_cancelled(priv->cancellable)) {
		// Reset the state before exit
		state = STATE_READ;

		tllt_powerable_off(TLLT_POWERABLE(args->toaster->top_heating_element));
		tllt_powerable_off(TLLT_POWERABLE(args->toaster->bottom_heating_element));

		return G_SOURCE_REMOVE;
	}

	switch (state) {
	case STATE_READ: {
		const double temp = tllt_sensor_read(TLLT_SENSOR(args->toaster->thermistor));
		if (temp < args->temperature) {
			tllt_powerable_on(TLLT_POWERABLE(args->toaster->top_heating_element));
			tllt_powerable_on(TLLT_POWERABLE(args->toaster->bottom_heating_element));

			state = STATE_NO_READ;
		}

		if (temp > args->temperature) {
			tllt_powerable_off(TLLT_POWERABLE(args->toaster->top_heating_element));
			tllt_powerable_off(TLLT_POWERABLE(args->toaster->bottom_heating_element));

			state = STATE_READ;
		}

		break;
	}
	case STATE_NO_READ:
		state = STATE_WAIT;
		break;
	case STATE_WAIT:
		state = STATE_READ;
		tllt_powerable_off(TLLT_POWERABLE(args->toaster->top_heating_element));
		tllt_powerable_off(TLLT_POWERABLE(args->toaster->bottom_heating_element));
		break;
	default:
		g_warn_if_reached();
	}

	return G_SOURCE_CONTINUE;
}

static void
toaster_operation_destroy(gpointer user_data)
{
	TlltToasterOperationArgs *args = user_data;
	TlltToasterPrivate *priv	   = tllt_toaster_get_instance_private(args->toaster);

	g_object_unref(priv->cancellable);
	g_object_unref(args->toaster);

	tllt_toaster_operations_args_unref(args);
}

static gboolean
prepare_toaster(gpointer user_data)
{
	TlltToasterOperationArgs *args = user_data;
	TlltToasterPrivate *priv	   = tllt_toaster_get_instance_private(args->toaster);

	const double temp = tllt_sensor_read(TLLT_SENSOR(args->toaster->thermistor));
	if (!(temp < args->temperature + 10 && temp > args->temperature - 10) &&
		!g_cancellable_is_cancelled(priv->cancellable)) {
		return G_SOURCE_CONTINUE;
	}

	if (g_cancellable_is_cancelled(priv->cancellable)) {
		g_signal_emit(args->toaster, obj_signals[SIGNAL_STOPPED], 0);
		priv->running = FALSE;
		toaster_operation_destroy(args);
	} else {
		args->ref_count++;
		g_object_ref(args->toaster);
		g_object_ref(priv->cancellable);
		g_timer_reset(priv->timer);

		g_signal_emit(args->toaster, obj_signals[SIGNAL_STARTED], 0);
		g_timer_start(priv->timer);
		g_timeout_add_full(G_PRIORITY_DEFAULT, 40, update_toaster, args, update_toaster_destroy);
		g_timeout_add_seconds_full(G_PRIORITY_DEFAULT, 15, control_toaster, args,
								   toaster_operation_destroy);
	}

	return G_SOURCE_REMOVE;
}

void
tllt_toaster_start(TlltToaster *self, const unsigned int time, const int temperature,
				   const TlltToasterUpdateFunc update, gpointer user_data)
{
	TlltToasterPrivate *priv = tllt_toaster_get_instance_private(self);

	priv->cancellable = g_cancellable_new();

	TlltToasterOperationArgs *toaster_op_args = g_malloc(sizeof(TlltToasterOperationArgs));
	g_return_if_fail(toaster_op_args != NULL);
	toaster_op_args->total_time_seconds = time;
	toaster_op_args->update				= update;
	toaster_op_args->user_data			= user_data;
	toaster_op_args->toaster			= self;
	toaster_op_args->temperature		= temperature;
	toaster_op_args->ref_count			= 1;
	g_object_ref(self);

	const double temp = tllt_sensor_read(TLLT_SENSOR(self->thermistor));
	if (temp < temperature) {
		tllt_powerable_on(TLLT_POWERABLE(self->top_heating_element));
		tllt_powerable_on(TLLT_POWERABLE(self->bottom_heating_element));
	} else {
		tllt_powerable_off(TLLT_POWERABLE(self->top_heating_element));
		tllt_powerable_off(TLLT_POWERABLE(self->bottom_heating_element));
	}

	g_timeout_add(500, prepare_toaster, toaster_op_args);
	priv->running = TRUE;
	g_signal_emit(self, obj_signals[SIGNAL_PREPARING], 0);
}

gboolean
tllt_toaster_is_running(TlltToaster *self)
{
	TlltToasterPrivate *priv = tllt_toaster_get_instance_private(self);

	return priv->running;
}