Feng Hong / Mbed OS Nucleo_rtos_basic
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include <HX711.h>
00003 #include <eeprom.h>
00004 #include "eeprom_cust.h"
00005 //#include "digitLCD.h"
00006 #include "SB1602E.h"
00007 #include "yoda2.h"
00008 #include "TextLCD.h"
00009 #include "DS3231.h"
00010 
00011 
00012 DigitalOut relay4(RELAY_4);
00013 DigitalOut relay3(RELAY_3);
00014 DigitalOut relay2(RELAY_2);
00015 DigitalOut relay1(RELAY_1);
00016 DigitalIn din1(DIN1);
00017 DigitalIn din2(DIN2);
00018 DigitalIn din3(DIN3);
00019 DigitalIn din4(DIN4);
00020 DigitalIn din5(DIN5);
00021 DigitalIn din6(DIN6);
00022 DigitalIn din7(DIN7);
00023 DigitalIn din8(DIN8);
00024 
00025 extern int moveMotor(int motornumber, int distance_mm, bool direction, bool freemove, bool gobackhome);
00026 extern int moveMotor1(int distance_mm, bool direction, bool freemove, bool gobackhome);
00027 extern int moveMotor2(int distance_mm, bool direction, bool freemove, bool gobackhome);
00028 extern int moveMotor3(int distance_mm, bool direction, bool freemove, bool gobackhome);
00029 extern int moveMotor3Until(bool direction, DigitalIn sensorpin);
00030 extern DigitalOut motor3_pul;
00031 extern DigitalOut motor3_en;
00032 extern DigitalOut motor3_dir;
00033 
00034 EventFlags button_event_flags;
00035 EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C32);
00036 extern void eeprom_test(void);
00037 #ifdef YODA2
00038 Serial uart1(PC_4, PC_5); // tx, rx
00039 // I2C Communication
00040 I2C i2c_lcd(LCD_SDA, LCD_SCL); // SDA, SCL
00041 TextLCD_I2C_N lcd(&i2c_lcd, ST7032_SA, TextLCD::LCD16x2, NC, TextLCD::ST7032_3V3);
00042 #endif
00043 #if 1
00044 int current_weight;
00045 //EEPROM ep(SDA,SCL,EEPROM_ADDR,EEPROM::T24C256);
00046 int device_address = DEVICE_DEFAULT_ADDRESS;  // address 11 bits: last 8 bits is device_address
00047 int device_type;  //address 11 bits: first 3 bits is device_type
00048 int init_id = 0x00300000;  // first 11 bit is the address
00049 int init_filter_handle, broadcast_filter_handle;
00050 int broadcast_id = 0x1ffC0000;
00051 #endif
00052 
00053 
00054 #if 1
00055 
00056 EventFlags LCD_update_flags;
00057 extern void main_menu();
00058 extern void analyzePayload();
00059 extern Device_Type_d device_type_v;
00060 extern data_field_d can_txdata_frame;
00061 extern CANMessage tx_message;
00062 #if 1
00063 #ifdef STM32F207xx
00064 HX711 hx711(PB_11, PB_10);// data, clk
00065 #endif
00066 
00067 #ifdef STM32F303xE
00068 //HX711 hx711(D8, D9);// data, clk
00069 #ifdef OWN_SOLDER_BOARD
00070 HX711 hx711(PA_14, PA_15);
00071 #endif
00072 #ifdef YODA2
00073 HX711 hx711(SCALE_SDA, SCALE_SCL);
00074 #endif
00075 #endif
00076 extern void scaleCalibration(bool release_led);
00077 extern void init_scale();
00078 Thread scale_thread;
00079 extern void scale_reading();
00080 #endif
00081 unsigned char rx_buffer[8], tx_buffer[8];
00082 unsigned char rx_length, tx_length;
00083 
00084 //#define LCD_1602
00085 #ifdef STM32F207xx
00086 SB1602E lcd( PB_9, PB_8 );  //  SDA, SCL
00087 CAN can1(PD_0, PD_1);
00088 CAN can2(PB_5, PB_6);
00089 DigitalOut led1(LED1);
00090 DigitalOut led2(LED2);
00091 //FlashIAP flashIAP;
00092 
00093 //#define LCD_1621
00094 //digitLCD lcd(PA_5,PA_4,PB_5); // WO, CS, DATA
00095 #endif
00096 #ifdef STM32F303xE
00097 
00098 #ifdef LCD_1602
00099 SB1602E lcd(D14, D15 );  //  SDA, SCL
00100 #endif
00101 CAN can1(CAN_RD, CAN_TD); // RD, TD
00102 DigitalOut led1(LED1);  // only one LED PA_5
00103 DigitalOut led2(LED2);  // only one LED PA_5
00104 #endif
00105 
00106 #if 0
00107 DigitalOut output1(PC_3);
00108 DigitalOut output2(PC_2);
00109 DigitalOut output3(PB_7);
00110 DigitalIn input1(PC_6);
00111 DigitalIn input2(PC_8);
00112 #endif
00113 
00114 
00115 uint8_t can_tx_data[8];
00116 uint8_t can_rx_data[8];
00117 #endif
00118 void print_char(char c = '*')
00119 {
00120     printf("%c\r\n", c);
00121     fflush(stdout);
00122 }
00123 
00124 #if 1
00125 Thread can_receivethread;
00126 Thread can_handlethread;
00127 Thread mainmenu_thread;
00128 bool can_register_success = false; 
00129 CANMessage  msg;
00130 MemoryPool<CANMessage, 16> can_mpool;
00131 Queue<CANMessage, 16> can_queue;
00132 #endif
00133 InterruptIn button0(USER_BUTTON);
00134 volatile bool button0_pressed = false; // Used in the main loop
00135 volatile bool button0_enabled = true; // Used for debouncing
00136 Timeout button0_timeout; // Used for debouncing
00137 InterruptIn button1(BUTTON1);
00138 volatile bool button1_pressed = false; // Used in the main loop
00139 volatile bool button1_enabled = true; // Used for debouncing
00140 Timeout button1_timeout; // Used for debouncing
00141 InterruptIn button2(BUTTON2);
00142 volatile bool button2_pressed = false; // Used in the main loop
00143 volatile bool button2_enabled = true; // Used for debouncing
00144 Timeout button2_timeout; // Used for debouncing
00145 InterruptIn button3(BUTTON3);
00146 volatile bool button3_pressed = false; // Used in the main loop
00147 volatile bool button3_enabled = true; // Used for debouncing
00148 Timeout button3_timeout; // Used for debouncing
00149 InterruptIn button4(BUTTON4);
00150 volatile bool button4_pressed = false; // Used in the main loop
00151 volatile bool button4_enabled = true; // Used for debouncing
00152 Timeout button4_timeout; // Used for debouncing
00153 
00154 
00155 // Enables button when bouncing is over
00156 //button0
00157 void button0_enabled_cb(void)
00158 {
00159     int button_status;
00160     button_status = button0.read();
00161     if (button_status == 0)
00162     {    
00163         printf("button0 down\r\n");
00164 //        scaleCalibration(true);
00165         moveMotor3(100, true, true, false);
00166         button_status = button0.read();
00167         if (button_status == 0)
00168         {
00169             printf("button0 hold\r\n");
00170         }
00171         else
00172         {
00173             printf("button0 press hold and release\r\n");
00174         }
00175     }    
00176     else
00177         printf("button0 released\r\n");
00178     button0_enabled = true;
00179 }
00180 
00181 // ISR handling button pressed event
00182 void button0_onpressed_cb(void)
00183 {
00184     if (button0_enabled) { // Disabled while the button is bouncing
00185         button0_enabled = false;
00186         button0_pressed = true; // To be read by the main loop
00187         button0_timeout.attach(callback(button0_enabled_cb), 0.3); // Debounce time 300 ms
00188     }
00189 }
00190 //button0--
00191 //button1
00192 void button1_enabled_cb(void)
00193 {
00194     int button_status;
00195     button_status = button1.read();
00196     if (button_status == 0)
00197     {    
00198         printf("button1 down\r\n");
00199         button_event_flags.set(BUTTON1_HOLD_EVENT);
00200     }    
00201     else
00202     {
00203         printf("button1 released\r\n");
00204         button_event_flags.set(BUTTON1_PRESSED_EVENT);
00205     }
00206     button1_enabled = true;
00207 }
00208 
00209 // ISR handling button pressed event
00210 void button1_onpressed_cb(void)
00211 {
00212     if (button1_enabled) { // Disabled while the button is bouncing
00213         button1_enabled = false;
00214         button1_pressed = true; // To be read by the main loop
00215         button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
00216     }
00217 }
00218 //button1--
00219 //button2
00220 void button2_enabled_cb(void)
00221 {
00222     int button_status;
00223     button_status = button2.read();
00224     if (button_status == 0)
00225     {    
00226         printf("button2 down\r\n");
00227         button_event_flags.set(BUTTON2_HOLD_EVENT);
00228     }    
00229     else
00230     {
00231         printf("button2 released\r\n");
00232         button_event_flags.set(BUTTON2_PRESSED_EVENT);        
00233     }
00234     button2_enabled = true;
00235 }
00236 
00237 // ISR handling button pressed event
00238 void button2_onpressed_cb(void)
00239 {
00240     if (button2_enabled) { // Disabled while the button is bouncing
00241         button2_enabled = false;
00242         button2_pressed = true; // To be read by the main loop
00243         button2_timeout.attach(callback(button2_enabled_cb), 0.3); // Debounce time 300 ms
00244     }
00245 }
00246 //button2--
00247 //button3
00248 void button3_enabled_cb(void)
00249 {
00250     int button_status;
00251     button_status = button3.read();
00252     if (button_status == 0)
00253     {    
00254         printf("button3 down\r\n");
00255         button_event_flags.set(BUTTON3_HOLD_EVENT);        
00256     }    
00257     else
00258     {
00259         printf("button3 released\r\n");
00260         button_event_flags.set(BUTTON3_PRESSED_EVENT);  
00261     }
00262     button3_enabled = true;
00263 }
00264 
00265 // ISR handling button pressed event
00266 void button3_onpressed_cb(void)
00267 {
00268     if (button3_enabled) { // Disabled while the button is bouncing
00269         button3_enabled = false;
00270         button3_pressed = true; // To be read by the main loop
00271         button3_timeout.attach(callback(button3_enabled_cb), 0.3); // Debounce time 300 ms
00272     }
00273 }
00274 //button3--
00275 //button4
00276 void button4_enabled_cb(void)
00277 {
00278     int button_status;
00279     button_status = button4.read();
00280     if (button_status == 0)
00281     {    
00282         printf("button4 down\r\n");
00283         button_event_flags.set(BUTTON4_HOLD_EVENT);         
00284     }    
00285     else
00286     {
00287         printf("button4 released\r\n");
00288         button_event_flags.set(BUTTON4_PRESSED_EVENT);   
00289     }
00290     button4_enabled = true;
00291 }
00292 
00293 // ISR handling button pressed event
00294 void button4_onpressed_cb(void)
00295 {
00296     if (button4_enabled) { // Disabled while the button is bouncing
00297         button4_enabled = false;
00298         button4_pressed = true; // To be read by the main loop
00299         button4_timeout.attach(callback(button4_enabled_cb), 0.3); // Debounce time 300 ms
00300     }
00301 }
00302 //button4--
00303 #if 1
00304 void can_sendData(int can_id, uint8_t *tx_data, int length)
00305 {
00306     CANMessage txmsg;
00307 
00308     txmsg.format = CANExtended;
00309     txmsg.id = can_id;
00310     txmsg.len = length;
00311     txmsg.data[0] = tx_data[0];
00312     txmsg.data[1] = tx_data[1];
00313     txmsg.data[2] = tx_data[2];
00314     txmsg.data[3] = tx_data[3];
00315     txmsg.data[4] = tx_data[4];
00316     txmsg.data[5] = tx_data[5];
00317     txmsg.data[6] = tx_data[6];
00318     txmsg.data[7] = tx_data[7];                            
00319     
00320 //    printf("can_sendData can_id=0x%08x \r\n", can_id);
00321     can1.write(txmsg);    
00322 }
00323 
00324 void can_rxthread()
00325 {
00326     int loop;
00327     while (true) {
00328  #if 1      
00329         if(can1.read(msg)) {
00330             print_char();
00331             printf("got message id=%d 0x%08x\r\n", msg.id, msg.id);
00332 //            b = *reinterpret_cast<int*>(msg.data);
00333             for (loop = 0; loop < msg.len; loop++)
00334             {
00335                 can_rx_data[loop] = msg.data[loop];
00336             }
00337             
00338             printf("got data: length:%d\r\n", msg.len);
00339             for (loop = 0; loop < msg.len; loop++)
00340             {
00341                 printf("data[%d]=%d\r\n", loop, can_rx_data[loop]);
00342             }           
00343 //            if(msg.id == 1337) 
00344             {
00345                 //only queue the message belongs to you
00346                 CANMessage *can_message = can_mpool.alloc();
00347                 memcpy((void *)can_message, (void *)&msg, sizeof(msg)); 
00348                 if (!can_queue.full())
00349                     can_queue.put(can_message);
00350                 else
00351                 {
00352                     printf("message queue is full. \r\n");    
00353                 }                
00354                 led2 = !led2;
00355              }
00356         }
00357 #endif
00358         wait(0.2);
00359     }
00360 }
00361 #endif
00362 void i2c_scanner(PinName sda, PinName scl)
00363 {
00364     I2C i2c(sda, scl); 
00365     
00366     int error, address;
00367     int nDevices;
00368     
00369     i2c.frequency(100000);
00370     printf("i2c_scanner sda=%d scl=%d \r\n", (int)sda, (int)scl); 
00371     printf("Scanning...\r\n");
00372     
00373     nDevices = 0;
00374     
00375     for(address = 0; address < 127; address++ ) 
00376     {
00377         i2c.start();
00378 //        error = i2c.write(address << 1); //We shift it left because mbed takes in 8 bit addreses
00379         error = i2c.write(address << 1, "1", 1, false);
00380         i2c.stop();
00381         if (error == 0)
00382         {
00383           printf("I2C device found at address 7bit:0x%X (8bit:0x%X)\r\n", address, (address<<1)); //Returns 8-bit addres
00384           nDevices++;
00385         }
00386     }
00387     if (nDevices == 0)
00388         printf("No I2C devices found\r\n");
00389     else
00390         printf("\r\ndone\r\n");
00391 }
00392 int main()
00393 {
00394     int loop = 0;
00395     int8_t ival;
00396     unsigned int can_id;
00397     int distance = 0;
00398 #if 0    
00399     int hour;
00400     int minute;
00401     int second;
00402 
00403     int dayOfWeek;
00404     int date;
00405     int month;
00406     int year;
00407     DS3231 rtc_test(SDA, SCL);
00408 //    DS3231 rtc_test(PF_0, PF_1);
00409 //        printf("\r\n\nDS3231 Library test program\r\nremi cormier 2012\r\n\n");
00410     
00411     rtc_test.setI2Cfrequency(400000);
00412     
00413     //rtc_test.writeRegister(DS3231_Aging_Offset,0); // uncomment to set Aging Offset 1LSB = approx. 0.1 ppm according from datasheet = 0.05 ppm @ 21 °C from my measurments
00414      
00415     rtc_test.convertTemperature();
00416       
00417     int reg=rtc_test.readRegister(DS3231_Aging_Offset);
00418     if (reg>127)
00419         {reg=reg-256;}
00420     printf("Aging offset : %i\r\n",reg);
00421          
00422     printf("OSF flag : %i",rtc_test.OSF());
00423     printf("\r\n");
00424      
00425     rtc_test.readDate(&date,&month,&year);
00426     printf("date : %02i-%02i-%02i",date,month,year);
00427     printf("\r\n");
00428      
00429     rtc_test.setTime(19,48,45); // uncomment to set time
00430      
00431     rtc_test.readTime(&hour,&minute,&second);
00432     printf("time : %02i:%02i:%02i",hour,minute,second);
00433     printf("\r\n");
00434      
00435     rtc_test.setDate(6,22,12,2012); // uncomment to set date
00436  #endif   
00437     uart1.baud(115200);
00438     uart1.printf("\n\n*** Hello Yoda2! ***\r\n");
00439     printf("\n\n*** Hello Yoda2! ***\r\n");
00440 
00441 //    wait(1);
00442 #if 1
00443     ep.read((uint32_t)EEPROM_DEVICE_ADDRESS_ADDRESS,device_address); 
00444     printf("EEPROM: read device address:%d 0x%08x\r\n", device_address, device_address);
00445     if ((device_address == 0) || (device_address == 0xFFFFFFFF))
00446         device_address = DEVICE_DEFAULT_ADDRESS;
00447     device_type = (device_address & 0x00000700) >> 8;
00448     device_type_v = (Device_Type_d)device_type;
00449 #ifdef LCD_1621
00450     lcd.clear();            // clears display
00451     lcd.allsegson();
00452    
00453  //   lcd.printf("ABCDEFGHI"); // Standard printf function, All ASCII characters will display
00454 #endif  
00455 #ifdef YODA2
00456 #if 1
00457     lcd.cls();
00458     lcd.setContrast(31);
00459     lcd.setCursor(TextLCD::CurOff_BlkOff);
00460     lcd.setAddress(0,0);
00461     lcd.printf("Hello Yoda2!");
00462 #endif    
00463 #endif
00464 #ifdef LCD_1602
00465 //    lcd.printf( 0, "Hello world!" );    //  line# (0 or 1), string
00466 //   lcd.printf( 1, "pi = %.6f", 3.14159265 );
00467 //    lcd.putcxy(0x55, 5, 1);
00468 //    lcd.printf(5, 0, "UUU");
00469 //    lcd.printf(0, 0, "pressed!" );      
00470 #endif   
00471 
00472 //    input1.mode(PullUp);
00473 //    input2.mode(PullUp);
00474 
00475 //    can1.reset();
00476 //    can2.reset();
00477 
00478 #if 1
00479     printf("device_address =0x%08x \r\n", (device_address<<18));
00480     can1.frequency(100000);
00481     can1.filter((device_address<<18), 0x1FFC0000, CANExtended, init_filter_handle);  // 0x1FFC0000 to filter the last 18bits 0-17
00482     device_address = (device_address & 0x000000FF);
00483 //only support one filter
00484 //    can1.filter(broadcast_id, 0x1FFC0000, CANExtended, broadcast_filter_handle); // the broadcast id
00485 //    can2.frequency(100000);
00486     //button0.mode(PullUp); // Activate pull-up
00487     can_receivethread.start(can_rxthread);  
00488     can_handlethread.start(analyzePayload);   
00489 #endif    
00490 
00491 #endif
00492     button1.mode(PullUp);
00493     button2.mode(PullUp);
00494     button3.mode(PullUp);
00495     button4.mode(PullUp);
00496     button0.fall(callback(button0_onpressed_cb)); // Attach ISR to handle button press event
00497     button0.rise(callback(button0_onpressed_cb)); // Attach ISR to handle button press event
00498     button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
00499     button1.rise(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
00500     button2.fall(callback(button2_onpressed_cb)); // Attach ISR to handle button press event
00501     button2.rise(callback(button2_onpressed_cb)); // Attach ISR to handle button press event
00502     button3.fall(callback(button3_onpressed_cb)); // Attach ISR to handle button press event
00503     button3.rise(callback(button3_onpressed_cb)); // Attach ISR to handle button press event
00504     button4.fall(callback(button4_onpressed_cb)); // Attach ISR to handle button press event
00505     button4.rise(callback(button4_onpressed_cb)); // Attach ISR to handle button press event    
00506 //    i2c_scanner(PB_9, PB_8);  
00507 //    eeprom_test();
00508 
00509 #if 1  
00510 //    scaleCalibration(true);
00511     switch (device_type_v)
00512     {
00513         case CupTrack:  
00514             lcd.setAddress(0,1);
00515             lcd.printf("CupTrack ");
00516             break;
00517         case JamTrack:
00518             lcd.setAddress(0,1);
00519             lcd.printf("JamTrack ");
00520             break;
00521         case TeaTrack:
00522             lcd.setAddress(0,1);
00523             lcd.printf("TeaTrack ");
00524             break;
00525         case Tea:
00526             lcd.setAddress(0,1);
00527             lcd.printf("Tea ");
00528             init_scale();
00529             scale_thread.start(scale_reading);
00530             break;
00531         case Jam:
00532             lcd.setAddress(0,1);
00533             lcd.printf("Jam ");
00534             init_scale();
00535             scale_thread.start(scale_reading);
00536             break;
00537         case Shaker:
00538             lcd.setAddress(0,1);
00539             lcd.printf("Shaker ");
00540             break;
00541         default:
00542             break;        
00543     }
00544     lcd.printf("%d", device_address);
00545     mainmenu_thread.start(main_menu);
00546 
00547 #endif
00548     moveMotor3(0, true, false, false);
00549     wait(2);
00550     while(1) {
00551         wait(2); 
00552 #if 1              
00553         printf("Sensors:\r\n");
00554         printf("%d %d %d %d \r\n", din1.read(), din2.read(), din3.read(), din4.read());
00555         printf("%d %d %d %d \r\n", din5.read(), din6.read(), din7.read(), din8.read());    
00556         printf("Sensors:\r\n");
00557         printf("%d %d %d %d \r\n", din1.read(), din2.read(), din3.read(), din4.read());
00558         printf("%d %d %d %d \r\n", din5.read(), din6.read(), din7.read(), din8.read()); 
00559 #endif
00560 #if 0        
00561         distance = 0;
00562         distance = moveMotor3Until(true, din5);
00563         printf("F distance=%d \r\n", distance);       
00564         printf("Sensors:\r\n");
00565         printf("%d %d %d %d \r\n", din1.read(), din2.read(), din3.read(), din4.read());
00566         printf("%d %d %d %d \r\n", din5.read(), din6.read(), din7.read(), din8.read());  
00567         wait(1);
00568         distance = 0;
00569         distance = moveMotor3Until(false, din5);  
00570         printf("Sensors:\r\n");
00571         printf("%d %d %d %d \r\n", din1.read(), din2.read(), din3.read(), din4.read());
00572         printf("%d %d %d %d \r\n", din5.read(), din6.read(), din7.read(), din8.read()); 
00573         printf("B distance=%d \r\n", distance);
00574 #endif
00575 #if 0        
00576         relay4 = 1;
00577         printf("relay4 turn on\r\n");
00578         wait(2);
00579         relay3 = 1;
00580         printf("relay3 turn on\r\n");
00581         wait(2);        
00582         relay2 = 1;
00583         printf("relay2 turn on\r\n");
00584         wait(2);  
00585         relay1 = 1;
00586         printf("relay1 turn on\r\n");
00587         wait(2); 
00588         relay4 = 0;
00589         relay3 = 0;
00590         relay2 = 0;
00591         relay1 = 0;
00592 #endif
00593 #if 0        
00594         rtc_test.readDateTime(&dayOfWeek,&date,&month,&year,&hour,&minute,&second);
00595         printf("date time : %i / %02i-%02i-%02i %02i:%02i:%02i",dayOfWeek,date,month,year,hour,minute,second);
00596         printf("\r\n");
00597      
00598         printf("temperature :%6.2f", rtc_test.readTemp());
00599         printf("\r\n");
00600 #endif        
00601 #if 0
00602         moveMotor3(850, true, false, false);
00603         wait(5);
00604         moveMotor3(850, false, false, false);  
00605 #endif        
00606 #if 0             
00607         moveMotor1(100, true, false, false);
00608         moveMotor2(100, true, false, false);
00609         moveMotor3(100, true, false, false);
00610         wait(1);
00611         moveMotor1(150, true, false, false);
00612         moveMotor2(150, true, false, false);
00613         moveMotor3(150, true, false, false);
00614         wait(1);
00615         moveMotor1(250, false, false, false);
00616         moveMotor2(250, false, false, false);
00617         moveMotor3(250, false, false, false);
00618 #endif        
00619 #if 1        
00620         if (!can_register_success)
00621         {    
00622             can_txdata_frame.cmd = COMMAND_REGISTER;
00623             can_txdata_frame.value1 = 0;
00624             can_txdata_frame.value2 = 0;
00625             can_txdata_frame.value3 = 0;
00626             memcpy(can_tx_data, (unsigned char *)&can_txdata_frame, sizeof(can_tx_data));
00627 //            printf("cmd=0x%08x value1=0x%08x size=%d %d\r\n", can_txdata_frame.cmd, can_txdata_frame.value1, sizeof(can_tx_data), sizeof(can_txdata_frame));
00628 //            printf("data 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x \r\n", can_tx_data[0], can_tx_data[1], can_tx_data[2], can_tx_data[3], can_tx_data[4], can_tx_data[5], can_tx_data[6], can_tx_data[7]);  
00629             can_id = (RASPBERRY_PI_CAN_ADDRESS << 18) | (((device_type << 8) |device_address) << 7) | 0x80000000;
00630 //            printf("device_address = 0x%08x can_id=0x%08x \r\n", device_address, can_id);
00631             can_sendData(can_id, can_tx_data, 8);        
00632         }
00633 #endif        
00634     }
00635 
00636 #if 0
00637     int idx = 0; // Just for printf below
00638     can_tx_data[0] = 0;
00639     while(1) {
00640         if (button0_pressed) { // Set when button is pressed
00641 #if 0        
00642             printf("scale value %f. \r\n", hx711.getGram());
00643 #endif
00644             can_tx_data[1] = can_tx_data[0]+1;
00645             can_tx_data[2] = can_tx_data[1]+1;
00646             can_tx_data[3] = can_tx_data[2]+1;
00647             can_tx_data[4] = can_tx_data[3]+1;
00648             can_tx_data[5] = can_tx_data[4]+1;
00649             can_tx_data[6] = can_tx_data[5]+1;
00650             can_tx_data[7] = can_tx_data[6]+1; 
00651             button0_pressed = false;
00652             printf("Button pressed %d\r\n", idx++);
00653             printf("ID=%d data[0]=%d. \r\n", init_id + idx%10, can_tx_data[0]);
00654 #ifdef LCD_1602
00655             lcd.printf(0, 0, "%d ", idx );    //  line# (0 or 1), string
00656 #endif   
00657             can1.write(CANMessage((init_id + idx%10), reinterpret_cast<char*>(can_tx_data), 8, CANData,CANExtended));            
00658             led1 = !led1;
00659             can_tx_data[0]++;
00660         }
00661     }
00662 #endif
00663   
00664 }