Subversion Repositories HomeAutomation

Rev

Rev 645 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 645 Rev 647
1
/**
1
/**
2
 * CanCarDisplay. HID for simple carsensor network
2
 * CanCarDisplay. HID for simple carsensor network
3
 * For HD44780- and KS0073-based LCD displays
3
 * For HD44780- and KS0073-based LCD displays
4
 *
4
 *
5
 * Contains no intelligence. It just prints what it is told to.
5
 * Contains no intelligence. It just prints what it is told to.
6
 *
6
 *
7
 * @date    2007-10-01
7
 * @date    2007-10-01
8
 * @author  Martin Norden
8
 * @author  Martin Norden
9
 *
9
 *
-
 
10
 * TODO: put rotary encoder in external library, same with s16/u16tostr, cleanup, read external sensors... a lot basically.
-
 
11
 * Assume all sensor conversion done on the sensorcard, use canstandard here
10
 */
12
 */
11
 
13
 
12
/*-----------------------------------------------------------------------------
14
/*-----------------------------------------------------------------------------
13
 * Includes
15
 * Includes
14
 *---------------------------------------------------------------------------*/
16
 *---------------------------------------------------------------------------*/
15
/* system files */
17
/* system files */
16
#include <avr/io.h>
18
#include <avr/io.h>
17
#include <inttypes.h>
19
#include <inttypes.h>
18
#include <avr/interrupt.h>
20
#include <avr/interrupt.h>
19
#include <stdio.h>
21
#include <stdio.h>
20
/* lib files */
22
/* lib files */
21
#include <bios.h>
23
#include <bios.h>
22
#include <config.h>
24
#include <config.h>
23
#include <drivers/lcd/clcd/lcd_HD44780.h>
25
#include <drivers/lcd/clcd/lcd_HD44780.h>
24
#include <stdlib.h> //for itoa
26
#include <stdlib.h> //for itoa
25
 
27
 
26
#include <string.h> //for memcpy
28
#include <string.h> //for memcpy
27
 
29
 
28
 
30
 
29
/* <SIZE> 8 bits 0xFFL */
31
/* <SIZE> 8 bits 0xFFL */
30
#define LCD_SIZE_ALL    0x00L
32
#define LCD_SIZE_ALL    0x00L
31
#define LCD_SIZE_16x2   0x02L
33
#define LCD_SIZE_16x2   0x02L
32
#define LCD_SIZE_20x4   0x10L
34
#define LCD_SIZE_20x4   0x10L
33
 
35
 
34
 
36
 
35
//Borde ha en bŠttre plats.
37
//Borde ha en bŠttre plats.
36
#define max(a,b) ((a>b)?a:b)
38
#define max(a,b) ((a>b)?a:b)
37
#define min(a,b) ((a<b)?a:b)
39
#define min(a,b) ((a<b)?a:b)
38
 
40
 
39
 
41
 
40
/*-----------------------------------------------------------------------------
42
/*-----------------------------------------------------------------------------
41
 * Defines
43
 * Defines
42
 *---------------------------------------------------------------------------*/
44
 *---------------------------------------------------------------------------*/
43
#define SIZE_X  20
45
#define SIZE_X  20
44
#define SIZE_Y  4
46
#define SIZE_Y  4
45
#define SIZE_LCD LCD_SIZE_20x4
47
#define SIZE_LCD LCD_SIZE_20x4
46
 
48
 
47
#define BUFFER_SIZE SIZE_X
49
#define BUFFER_SIZE SIZE_X
48
#define TRUE 1
50
#define TRUE 1
49
#define FALSE 0
51
#define FALSE 0
50
 
52
 
51
#define LEFT    0x01
53
#define LEFT    0x01
52
#define RIGHT   0x02
54
#define RIGHT   0x02
53
#define BUTTON_PUSH 0x03
55
#define BUTTON_PUSH 0x03
54
#define BUTTON_RELEASE 0x04
56
#define BUTTON_RELEASE 0x04
55
 
57
 
56
volatile Can_Message_t rxMsg;
58
volatile Can_Message_t rxMsg;
57
 
59
 
58
char incoming_text[ BUFFER_SIZE ];
60
char incoming_text[ BUFFER_SIZE ];
59
uint8_t rxMsgFull;
61
uint8_t rxMsgFull;
60
 
62
 
61
 
63
 
62
char buffer[ BUFFER_SIZE ];
64
char buffer[ BUFFER_SIZE ];
63
 
65
 
64
void send_dimensions(void);
66
void send_dimensions(void);
65
void rotenc(void);
67
void rotenc(void);
66
void rotenc_action(uint8_t type);
68
void rotenc_action(uint8_t type);
67
 
69
 
68
 
70
 
69
void updateDisplay(uint8_t screen, uint8_t transition);
71
void updateDisplay(uint8_t screen, uint8_t transition);
70
 
72
 
71
#define SCR_LAMBDA      0x00
73
#define SCR_LAMBDA      0x00
72
#define SCR_BOOST       0x01
74
#define SCR_BOOST       0x01
73
#define SCR_MAXBOOST    0x02
75
#define SCR_MAXBOOST    0x02
74
#define SCR_RPM         0x03
76
#define SCR_RPM         0x03
75
#define SCR_VOLTAGE     0x04
77
#define SCR_VOLTAGE     0x04
76
#define LAST_SCREEN     0x04
78
#define LAST_SCREEN     0x04
77
void standardScreen(char* text, uint16_t value, uint16_t maxvalue);
79
void standardScreen(char* text, uint16_t value, uint16_t maxvalue);
78
 
80
 
-
 
81
void numtoascii( int16_t num, char **str );
-
 
82
void rattoascii( int16_t num, uint8_t decimalplace, char *string); //decimalplace is number ofjah
-
 
83
 
-
 
84
uint8_t *parsetemperature(int16_t value, uint8_t *str,uint8_t decimal,uint8_t options);
79
uint8_t *io_s16toStr(int16_t value, uint8_t *str,uint8_t decimal,uint8_t options);
85
uint8_t *io_s16toStr(int16_t value, uint8_t *str,uint8_t decimal,uint8_t options);
80
uint8_t *io_u16toStr(uint16_t value, uint8_t *str,uint8_t decimal,uint8_t options);
86
uint8_t *io_u16toStr(uint16_t value, uint8_t *str,uint8_t decimal,uint8_t options);
81
 
87
 
82
 
88
 
83
 
89
 
84
/*******************************************************************************
90
/*******************************************************************************
85
 * Sensor Values, Scale factors etc.                                           *
91
 * Sensor Values, Scale factors etc.                                           *
86
 *******************************************************************************/
92
 *******************************************************************************/
87
int16_t sensor_Boost = 120; //Should be set automagically by incoming CAN
93
int16_t sensor_Boost = 120; //Should be set automagically by incoming CAN
88
int16_t sensor_Boost_Max = 200;
94
int16_t sensor_Boost_Max = 200;  //highest boost so far
89
 
95
 
90
uint16_t sensor_RPM = 2573;
96
uint16_t sensor_RPM = 2573;
91
uint8_t sensor_Lambda = 127;
97
uint8_t sensor_Lambda = 127;
92
uint8_t sensor_Voltage = 143;
98
uint8_t sensor_Voltage = 143;
93
 
99
 
94
#define scale_Boost 809 //Read from eeprom later on?
100
#define scale_Boost 200 //see "Representation av storheter"
95
#define scale_BoostOffset 168 //Read from eeprom later on?
-
 
96
#define scale_BoostMAX 200
101
#define scale_BoostMAX 200
97
#define scale_RPMMAX 8000 //Can be set as in eeprom later perhaps?
102
#define scale_RPMMAX 8000 //Can be set as in eeprom later perhaps?
98
#define scale_Voltage 1024
103
#define scale_Voltage 1024
99
#define scale_VoltageMAX 150
104
#define scale_VoltageMAX 150
100
 
105
 
101
 
106
 
102
uint8_t currentScreen;
107
uint8_t currentScreen;
103
 
108
 
104
// CAN message reception callback
109
// CAN message reception callback
105
// This function runs with interrupts disabled, keep it as short as possible
110
// This function runs with interrupts disabled, keep it as short as possible
106
void can_receive(Can_Message_t *msg){
111
void can_receive(Can_Message_t *msg){
107
    if(!rxMsgFull){
112
    if(!rxMsgFull){
108
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
113
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
109
        rxMsgFull = 1;
114
        rxMsgFull = 1;
110
    }
115
    }
111
}
116
}
112
 
117
 
113
ISR( PCINT2_vect ){
118
ISR( PCINT2_vect ){
114
// For ROTENC_A and ROTENC_B FIXME Šr inte riktigt rŠtt. Verkar vara bra tycker jag?
119
// For ROTENC_A and ROTENC_B FIXME Šr inte riktigt rŠtt. Verkar vara bra tycker jag?
115
    rotenc();
120
    rotenc();
116
}
121
}
117
 
122
 
118
ISR( PCINT0_vect ){
123
ISR( PCINT0_vect ){
119
// For ROTENC_BTN
124
// For ROTENC_BTN
120
    rotenc();
125
    rotenc();
121
}
126
}
122
 
127
 
123
/*-----------------------------------------------------------------------------
128
/*-----------------------------------------------------------------------------
124
 * Main Program
129
 * Main Program
125
 *---------------------------------------------------------------------------*/
130
 *---------------------------------------------------------------------------*/
126
int main(void) {
131
int main(void) {
127
    currentScreen = 0; //init display position
132
    currentScreen = 0; //init display position
128
   
133
   
129
//  uint32_t act; not used anymoer
134
//  uint32_t act; not used anymoer
130
//  uint8_t act_type, lcd_size; not used anymore
135
//  uint8_t act_type, lcd_size; not used anymore
131
    //uint8_t m, lcd_action;
136
    //uint8_t m, lcd_action;
132
 
137
 
133
    sei();
138
    sei();
134
 
139
 
135
    /*
140
    /*
136
     * Initialize rotaryencoders and buttons
141
     * Initialize rotaryencoders and buttons
137
     */
142
     */
138
    // Set as input
143
    // Set as input
139
    DDRD &= ~(1<<DDD2);
144
    DDRD &= ~(1<<DDD2);
140
    DDRB &= ~((1<<DDB7)|(1<<DDB0));
145
    DDRB &= ~((1<<DDB7)|(1<<DDB0));
141
    // Enable pull-up
146
    // Enable pull-up
142
    PORTD |= (1<<PD2);
147
    PORTD |= (1<<PD2);
143
    PORTB |= (1<<PB7)|(1<<PB0);
148
    PORTB |= (1<<PB7)|(1<<PB0);
144
    // Enable IO-pin interrupt
149
    // Enable IO-pin interrupt
145
    PCICR |= (1<<PCIE2)|(1<<PCIE0);
150
    PCICR |= (1<<PCIE2)|(1<<PCIE0);
146
    // Unmask PB7, PB0 and PD2
151
    // Unmask PB7, PB0 and PD2
147
    PCMSK2 |= (1<<PCINT18);
152
    PCMSK2 |= (1<<PCINT18);
148
    PCMSK0 |= (1<<PCINT7)|(1<<PCINT0);
153
    PCMSK0 |= (1<<PCINT7)|(1<<PCINT0);
149
 
154
 
150
    // Contrast
155
    // Contrast
151
    TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
156
    TCCR0A |= (1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
152
    TCCR0B |= (1<<CS00);
157
    TCCR0B |= (1<<CS00);
153
    OCR0B = 0x10;
158
    OCR0B = 0x10;
154
    DDRD |= (1<<DDD5);
159
    DDRD |= (1<<DDD5);
155
 
160
 
156
    // Backlight
161
    // Backlight
157
    TCCR1A |= (1<<COM1A1)|(1<<WGM11);
162
    TCCR1A |= (1<<COM1A1)|(1<<WGM11);
158
    TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
163
    TCCR1B |= (1<<WGM13)|(1<<WGM12)|(1<<CS10);
159
    ICR1 = 0xFF; // Make it 8 bits
164
    ICR1 = 0xFF; // Make it 8 bits
160
    OCR1A = 0xFF;
165
    OCR1A = 0xFF;
161
    DDRB |= (1<<DDB1);
166
    DDRB |= (1<<DDB1);
162
 
167
 
163
 
168
 
164
    Can_Message_t txMsg;
169
    Can_Message_t txMsg;
165
    txMsg.ExtendedFlag=1;
170
    txMsg.ExtendedFlag=1;
166
    txMsg.RemoteFlag=0;
171
    txMsg.RemoteFlag=0;
167
// FIXME skicka app startad
172
// FIXME skicka app startad
168
    BIOS_CanCallback = &can_receive;
173
    BIOS_CanCallback = &can_receive;
169
 
174
 
170
    lcd_init( LCD_DISP_ON );
175
    lcd_init( LCD_DISP_ON );
171
    lcd_clrscr();
176
    lcd_clrscr();
172
    lcd_puts("CarDisplay");
177
    lcd_puts("CarDisplay");
173
 
178
 
174
    send_dimensions();
179
    send_dimensions();
175
    lcd_gotoxy(0,1);
180
    lcd_gotoxy(0,1);
176
    lcd_puts("CAN Connected!");
181
    lcd_puts("CAN Connected!");
177
 
182
 
178
    /* main loop */
183
    /* main loop */
179
    while(1){
184
    while(1){
180
        /* check if any messages have been received */
185
        /* check if any messages have been received */
181
        if(rxMsgFull){
186
        if(rxMsgFull){
182
            uint16_t act_type;
187
            uint16_t sns_type;
183
            uint8_t lcdid;
188
            uint8_t snsid;
184
            if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT){// FIXME ett jŠkla herk, stŠda upp
189
            if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_SNS){// FIXME ett jŠkla herk, stŠda upp
185
               
190
           
-
 
191
                sns_type = (uint16_t)((rxMsg.Id & CAN_MASK_SNS_TYPE) >> CAN_SHIFT_SNS_TYPE);
-
 
192
                snsid = (uint8_t)((rxMsg.Id & CAN_MASK_SNS_ID) >> CAN_SHIFT_SNS_ID);
186
               
193
               
187
                act_type =(uint16_t)((rxMsg.Id & CAN_MASK_ACT_TYPE) >> CAN_SHIFT_ACT_TYPE);
-
 
188
           
194
               
-
 
195
                if (sns_type == SNS_TYPE_PRESSURE) {
-
 
196
                    sensor_Boost = rxMsg.Data.bytes[0]*256 + rxMsg.Data.bytes[1];
189
                lcdid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID);               
197
                    if (currentScreen == SCR_BOOST) updateDisplay(SCR_BOOST,0);
190
 
198
                }
191
 
199
 
192
            }
200
            }
193
            rxMsgFull = 0;
201
            rxMsgFull = 0;
194
        }
202
        }
195
    }
203
    }
196
}
204
}
197
 
205
 
198
void send_dimensions(){
206
void send_dimensions(){
199
    Can_Message_t txMsg;
207
    Can_Message_t txMsg;
200
 
208
 
201
    txMsg.ExtendedFlag=1;
209
    txMsg.ExtendedFlag=1;
202
    txMsg.RemoteFlag=0;
210
    txMsg.RemoteFlag=0;
203
    txMsg.Id=0xA000001;
211
    txMsg.Id=0xA000001;
204
    txMsg.Data.bytes[0] = SIZE_X;
212
    txMsg.Data.bytes[0] = SIZE_X;
205
    txMsg.Data.bytes[1] = SIZE_Y;
213
    txMsg.Data.bytes[1] = SIZE_Y;
206
    txMsg.DataLength = 2;
214
    txMsg.DataLength = 2;
207
 
215
 
208
    BIOS_CanSend(&txMsg);
216
    BIOS_CanSend(&txMsg);
209
}
217
}
210
 
218
 
211
void rotenc_action(uint8_t type){
219
void rotenc_action(uint8_t type){
212
    //Let's do something with something!
220
    //Let's do something with something!
213
    lcd_clrscr();
221
    lcd_clrscr();
214
    if (type == LEFT){ 
222
    if (type == LEFT){ 
215
        if (currentScreen == 0) currentScreen = LAST_SCREEN+1;
223
        if (currentScreen == 0) currentScreen = LAST_SCREEN+1;
216
        currentScreen--;
224
        currentScreen--;
217
        updateDisplay(currentScreen,1);
225
        updateDisplay(currentScreen,1);
218
    }
226
    }
219
    if (type == RIGHT){
227
    if (type == RIGHT){
220
        currentScreen++;
228
        currentScreen++;
221
        if (currentScreen > LAST_SCREEN) currentScreen = 0;
229
        if (currentScreen > LAST_SCREEN) currentScreen = 0;
222
        updateDisplay(currentScreen,1);
230
        updateDisplay(currentScreen,1);
223
    }
231
    }
224
    if (type == BUTTON_PUSH){
232
    if (type == BUTTON_PUSH){
225
 
233
 
226
    }
234
    }
227
    if (type == BUTTON_RELEASE){
235
    if (type == BUTTON_RELEASE){
228
 
236
 
229
    }
237
    }
230
}
238
}
231
 
239
 
232
void updateDisplay(uint8_t screen, uint8_t transition){
240
void updateDisplay(uint8_t screen, uint8_t transition){
233
    char buffer[10];   
241
    char buffer[20];   
234
   
242
   
235
    if (transition){
243
    if (transition){
236
        lcd_clrscr();
244
        lcd_clrscr();
-
 
245
        lcd_setanimation(0);
-
 
246
    } else {
-
 
247
        lcd_setanimation(0);
237
    }
248
    }
238
    switch (screen){
249
    switch (screen){
239
    case SCR_LAMBDA:
250
    case SCR_LAMBDA:
240
        lcd_gotoxy(0,0);
251
        lcd_gotoxy(0,0);
241
        lcd_puts("LEAN Lambda RICH");
252
        lcd_puts("LEAN Lambda RICH");
242
        lcd_gotoxy(0,1);
253
        lcd_gotoxy(0,1);
243
        lcdProgressBar(sensor_Lambda, 255, 16); //TODO: vacker bargraph med bara en plupp i mitten
254
        lcdProgressBar(sensor_Lambda, 255, 16); //TODO: vacker bargraph med bara en plupp i mitten
244
    break;
255
    break;
245
    case SCR_RPM:      
256
    case SCR_RPM:      
246
        lcd_gotoxy(0,0);
257
        lcd_gotoxy(0,0);
247
        itoa(sensor_RPM,buffer,10);
258
        itoa(sensor_RPM,buffer,10);
248
        lcd_puts("RPM: ");
259
        lcd_puts("RPM: ");
249
        lcd_puts(buffer);
260
        lcd_puts(buffer);
250
        lcd_gotoxy(0,1);
261
        lcd_gotoxy(0,1);
251
        lcdProgressBar(sensor_RPM, scale_RPMMAX, 16);
262
        lcdProgressBar(sensor_RPM, scale_RPMMAX, 16);
252
    break;
263
    break;
253
    case SCR_BOOST:
264
    case SCR_BOOST:
254
           
265
           
255
        lcd_gotoxy(0,0);
266
        lcd_gotoxy(0,0);
256
        lcd_puts("Boost:");
267
        lcd_puts("Boost:");
257
   
268
   
258
        lcd_gotoxy(7,0);
269
        lcd_gotoxy(7,0);
259
        lcd_puts(io_s16toStr(((int32_t)sensor_Boost*scale_Boost)>>10,buffer,2,1));
270
        //lcd_puts(io_s16toStr(((int32_t)sensor_Boost*scale_Boost)>>10,buffer,2,1));
-
 
271
        rattoascii(sensor_Boost,6,buffer);
-
 
272
        lcd_puts(buffer);
260
   
273
       
261
        lcd_gotoxy(13,0);
274
        //lcd_gotoxy(13,0);
262
        lcd_puts("bar");
275
        lcd_puts(" bar");
263
       
276
       
264
        lcd_gotoxy(0,1);
277
        lcd_gotoxy(0,1);
265
        lcdProgressBar(max(0,sensor_Boost), ((int32_t)scale_BoostMAX)>>10, 16);
278
        lcdProgressBar(max(0,sensor_Boost), scale_BoostMAX, 16);
266
    break;
279
    break;
267
    case SCR_MAXBOOST:
280
    case SCR_MAXBOOST: //currently just a testscreen ffs omg
268
        lcd_gotoxy(0,0);
281
        lcd_gotoxy(0,0);
269
        lcd_puts("Max Boost: ");
282
        lcd_puts("Max Boost: ");
270
        lcd_gotoxy(11,0);
283
        lcd_gotoxy(11,0);
-
 
284
        lcd_gotoxy(0,1);
271
        lcd_puts(io_s16toStr(((int32_t)sensor_Boost_Max*scale_Boost)>>10,buffer,2,1));     
285
        rattoascii(0x0001,6,buffer);
-
 
286
        lcd_puts(buffer);
272
    break;
287
    break;
273
    case SCR_VOLTAGE:
288
    case SCR_VOLTAGE:
274
 
289
 
275
        lcd_gotoxy(0,0);
290
        lcd_gotoxy(0,0);
276
        lcd_puts("Voltage:");
291
        lcd_puts("Voltage:");
277
       
292
       
278
        lcd_gotoxy(9,0);
293
        lcd_gotoxy(9,0);
279
        lcd_puts(io_u16toStr(((uint32_t)sensor_Voltage*scale_Voltage)>>10,buffer,1,0));
294
        //lcd_puts(io_u16toStr(((uint32_t)sensor_Voltage*scale_Voltage)>>10,buffer,1,0));
280
       
295
       
281
        lcd_gotoxy(16,0);
296
        lcd_gotoxy(14,0);
282
        lcd_puts("V");     
297
        lcd_puts("V");     
283
        lcd_puts("spanning!");
298
        lcd_puts("spanning!");
284
       
299
       
285
        lcd_gotoxy(0,1);
300
        lcd_gotoxy(0,1);
286
        lcdProgressBar(sensor_Voltage, scale_VoltageMAX, 16);
301
        lcdProgressBar(sensor_Voltage, scale_VoltageMAX, 16);
287
    break;
302
    break;
288
    }
303
    }
289
}
304
}
290
 
305
 
291
void standardScreen(char* text, uint16_t value, uint16_t maxvalue){
306
void standardScreen(char* text, uint16_t value, uint16_t maxvalue){
292
    lcd_gotoxy(0,0);
307
    lcd_gotoxy(0,0);
293
    char buffer[5];
308
    char buffer[5];
294
    itoa(value,buffer,10);
309
    itoa(value,buffer,10);
295
    lcd_puts(text);
310
    lcd_puts(text);
296
    lcd_puts(" ");
311
    lcd_puts(" ");
297
    lcd_puts(buffer);
312
    lcd_puts(buffer);
298
    lcd_gotoxy(0,1);
313
    lcd_gotoxy(0,1);
299
    lcdProgressBar(value, maxvalue, 16);
314
    lcdProgressBar(value, maxvalue, 16);
300
}
315
}
301
 
316
 
302
 
317
 
303
 
318
 
304
 
319
 
305
 
320
 
306
 
321
 
307
 
322
 
308
 
323
 
309
 
324
 
310
 
325
 
311
 
326
 
312
 
327
 
313
 
328
 
314
 
329
 
315
 
330
 
316
 
331
 
317
void rotenc(){
332
void rotenc(){
318
    uint8_t rot_data = 0;
333
    uint8_t rot_data = 0;
319
    static uint8_t rot_lastdir = 0, rot_laststate = 0, btn_laststate = 0;
334
    static uint8_t rot_lastdir = 0, rot_laststate = 0, btn_laststate = 0;
320
 
335
 
321
    Can_Message_t txMsg;
336
    Can_Message_t txMsg;
322
    txMsg.ExtendedFlag=1;
337
    txMsg.ExtendedFlag=1;
323
    txMsg.RemoteFlag=0;
338
    txMsg.RemoteFlag=0;
324
    //txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;//FIXME: borttaget
339
    //txMsg.Id = ( CLASS_SNS<<CLASS_MASK_BITS )|( SNS_ACT_BUTTON<<SNS_TYPE_BITS )|( 0x01<<SNS_ID_BITS )| NODE_ID;//FIXME: borttaget
325
    txMsg.DataLength=2;
340
    txMsg.DataLength=2;
326
 
341
 
327
    //Take care of button push
342
    //Take care of button push
328
    if ((PINB&(1<<PB7)) != btn_laststate){ //The buttonstate has changed! Let's send a message!
343
    if ((PINB&(1<<PB7)) != btn_laststate){ //The buttonstate has changed! Let's send a message!
329
        btn_laststate = (PINB&(1<<PB7));
344
        btn_laststate = (PINB&(1<<PB7));
330
        txMsg.Data.bytes[0] = 5;
345
        txMsg.Data.bytes[0] = 5;
331
        txMsg.Data.bytes[1] = (btn_laststate)?0:1;
346
        txMsg.Data.bytes[1] = (btn_laststate)?0:1;
332
        txMsg.DataLength=2;
347
        txMsg.DataLength=2;
333
        BIOS_CanSend(&txMsg);
348
        BIOS_CanSend(&txMsg);
334
        rotenc_action((btn_laststate)?BUTTON_RELEASE:BUTTON_PUSH);
349
        rotenc_action((btn_laststate)?BUTTON_RELEASE:BUTTON_PUSH);
335
    }
350
    }
336
   
351
   
337
    //Take care of rotary encoder movement
352
    //Take care of rotary encoder movement
338
    if(PINB&(1<<PB0)){
353
    if(PINB&(1<<PB0)){
339
        rot_data |= 0x01;
354
        rot_data |= 0x01;
340
    }
355
    }
341
    if(PIND&(1<<PD2)){
356
    if(PIND&(1<<PD2)){
342
        rot_data |= 0x02;
357
        rot_data |= 0x02;
343
    }
358
    }
344
 
359
 
345
    if( rot_data==0 || rot_data==3 ){ // Are both signals high or low?
360
    if( rot_data==0 || rot_data==3 ){ // Are both signals high or low?
346
        if( rot_data==0 && rot_laststate!=rot_data ){ // Are both signals low? In that case we are finished with one turn and should print out the direction it went. 
361
        if( rot_data==0 && rot_laststate!=rot_data ){ // Are both signals low? In that case we are finished with one turn and should print out the direction it went. 
347
            if( rot_lastdir&0x01 ){
362
            if( rot_lastdir&0x01 ){
348
                // Moving right
363
                // Moving right
349
                txMsg.Data.bytes[0]=RIGHT;
364
                txMsg.Data.bytes[0]=RIGHT;
350
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
365
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
351
                txMsg.DataLength=2;
366
                txMsg.DataLength=2;
352
                BIOS_CanSend(&txMsg);
367
                BIOS_CanSend(&txMsg);
353
                rotenc_action(RIGHT);
368
                rotenc_action(RIGHT);
354
            }else{
369
            }else{
355
                // Moving left
370
                // Moving left
356
                txMsg.Data.bytes[0]=LEFT;
371
                txMsg.Data.bytes[0]=LEFT;
357
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
372
                txMsg.Data.bytes[1] = (PINB&(1<<PB7))?0:1;
358
                txMsg.DataLength=2;
373
                txMsg.DataLength=2;
359
                BIOS_CanSend(&txMsg);
374
                BIOS_CanSend(&txMsg);
360
                rotenc_action(LEFT);
375
                rotenc_action(LEFT);
361
            }
376
            }
362
        }
377
        }
363
        rot_laststate = rot_data;
378
        rot_laststate = rot_data;
364
    } else { // No, only one of the signals are high. We can use this to find out what direction we are moving.
379
    } else { // No, only one of the signals are high. We can use this to find out what direction we are moving.
365
        rot_lastdir = rot_data;
380
        rot_lastdir = rot_data;
366
    }
381
    }
367
}
382
}
368
 
383
 
369
 
384
 
370
 
385
 
371
 
386
 
372
 
387
 
373
 
388
 
-
 
389
 
-
 
390
 
-
 
391
 
-
 
392
//‡ la pengi
-
 
393
void numtoascii( int16_t num, char **str ) {
-
 
394
    if( num==0 ) return;
-
 
395
    numtoascii( num/10, str );
-
 
396
    **str = '0' + (num%10);
-
 
397
    (*str)++;
-
 
398
}
-
 
399
 
-
 
400
void rattoascii( int16_t num, uint8_t decimalplace, char *string){ //decimalplace is number of decimals
-
 
401
    uint8_t i;
-
 
402
#define ANTAL_NUFFROR 3
-
 
403
    if( num<0 ) {
-
 
404
        *(string++) = '-';
-
 
405
        num = -num;
-
 
406
    }
-
 
407
    numtoascii(num>>decimalplace, &string );
-
 
408
    *(string++) = '.';
-
 
409
    for(i=0;i<ANTAL_NUFFROR;i++) {
-
 
410
        num %= 1<<decimalplace;
-
 
411
        num *= 10;
-
 
412
        *(string++) = '0' + (num>>decimalplace);
-
 
413
    }
-
 
414
    *(string++) = 0;
-
 
415
}
374
 
416
 
375
 
417
 
376
//Supersnodd:
418
//Supersnodd:
377
 
419
 
378
 
420
 
379
 
421
 
380
uint8_t _io_buf[10];
422
uint8_t _io_buf[10];
-
 
423
 
-
 
424
 
-
 
425
/*
-
 
426
uint8_t *parsetemperature(int16_t value,uint8_t *str,uint8_t decimal,uint8_t options){
-
 
427
    uint16_t tmp;
-
 
428
    uint8_t i=0;
-
 
429
   
-
 
430
    //value = 0xfcf4;
-
 
431
    //decimal = 6;
-
 
432
   
-
 
433
    if(value<0) {
-
 
434
        str[i++]='-'; //add negative sign
-
 
435
        tmp=-value>>decimal;
-
 
436
    } else {
-
 
437
        if(options&0x01) str[i++]=' '; //Can the number be negative? Leave space for minussign
-
 
438
        tmp=value>>decimal;
-
 
439
    }  
-
 
440
   
-
 
441
    //Let's start axing up the value to print it beautifully. We have tmp, the integer part of the value
-
 
442
    while(tmp>0) {
-
 
443
        str[i++]='0' + (tmp%10);
-
 
444
        tmp=tmp/10;
-
 
445
    }  
-
 
446
   
-
 
447
    //now we hafve printed all of zat
-
 
448
    str[i++] = '.';
-
 
449
   
-
 
450
    //let's go on to the fractional part... (cwap!)
-
 
451
   
-
 
452
   
-
 
453
    str[i++]=0;
-
 
454
    return(str);
-
 
455
}
-
 
456
*/
381
 
457
 
-
 
458
// nŒn annans rutin som jag inte riktigt fšrstŒr mig pŒ.
382
uint8_t *io_s16toStr(int16_t value,uint8_t *str,uint8_t decimal,uint8_t options) {
459
uint8_t *io_s16toStr(int16_t value,uint8_t *str,uint8_t decimal,uint8_t options) {
383
    uint8_t i=0,j=0,w=0;
460
    uint8_t i=0,j=0,w=0;
384
    uint16_t tmp;
461
    uint16_t tmp;
385
   
462
   
386
    if(value<0) {
463
    if(value<0) {
387
        str[i++]='-';
464
        str[i++]='-';
388
        tmp=-value;
465
        tmp=-value;
389
    } else {
466
    } else {
390
        if(options&0x01) str[i++]=' ';
467
        if(options&0x01) str[i++]=' ';
391
        tmp=value;
468
        tmp=value;
392
    }
469
    }
393
    while(tmp>0) {
470
    while(tmp>0) {
394
        _io_buf[j++]='0'+(tmp%10);
471
        _io_buf[j++]='0'+(tmp%10);
395
        tmp=tmp/10;
472
        tmp=tmp/10;
396
    }
473
    }
397
    if(j<=decimal) {
474
    if(j<=decimal) {
398
        str[i++]='0';
475
        str[i++]='0';
399
        if(decimal>0) str[i++]='.';
476
        if(decimal>0) str[i++]='.';
400
        w=i;
477
        w=i;
401
        while((i-w)<(decimal-j)) str[i++]='0';
478
        while((i-w)<(decimal-j)) str[i++]='0';
402
        while(j!=0) str[i++]=_io_buf[--j];
479
        while(j!=0) str[i++]=_io_buf[--j];
403
    } else {
480
    } else {
404
        while(j!=0) {
481
        while(j!=0) {
405
            str[i++]=_io_buf[--j];
482
            str[i++]=_io_buf[--j];
406
            if(j==decimal) str[i++]='.';
483
            if(j==decimal) str[i++]='.';
407
        }
484
        }
408
    }
485
    }
409
    str[i++]=0;
486
    str[i++]=0;
410
    return(str);
487
    return(str);
411
}
488
}
412
 
489
 
413
uint8_t *io_u16toStr(uint16_t value,uint8_t *str,uint8_t decimal,uint8_t options) {
490
uint8_t *io_u16toStr(uint16_t value,uint8_t *str,uint8_t decimal,uint8_t options) {
414
    uint8_t i=0;
491
    uint8_t i=0;
415
    uint8_t j=0;
492
    uint8_t j=0;
416
    uint8_t w=0;
493
    uint8_t w=0;
417
   
494
   
418
    while(value>0) {
495
    while(value>0) {
419
        _io_buf[j++]='0'+(value%10);
496
        _io_buf[j++]='0'+(value%10);
420
        value=value/10;
497
        value=value/10;
421
    }
498
    }
422
 
499
 
423
    if(j<=decimal) {
500
    if(j<=decimal) {
424
        str[i++]='0';
501
        str[i++]='0';
425
        if(decimal>0) str[i++]='.';
502
        if(decimal>0) str[i++]='.';
426
        w=i;
503
        w=i;
427
        while((i-w)<(decimal-j)) str[i++]='0';
504
        while((i-w)<(decimal-j)) str[i++]='0';
428
        while(j!=0) str[i++]=_io_buf[--j];
505
        while(j!=0) str[i++]=_io_buf[--j];
429
    } else {
506
    } else {
430
        while(j>0) {
507
        while(j>0) {
431
            str[i++]=_io_buf[--j];
508
            str[i++]=_io_buf[--j];
432
            if((decimal>0)&&(j==decimal)) str[i++]='.';
509
            if((decimal>0)&&(j==decimal)) str[i++]='.';
433
        }
510
        }
434
    }
511
    }
435
    str[i++]=0;
512
    str[i++]=0;
436
    return(str);
513
    return(str);
437
}
514
}
438
 
515