parts: Updated to name their IRQs
[simavr] / examples / parts / ac_input.c
1 /*
2         ac_input.c
3
4         Copyright Luki <humbell@ethz.ch>
5         Copyright 2011 Michel Pollet <buserror@gmail.com>
6
7         This file is part of simavr.
8
9         simavr is free software: you can redistribute it and/or modify
10         it under the terms of the GNU General Public License as published by
11         the Free Software Foundation, either version 3 of the License, or
12         (at your option) any later version.
13
14         simavr is distributed in the hope that it will be useful,
15         but WITHOUT ANY WARRANTY; without even the implied warranty of
16         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17         GNU General Public License for more details.
18
19         You should have received a copy of the GNU General Public License
20         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include "sim_avr.h"
24 #include "ac_input.h"
25 #include "stdio.h"
26
27 static avr_cycle_count_t
28 switch_auto(struct avr_t * avr,
29         avr_cycle_count_t when, void * param)
30 {
31         ac_input_t * b = (ac_input_t *) param;
32         b->value = !b->value;
33         avr_raise_irq(b->irq + IRQ_AC_OUT, b->value);
34         return when + avr_usec_to_cycles(avr, 100000 / 50);
35 }
36
37 void ac_input_init(avr_t *avr, ac_input_t *b)
38 {
39         const char * name = ">ac_input";
40         b->irq = avr_alloc_irq(&avr->irq_pool, 0, IRQ_AC_COUNT, &name);
41         b->avr = avr;
42         b->value = 0;
43         avr_cycle_timer_register_usec(avr, 100000 / 50, switch_auto, b);
44         printf("ac_input_init period %duS or %d cycles\n",
45                         100000 / 50, avr_usec_to_cycles(avr, 100000 / 50));
46 }