Commit 8a8f61ba1d6f7461191fa8b6008054275508dc36

Authored by Enrique Díaz Roque
1 parent 497fec6f
Exists in master and in 1 other branch dev

Updated ino to work with timer1 (time control)

Showing 1 changed file with 198 additions and 112 deletions   Show diff stats
ino/src/sketch.ino
1 1 #include <heisenwave.h>
2 2 #include <LCD.h>
3   -#include <Time.h>
4 3  
5   -#define SIZE 256
  4 +#define LEAP_YEAR(Y) (((1970+Y)>0) && !((1970+Y)%4) && (((1970+Y)%100) || !((1970+Y)%400)))
  5 +#define ONE_SECOND_TCKS 15625
  6 +#define SIZE 256
  7 +
  8 +// General stuff
  9 +
  10 +volatile uint32_t timestamp = 0;
  11 +uint8_t last_second = 0;
6 12  
7 13 // LCD stuff
8 14  
  15 +LCD lcd;
9 16 char ino_alias[16];
10   -uint8_t last_second = 0;
11 17 uint8_t lcd_swap = 0;
12   -LCD lcd;
13 18  
14 19 // ADC stuff
15 20  
... ... @@ -22,171 +27,252 @@ volatile uint8_t pc_index = 0;
22 27 volatile bool transfer_active = false;
23 28 volatile bool transfer_ready = false;
24 29  
  30 +struct time_formated {
  31 + uint8_t seconds, minutes, hours, day, month, year;
  32 +};
  33 +
25 34 // ISR routines
26 35  
27 36 ISR(ADC_vect) {
28   - buffer[index++] = ADCH;
  37 + buffer[index++] = ADCH;
29 38  
30   - if (index == 0) {
31   - buffer = (buffer == frame_1)? frame_2 : frame_1;
32   - pc_buffer = (buffer == frame_1)? frame_2 : frame_1;
  39 + if (index == 0) {
  40 + buffer = (buffer == frame_1)? frame_2 : frame_1;
  41 + pc_buffer = (buffer == frame_1)? frame_2 : frame_1;
33 42  
34   - // will this double buffer system be enough?...
35   - // if transfer = true => not enough!...
36   - // we could disable interrupts while send_data...?
  43 + // will this double buffer system be enough?...
  44 + // if transfer = true => not enough!...
  45 + // we could disable interrupts while send_data...?
37 46  
38   - transfer_ready = true;
39   - }
  47 + transfer_ready = true;
  48 + }
  49 +}
  50 +
  51 +ISR(TIMER1_COMPA_vect) {
  52 + TCNT1 = 0; // we won't lose ticks
  53 + timestamp++; // inc. 1 sec
40 54 }
41 55  
42 56 // Procedures && functions
43 57  
44   -void send_data() {
45   - if (transfer_active && transfer_ready) {
46   - Serial.write(HW_NEW_FRAME);
  58 +void getTimeFormated(struct time_formated &tf) {
  59 + // Modified version from <Time.h>
  60 +
  61 + uint8_t year = 0;
  62 + uint8_t month = 0;
  63 + uint8_t monthLength = 0;
  64 + uint32_t time;
  65 + unsigned long days = 0;
  66 + uint8_t monthDays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
  67 +
  68 + time = (uint32_t) timestamp;
  69 + tf.seconds = time % 60;
  70 + time /= 60; // minutes
  71 + tf.minutes = time % 60;
  72 + time /= 60; // hours
  73 + tf.hours = time % 24;
  74 + time /= 24; // days
  75 +
  76 + while ((unsigned)(days += (LEAP_YEAR(year) ? 366 : 365)) <= time) {
  77 + year++;
  78 + }
  79 +
  80 + tf.year = year;
  81 +
  82 + days -= LEAP_YEAR(year) ? 366 : 365;
  83 + time -= days; // days in current year
  84 + days = 0;
  85 +
  86 + for (month = 0; month < 12; month++) {
  87 + if (month == 1) { // february
  88 + if (LEAP_YEAR(year)) {
  89 + monthLength = 29;
  90 + } else {
  91 + monthLength = 28;
  92 + }
  93 + } else {
  94 + monthLength = monthDays[month];
  95 + }
47 96  
48   - for (int i = 0; i < SIZE; i++) {
49   - Serial.write(pc_buffer[i]);
  97 + if (time >= monthLength) {
  98 + time -= monthLength;
  99 + } else {
  100 + break;
  101 + }
50 102 }
51 103  
52   - transfer_ready = false;
53   - }
  104 + tf.month = month + 1;
  105 + tf.day = time + 1;
  106 +}
  107 +
  108 +void send_data() {
  109 + if (transfer_active && transfer_ready) {
  110 + Serial.write(HW_NEW_FRAME);
  111 +
  112 + for (int i = 0; i < SIZE; i++) {
  113 + Serial.write(pc_buffer[i]);
  114 + }
  115 +
  116 + transfer_ready = false;
  117 + }
54 118 }
55 119  
56 120 void print_time_lcd() {
57   - bool alias_turn;
58   - char buffer[4];
59   - lcd.clear_screen();
60   -
61   - if (++lcd_swap >= 10) {
62   - lcd_swap = 0;
63   - }
64   -
65   - if (lcd_swap < 5) {
66   - lcd.print(ino_alias);
67   - } else {
68   - lcd.set_cursor(3,1);
69   - lcd.print(dayShortStr(day()));
70   - lcd.print("/");
71   - lcd.print(monthShortStr(month()));
72   - lcd.print("/");
73   - sprintf(buffer, "%d", year());
  121 + struct time_formated tf;
  122 + getTimeFormated(tf);
  123 +
  124 + bool alias_turn;
  125 + char buffer[4];
  126 + lcd.clear_screen();
  127 +
  128 + if (++lcd_swap >= 10) {
  129 + lcd_swap = 0;
  130 + }
  131 +
  132 + if (lcd_swap < 5) {
  133 + lcd.print(ino_alias);
  134 + } else {
  135 + lcd.set_cursor(3,1);
  136 + sprintf(buffer, "%d", tf.day);
  137 + lcd.print(buffer);
  138 + lcd.print("/");
  139 + sprintf(buffer, "%d", tf.month);
  140 + lcd.print(buffer);
  141 + lcd.print("/");
  142 + sprintf(buffer, "%d", tf.year+1970);
  143 + lcd.print(buffer);
  144 + }
  145 +
  146 + lcd.set_cursor(5,2);
  147 +
  148 + sprintf(buffer, "%d", tf.hours);
  149 + if (strlen(buffer) < 2) lcd.print("0");
  150 + lcd.print(buffer);lcd.print(":");
  151 + sprintf(buffer, "%d", tf.minutes);
  152 + if (strlen(buffer) < 2) lcd.print("0");
  153 + lcd.print(buffer);lcd.print(":");
  154 + sprintf(buffer, "%d", tf.seconds);
  155 + if (strlen(buffer) < 2) lcd.print("0");
74 156 lcd.print(buffer);
75   - }
76   -
77   - lcd.set_cursor(5,2);
78   -
79   - sprintf(buffer, "%d", hour());
80   - if (strlen(buffer) < 2) lcd.print("0");
81   - lcd.print(buffer);lcd.print(":");
82   - sprintf(buffer, "%d", minute());
83   - if (strlen(buffer) < 2) lcd.print("0");
84   - lcd.print(buffer);lcd.print(":");
85   - sprintf(buffer, "%d", second());
86   - if (strlen(buffer) < 2) lcd.print("0");
87   - lcd.print(buffer);
88 157 }
89 158  
90 159 void hardware_setup() {
91   - // Max Prescaler
92   - ADCSRA |= (0<<ADPS2) | (0<<ADPS1) | (1<<ADPS0) | (1<<ADATE) | (1<<ADEN) | (1<<ADIE);
93   - ADMUX |= (1<<REFS0) | (1<<ADLAR);
94   - ADMUX &= ~(1<<REFS1);
95   - ADCSRB &= ~((1<<ADTS2) | (1<<ADTS1) | (1<<ADTS0));
96   - ADCSRA |= (1<<ADSC);
  160 + // ADC - Max prescaler
  161 +
  162 + ADCSRA |= (0<<ADPS2) | (0<<ADPS1) | (1<<ADPS0) | (1<<ADATE) | (1<<ADEN) | (1<<ADIE);
  163 + ADMUX |= (1<<REFS0) | (1<<ADLAR);
  164 + ADMUX &= ~(1<<REFS1);
  165 + ADCSRB &= ~((1<<ADTS2) | (1<<ADTS1) | (1<<ADTS0));
  166 + ADCSRA |= (1<<ADSC);
  167 +
  168 + // Timer 1
  169 +
  170 + TCCR1A = 0;
  171 + TCCR1B = 0; // stop
  172 + TCNT1 = 0;
  173 + TIFR1 = 0;
  174 + OCR1A = ONE_SECOND_TCKS;
  175 + TIMSK1 = (1 << OCIE1A) | (1 << TOIE1);
  176 + TCCR1B = (1 << CS12) | (1 << CS10);
97 177 }
98 178  
99 179 // Main
100 180  
101 181 void setup() {
102   - hardware_setup();
103   - Serial.begin(115200);
  182 + ino_alias[0] = 'A';
  183 + hardware_setup();
  184 + Serial.begin(115200);
  185 +
  186 + timestamp = 1431447151; // testing purposes
104 187  
105   - lcd.clear_screen();
106   - lcd.backlight(true);
  188 + lcd.clear_screen();
  189 + lcd.backlight(true);
107 190  
108   - //Serial.write(HW_ACK);
109   - Serial.write("HW");
110   - Serial.write(HW_INO_VERSION_MAJOR);
111   - Serial.write(HW_INO_VERSION_MINOR);
  191 + //Serial.write(HW_ACK);
  192 + Serial.write("HW");
  193 + Serial.write(HW_INO_VERSION_MAJOR);
  194 + Serial.write(HW_INO_VERSION_MINOR);
112 195 }
113 196  
114 197 void loop() {
115   - send_data();
  198 + send_data();
116 199  
117   - uint8_t current;
118   - if (current = second(), last_second != current) {
119   - print_time_lcd();
120   - last_second = current;
121   - }
  200 + uint8_t current;
  201 + struct time_formated tf;
  202 + getTimeFormated(tf);
122 203  
123   - if (Serial.available() < 1) {
124   - switch (Serial.read()) {
125   - case HW_HELO:
  204 + if (current = tf.seconds, last_second != current) {
  205 + print_time_lcd();
  206 + last_second = current;
  207 + }
  208 +
  209 + if (Serial.available() < 1) {
  210 + switch (Serial.read()) {
  211 + case HW_HELO:
126 212 {
127   - while (Serial.available() < HW_HELO_SIZE);
  213 + while (Serial.available() < HW_HELO_SIZE);
128 214  
129   - for (byte i = 0; i < HW_HELO_SIZE; i++) {
130   - ino_alias[i] = Serial.read();
131   - }
  215 + for (byte i = 0; i < HW_HELO_SIZE; i++) {
  216 + ino_alias[i] = Serial.read();
  217 + }
132 218  
133   - Serial.write(HW_ACK);
  219 + Serial.write(HW_ACK);
134 220 }
135   - case HW_SMPL_RATE:
  221 + case HW_SMPL_RATE:
136 222 {
137   - // change register
  223 + // change register
138 224  
139   - break;
  225 + break;
140 226 }
141   - case HW_DTIME_GET:
  227 + case HW_DTIME_GET:
142 228 {
143   - Serial.write(HW_ACK);
144   - Serial.write((byte *) now(), sizeof(uint32_t));
  229 + Serial.write(HW_ACK);
  230 + //Serial.write(&timestamp, sizeof(uint32_t));
145 231  
146   - break;
  232 + break;
147 233 }
148   - case HW_DTIME_SET:
  234 + case HW_DTIME_SET:
149 235 {
150   - while (Serial.available() < HW_DATE_SIZE);
  236 + while (Serial.available() < HW_DATE_SIZE);
151 237  
152   - uint32_t unix_time = 0;
153   - for (uint8_t i = HW_DATE_SIZE-1; i >= 0; i--) {
154   - unix_time += Serial.read() << (8*i);
155   - }
156   - setTime(unix_time);
  238 + uint32_t unix_time = 0;
  239 + for (uint8_t i = HW_DATE_SIZE-1; i >= 0; i--) {
  240 + unix_time += Serial.read() << (8*i);
  241 + }
  242 + timestamp = unix_time;
157 243  
158   - Serial.write(HW_ACK);
  244 + Serial.write(HW_ACK);
159 245  
160   - break;
  246 + break;
161 247 }
162   - case HW_SLEEP:
  248 + case HW_SLEEP:
163 249 {
164   - SMCR = 0; // (mode) low power reduction
165   - SMCR |= (1 << SE);
166   - asm volatile("sleep");
  250 + SMCR = 0; // (mode) low power reduction
  251 + SMCR |= (1 << SE);
  252 + asm volatile("sleep");
167 253  
168   - // ZzZzZ
  254 + // ZzZzZ
169 255  
170   - SMCR = 0;
  256 + SMCR = 0;
171 257  
172   - break;
  258 + break;
173 259 }
174   - case HW_GET_DATA:
  260 + case HW_GET_DATA:
175 261 {
176   - transfer_active = true;
  262 + transfer_active = true;
177 263  
178   - break;
  264 + break;
179 265 }
180   - case HW_STOP_DATA:
  266 + case HW_STOP_DATA:
181 267 {
182   - // Stop sending chunks?
183   - transfer_active = false;
  268 + // Stop sending chunks?
  269 + transfer_active = false;
184 270  
185   - break;
  271 + break;
  272 + }
  273 + default:
  274 + // This should never happen, command ignored
  275 + break;
186 276 }
187   - default:
188   - // This should never happen, command ignored
189   - break;
190 277 }
191   - }
192 278 }
... ...