reprap: Progress
[simavr] / examples / board_reprap / src / heatpot.c
1 /*
2         heatpot.c
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include "sim_avr.h"
25 #include "sim_time.h"
26
27 #include "heatpot.h"
28
29 static avr_cycle_count_t
30 heatpot_evaluate_timer(
31                 struct avr_t * avr,
32         avr_cycle_count_t when,
33         void * param)
34 {
35         heatpot_p  p = (heatpot_p) param;
36         return when + p->cycle;
37 }
38
39 static void
40 heatpot_tally_in_hook(
41                 struct avr_irq_t * irq,
42                 uint32_t value,
43                 void * param)
44 {
45         heatpot_p p = (heatpot_p)param;
46         heatpot_data_t v = {.v = value };
47
48         heatpot_tally(p, v.sid, v.cost);
49 }
50
51 static const char * irq_names[IRQ_HEATPOT_COUNT] = {
52         [IRQ_HEATPOT_TALLY] = "8<heatpot.tally",
53 };
54
55 void
56 heatpot_init(
57                 struct avr_t * avr,
58                 heatpot_p p,
59                 const char * name,
60                 float ambiant )
61 {
62         p->avr = avr;
63         strcpy(p->name, (char*)name);
64         p->irq = avr_alloc_irq(&avr->irq_pool, 0, IRQ_HEATPOT_COUNT, irq_names);
65         avr_irq_register_notify(p->irq + IRQ_HEATPOT_TALLY, heatpot_tally_in_hook, p);
66
67         p->cycle = avr_usec_to_cycles(avr, 100000 / 1000);
68         avr_cycle_timer_register_usec(avr, p->cycle, heatpot_evaluate_timer, p);
69
70 }
71
72 void
73 heatpot_tally(
74                 heatpot_p p,
75                 int sid,
76                 float cost )
77 {
78         int f = -1, ei = -1;
79         for (int si = 0; si < 32 && f == -1; si++)
80                 if (p->tally[si].sid == 0)
81                         ei = si;
82                 else if (p->tally[si].sid == sid)
83                         f = si;
84         if (f == -1) {
85                 if (ei == -1) {
86                         printf("%s(%s) no room for extra tally source id %d\n", __func__, p->name, sid);
87                         return;
88                 }
89                 f = ei;
90         }
91         p->tally[f].sid = sid;
92         p->tally[f].cost = cost;
93 }