Improved error handling during code generation
[cerebrum.git] / avr / 7seg.c.tp
blob8a8d2423488ecf7d382a934762690dd207169f3b
1 /*
2  Copyright (C) 2012 jaseg <s@jaseg.de>
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 3 as published by the Free Software Foundation.
7  */
9 #include <avr/io.h>
10 #include <avr/pgmspace.h>
12 #ifndef _7SEG_STATIC_
13 #define _7SEG_STATIC_
15 inline void l7seg_pulse_clk(void){
16     L7SEG_CLK_PORT |= _BV(L7SEG_CLK_PIN);
17     L7SEG_CLK_PORT &= ~_BV(L7SEG_CLK_PIN);
20 inline void l7seg_data_out(uint8_t val){
21     L7SEG_DATA_PORT &= ~_BV(L7SEG_DATA_PIN);
22     if(!val){
23         L7SEG_DATA_PORT |= _BV(L7SEG_DATA_PIN);
24     }
27 inline void l7seg_select_digit(uint8_t digit){
28     L7SEG_DIGIT1_PORT &= ~_BV(L7SEG_DIGIT1_PIN);
29     L7SEG_DIGIT2_PORT &= ~_BV(L7SEG_DIGIT2_PIN);
30     if(digit&1){ 
31         L7SEG_DIGIT1_PORT |= _BV(L7SEG_DIGIT1_PIN);
32     }
33     if(digit&2){
34         L7SEG_DIGIT2_PORT |= _BV(L7SEG_DIGIT2_PIN);
35     }
38 int l7seg_digit1[] = {
39     0x56C0, //0
40     0x0600, //1
41     0x94C0, //2
42     0x9640, //3
43     0xC600, //4
44     0xD240, //5
45     0xD2C0, //6
46     0x1600, //7
47     0xD6C0, //8
48     0xD640, //9
49     0xD680, //A
50     0xC2C0, //B
51     0x50C0, //C
52     0x86C0, //D
53     0xD0C0, //E
54     0xD080  //F
57 int l7seg_digit2[] = {
58     0x011F,
59     0x000C,
60     0x081B,
61     0x081E,
62     0x090C,
63     0x0916,
64     0x0917,
65     0x001C,
66     0x091F,
67     0x091E,
68     0x091D,
69     0x0907,
70     0x0113,
71     0x080F,
72     0x0913,
73     0x0911
76 int l7seg_get_digit(uint8_t b, uint8_t a){
77         int out = 0;
78         a -= '0';
79         b -= '0';
80         if(a > 0x20)
81                 a-=0x20;
82         if(a > 0x10)
83                 a-=7;
84         if(b > 0x20)
85                 b-=0x20;
86         if(b > 0x10)
87                 b-=7;
88         out = l7seg_digit1[a];
89         return out | l7seg_digit2[b];
92 typedef char l7seg_buftype[4];
94 #endif//_7SEG_STATIC_
96 void ${init_function} (){
97         L7SEG_CLK_DDR |= _BV(L7SEG_CLK_PIN);
98         L7SEG_DATA_DDR |= _BV(L7SEG_DATA_PIN);
99         L7SEG_DIGIT1_DDR |= _BV(L7SEG_DIGIT1_PIN);
100         L7SEG_DIGIT2_DDR |= _BV(L7SEG_DIGIT2_PIN);
103 void ${loop_function} (){
104         static int l7seg_cycle=1;
105     static uint8_t pos = 0;
106         uint8_t nbuf = 0;
107     if(pos&1){
108         l7seg_cycle = l7seg_get_digit(${modulevar("buf")}[2], ${modulevar("buf")}[3]);
109         l7seg_select_digit(0);
110         for(uint8_t i = 0; i<16; i++){
111             l7seg_data_out(l7seg_cycle&1);
112             l7seg_cycle>>=1;
113             l7seg_pulse_clk();
114         }
115         l7seg_data_out(0);
116         l7seg_select_digit(1);
117     }else{
118         l7seg_cycle = l7seg_get_digit(${modulevar("buf")}[0], ${modulevar("buf")}[1]);
119         l7seg_select_digit(0);
120         for(uint8_t i = 0; i<16; i++){
121             l7seg_data_out(l7seg_cycle&1);
122             l7seg_cycle>>=1;
123             l7seg_pulse_clk();
124         }
125         l7seg_data_out(0);
126         l7seg_select_digit(2);
127     }
128     pos++;
131 ${modulevar("buf", "l7seg_buftype", "4c")} = {'1','3','3','7'};