「裁線條碼亮燈」:修訂間差異

出自TSK WIKI
跳至導覽 跳至搜尋
Root​(留言 | 貢獻)
Root​(留言 | 貢獻)
第12行: 第12行:
   
   
#define NUM_LEDS 60 // led灯数量
#define NUM_LEDS 60 // led灯数量
#define DATA_PIN 6 // 数据传输引脚(Digital 6)
#define DATA_PIN 10 // 数据传输引脚(Digital 6)
#define BRIGHTNESS 25 //設定led亮度
#define BRIGHTNESS 25 //設定led亮度
   
   
第20行: 第20行:
#define A  A0
#define A  A0
#define B  A1
#define B  A1
#define C  A2
#define C  A2  
   
   
int LIGHTDELAY= 1/NUM_LEDS;
int LIGHTDELAY= 60/NUM_LEDS/60;
String s;
String s;
CRGB leds[NUM_LEDS];
CRGB leds[NUM_LEDS];
第57行: 第56行:
       // If bit position not in 2, it's uppercase words
       // If bit position not in 2, it's uppercase words
       OnKeyScanned(i > 2, buf, buf[i]);
       OnKeyScanned(i > 2, buf, buf[i]);
     }
     }  
     return;
     return;
   }
   }
第68行: 第66行:
     if (upper) return (key - 4 + 'A');
     if (upper) return (key - 4 + 'A');
     else return (key - 4 + 'a');
     else return (key - 4 + 'a');
   }
   }  
   // Numbers
   // Numbers
   else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
   else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
第77行: 第74行:
   if(key== 55 ) {  
   if(key== 55 ) {  
     return 46;  
     return 46;  
     }else return 0;
     }else return 0;  
}
}
   
   
第85行: 第81行:
  // Serial.print((char)ascii);   
  // Serial.print((char)ascii);   
  delay(100);
  delay(100);
   s= s + ((char)ascii) + "";
   s= s + ((char)ascii) + "";  
}
}
   
   
void MyParser::OnScanFinished() {
void MyParser::OnScanFinished() {
   int i,k,j=2;
   int i,k,j=2;  
   float a=s.toFloat();
   float a=s.toFloat();
// float b=a;
   a=a*60;
   a=a*60;
   int uplimit=ceil(a)-1;
   int uplimit=ceil(a)-1;
   int downlimit=floor(a)-1;
   int downlimit=floor(a)-1;
  //Serial.println(uplimit); 
 
  FastLED.clear();
    
    
   
  /*
   for(i=1;i< downlimit;i++){
   for(i=1;i< downlimit;i++){
     leds[i] = CRGB::Yellow;
     leds[i] = CRGB::Yellow;
     FastLED.show();
     FastLED.show();
     delay(LIGHTDELAY);
     delay(LIGHTDELAY);
     FastLED.clear();
     FastLED.clear();  
    matrix.setCursor(5, 0);
  }
      
  */
  matrix.fillScreen(matrix.Color333(0, 0, 0)); // fill the screen with 'black'
  matrix.setCursor(5, 0);
  matrix.setTextColor(matrix.Color333(7,0,0));
     matrix.fillScreen(matrix.Color333(0, 0, 0)); // fill the screen with 'black'
  matrix.setTextSize(1);
    matrix.setTextColor(matrix.Color333(7,0,0));
  matrix.print(s);
    delay(100);
  matrix.setCursor(25, 7);
    matrix.setTextSize(1);
  matrix.setTextColor(matrix.Color333(0,7,0));
    matrix.print(s);
  matrix.print("m");
    matrix.setCursor(25, 7);
 
    matrix.setTextColor(matrix.Color333(0,7,0));
   
    matrix.print("m");  
   }
   
FastLED.setBrightness(  BRIGHTNESS );
  FastLED.clear();
   FastLED.show();
   if(uplimit==downlimit){
  FastLED.setBrightness(  10 );  
 
   if(uplimit==downlimit){  
     leds[uplimit] = CRGB::White;
     leds[uplimit] = CRGB::White;
   }else{
   }else{
第127行: 第119行:
     leds[downlimit] = CRGB::Green;  // 设置第2个led灯的颜色Red
     leds[downlimit] = CRGB::Green;  // 设置第2个led灯的颜色Red
   }
   }
  leds[0] = CRGB::Black;
  delay(1000);
   FastLED.show();
   FastLED.show();
   Serial.println(s);
   Serial.println( uplimit);
  Serial.println( downlimit); 
 
   s="";
   s="";
   
   
第144行: 第138行:
   matrix.begin();
   matrix.begin();
   Serial.begin( 115200 );
   Serial.begin( 115200 );
   Serial.println("Start");
   Serial.println("Start");  
   
   
   if (Usb.Init() == -1) {
   if (Usb.Init() == -1) {
第151行: 第144行:
   }
   }
   
   
   delay( 200 );
   delay( 100 );  
   Hid.SetReportParser(0, &Parser);
   Hid.SetReportParser(0, &Parser);
  matrix.fillScreen(matrix.Color333(0, 0, 0));
  FastLED.clear();
}
}
   
   
第161行: 第151行:
   Usb.Task();
   Usb.Task();
}
}
</c>
</c>



於 2019年1月15日 (二) 10:00 的修訂

20190115

<c> //20190115 增加LED看板顯示

  1. include <usbhid.h>
  2. include <usbhub.h>
  3. include <hiduniversal.h>
  4. include <hidboot.h>
  5. include <SPI.h>
  6. include <FastLED.h>
  7. include <Adafruit_GFX.h> // Core graphics library
  8. include <RGBmatrixPanel.h> // Hardware-specific library
  1. define NUM_LEDS 60 // led灯数量
  2. define DATA_PIN 10 // 数据传输引脚(Digital 6)
  3. define BRIGHTNESS 25 //設定led亮度
  1. define CLK 8 // MUST be on PORTB! (Use pin 11 on Mega)
  2. define LAT A3
  3. define OE 9
  4. define A A0
  5. define B A1
  6. define C A2

int LIGHTDELAY= 1/NUM_LEDS; String s; CRGB leds[NUM_LEDS]; RGBmatrixPanel matrix(A, B, C, CLK, LAT, OE, false);

class MyParser : public HIDReportParser {

 public:
   MyParser();
   void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
 protected:
   uint8_t KeyToAscii(bool upper, uint8_t mod, uint8_t key);
   virtual void OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
   virtual void OnScanFinished();

};

MyParser::MyParser() {}

void MyParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {

 // If error or empty, return
 if (buf[2] == 1 || buf[2] == 0) return;

 for (uint8_t i = 7; i >= 2; i--) {
   // If empty, skip
   if (buf[i] == 0) continue;

   // If enter signal emitted, scan finished
   if (buf[i] == UHS_HID_BOOT_KEY_ENTER) {
     OnScanFinished();
   }

   // If not, continue normally
   else {
     // If bit position not in 2, it's uppercase words
     OnKeyScanned(i > 2, buf, buf[i]);
   } 
   return;
 }

}

uint8_t MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {

 // Letters
 if (VALUE_WITHIN(key, 0x04, 0x1d)) {
   if (upper) return (key - 4 + 'A');
   else return (key - 4 + 'a');
 } 
 // Numbers
 else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
   return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
 }

 if(key== 55 ) { 
   return 46; 
   }else return 0; 

}

void MyParser::OnKeyScanned(bool upper, uint8_t mod, uint8_t key) {

 uint8_t ascii = KeyToAscii(upper, mod, key);
// Serial.print((char)ascii);  
delay(100);
 s= s + ((char)ascii) + ""; 

}

void MyParser::OnScanFinished() {

 int i,k,j=2; 
 float a=s.toFloat();
 a=a*60;  
 int uplimit=ceil(a)-1;
 int downlimit=floor(a)-1;
 
/*
 for(i=1;i< downlimit;i++){
   leds[i] = CRGB::Yellow;
   FastLED.show();
   delay(LIGHTDELAY);
   FastLED.clear();    
  }
  */
 matrix.setCursor(5, 0);
   matrix.fillScreen(matrix.Color333(0, 0, 0)); // fill the screen with 'black'
   matrix.setTextColor(matrix.Color333(7,0,0));
   delay(100);
   matrix.setTextSize(1);
   matrix.print(s);
   matrix.setCursor(25, 7);
   matrix.setTextColor(matrix.Color333(0,7,0));
   matrix.print("m");   
    
 FastLED.clear();
 FastLED.show();
 FastLED.setBrightness(  10 );   
 
 if(uplimit==downlimit){   
   leds[uplimit] = CRGB::White;
 }else{
   leds[uplimit] = CRGB::Red;  // 设置第1个led灯的颜色Red
   leds[downlimit] = CRGB::Green;  // 设置第2个led灯的颜色Red
 }
 delay(1000);
 FastLED.show();
 Serial.println( uplimit);
 Serial.println( downlimit);  
 s="";

}

USB Usb; USBHub Hub(&Usb); HIDUniversal Hid(&Usb); MyParser Parser;

void setup() {

 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // 初始化所有LED灯
 FastLED.setBrightness(  BRIGHTNESS );
 matrix.begin();
 Serial.begin( 115200 );
 Serial.println("Start"); 

 if (Usb.Init() == -1) {
   Serial.println("OSC did not start.");
 }

 delay( 100 ); 
 Hid.SetReportParser(0, &Parser);  

}

void loop() {

 Usb.Task();

} </c>

20190104-2

<c> //20190104增加LCD顯示

  1. include <Wire.h>
  2. include <LiquidCrystal_I2C.h>
  3. include <usbhid.h>
  4. include <usbhub.h>
  5. include <hiduniversal.h>
  6. include <hidboot.h>
  7. include <SPI.h>
  8. include <FastLED.h>
  1. define NUM_LEDS 60 // led灯数量
  2. define DATA_PIN 6 // 数据传输引脚(Digital 6)
  3. define BRIGHTNESS 25 //設定led亮度

int LIGHTDELAY= 60/NUM_LEDS/60; String s; CRGB leds[NUM_LEDS]; LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display

class MyParser : public HIDReportParser {

 public:
   MyParser();
   void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
 protected:
   uint8_t KeyToAscii(bool upper, uint8_t mod, uint8_t key);
   virtual void OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
   virtual void OnScanFinished();

};

MyParser::MyParser() {}

void MyParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {

 // If error or empty, return
 if (buf[2] == 1 || buf[2] == 0) return;

 for (uint8_t i = 7; i >= 2; i--) {
   // If empty, skip
   if (buf[i] == 0) continue;

   // If enter signal emitted, scan finished
   if (buf[i] == UHS_HID_BOOT_KEY_ENTER) {
     OnScanFinished();
   }

   // If not, continue normally
   else {
     // If bit position not in 2, it's uppercase words
     OnKeyScanned(i > 2, buf, buf[i]);
   }

   return;
 }

}

uint8_t MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {

 // Letters
 if (VALUE_WITHIN(key, 0x04, 0x1d)) {
   if (upper) return (key - 4 + 'A');
   else return (key - 4 + 'a');
 }

 // Numbers
 else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
   return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
 }

 if(key== 55 ) { 
   return 46; 
   }else return 0;

}

void MyParser::OnKeyScanned(bool upper, uint8_t mod, uint8_t key) {

 uint8_t ascii = KeyToAscii(upper, mod, key);
// Serial.print((char)ascii);  
delay(100);
 s= s + ((char)ascii) + "";

}

void MyParser::OnScanFinished() {

 int i,k,j=2;
 
 float a=s.toFloat();
 String lcdtext2=s +" m";
 lcd.clear();
 lcd.setCursor(1,0);
 lcd.print("Cable Length:");
 lcd.setCursor(6,1);
 lcd.print(lcdtext2);  
 a=a*60;
 int uplimit=ceil(a)-1;
 int downlimit=floor(a)-1;
 FastLED.clear(); 
 for(i=1;i< downlimit;i++){
   leds[i] = CRGB::Yellow;
   FastLED.show();
   delay(LIGHTDELAY);
   FastLED.clear();
 }


 if(uplimit==downlimit){
   leds[uplimit] = CRGB::White;
 }else{
   leds[uplimit] = CRGB::Red;  // 设置第1个led灯的颜色Red
   leds[downlimit] = CRGB::Green;  // 设置第2个led灯的颜色Red
 }
 FastLED.show();
 s="";
 

}

USB Usb; USBHub Hub(&Usb); HIDUniversal Hid(&Usb); MyParser Parser;

void setup() {

 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // 初始化所有LED灯
 FastLED.setBrightness(  BRIGHTNESS );
 Serial.begin( 115200 );
 lcd.init();
 lcd.backlight();
 lcd.setCursor(1,0);
 lcd.print("Cable Length:");


 if (Usb.Init() == -1) {
   Serial.println("OSC did not start.");
 }

 delay( 200 );

 Hid.SetReportParser(0, &Parser);

}

void loop() {

 Usb.Task();

}

</c>

20190104-1

<c> //20190104增加流水燈

  1. include <usbhid.h>
  2. include <usbhub.h>
  3. include <hiduniversal.h>
  4. include <hidboot.h>
  5. include <SPI.h>
  6. include <FastLED.h>
  1. define NUM_LEDS 60 // led灯数量
  2. define DATA_PIN 6 // 数据传输引脚(Digital 6)
  3. define BRIGHTNESS 25 //設定led亮度

int LIGHTDELAY= 60/NUM_LEDS/60; String s; CRGB leds[NUM_LEDS];

class MyParser : public HIDReportParser {

 public:
   MyParser();
   void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
 protected:
   uint8_t KeyToAscii(bool upper, uint8_t mod, uint8_t key);
   virtual void OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
   virtual void OnScanFinished();

};

MyParser::MyParser() {}

void MyParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {

 // If error or empty, return
 if (buf[2] == 1 || buf[2] == 0) return;

 for (uint8_t i = 7; i >= 2; i--) {
   // If empty, skip
   if (buf[i] == 0) continue;

   // If enter signal emitted, scan finished
   if (buf[i] == UHS_HID_BOOT_KEY_ENTER) {
     OnScanFinished();
   }

   // If not, continue normally
   else {
     // If bit position not in 2, it's uppercase words
     OnKeyScanned(i > 2, buf, buf[i]);
   }

   return;
 }

}

uint8_t MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {

 // Letters
 if (VALUE_WITHIN(key, 0x04, 0x1d)) {
   if (upper) return (key - 4 + 'A');
   else return (key - 4 + 'a');
 }

 // Numbers
 else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
   return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
 }

 if(key== 55 ) { 
   return 46; 
   }else return 0;

}

void MyParser::OnKeyScanned(bool upper, uint8_t mod, uint8_t key) {

 uint8_t ascii = KeyToAscii(upper, mod, key);
// Serial.print((char)ascii);  
delay(100);
 s= s + ((char)ascii) + "";

}

void MyParser::OnScanFinished() {

 int i,k,j=2;
 
 float a=s.toFloat();
 a=a*60;
 int uplimit=ceil(a)-1;
 int downlimit=floor(a)-1;
 //Serial.println(uplimit);  
 FastLED.clear();

 for(i=1;i< downlimit;i++){
   leds[i] = CRGB::Yellow;
   FastLED.show();
   delay(LIGHTDELAY);
   FastLED.clear();
 }


 if(uplimit==downlimit){
   leds[uplimit] = CRGB::White;
 }else{
   leds[uplimit] = CRGB::Red;  // 设置第1个led灯的颜色Red
   leds[downlimit] = CRGB::Green;  // 设置第2个led灯的颜色Red
 }
 FastLED.show();
 s="";
 

}

USB Usb; USBHub Hub(&Usb); HIDUniversal Hid(&Usb); MyParser Parser;

void setup() {

 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // 初始化所有LED灯
 FastLED.setBrightness(  BRIGHTNESS );
 Serial.begin( 115200 );
 Serial.println("Start");

 if (Usb.Init() == -1) {
   Serial.println("OSC did not start.");
 }

 delay( 200 );

 Hid.SetReportParser(0, &Parser);

}

void loop() {

 Usb.Task();

}

</c>

20190103

<c>

  1. include <usbhid.h>
  2. include <usbhub.h>
  3. include <hiduniversal.h>
  4. include <hidboot.h>
  5. include <SPI.h>
  6. include <FastLED.h>
  1. define NUM_LEDS 60 // led灯数量
  2. define DATA_PIN 6 // 数据传输引脚(Digital 6)
  3. define BRIGHTNESS 25 ////設定led亮度

String s; CRGB leds[NUM_LEDS];

class MyParser : public HIDReportParser {

 public:
   MyParser();
   void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
 protected:
   uint8_t KeyToAscii(bool upper, uint8_t mod, uint8_t key);
   virtual void OnKeyScanned(bool upper, uint8_t mod, uint8_t key);
   virtual void OnScanFinished();

};

MyParser::MyParser() {}

void MyParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {

 // If error or empty, return
 if (buf[2] == 1 || buf[2] == 0) return;
 for (uint8_t i = 7; i >= 2; i--) {
   // If empty, skip
   if (buf[i] == 0) continue;
   // If enter signal emitted, scan finished
   if (buf[i] == UHS_HID_BOOT_KEY_ENTER) {
     OnScanFinished();
   }
   // If not, continue normally
   else {
     // If bit position not in 2, it's uppercase words
     OnKeyScanned(i > 2, buf, buf[i]);
   }
   return;
 }

}

uint8_t MyParser::KeyToAscii(bool upper, uint8_t mod, uint8_t key) {

 // Letters
 if (VALUE_WITHIN(key, 0x04, 0x1d)) {
   if (upper) return (key - 4 + 'A');
   else return (key - 4 + 'a');
 }
 // Numbers
 else if (VALUE_WITHIN(key, 0x1e, 0x27)) {
   return ((key == UHS_HID_BOOT_KEY_ZERO) ? '0' : key - 0x1e + '1');
 }
 if(key== 55 ) { 
   return 46; 
   }else return 0;
 

}

void MyParser::OnKeyScanned(bool upper, uint8_t mod, uint8_t key) {

 uint8_t ascii = KeyToAscii(upper, mod, key);
// Serial.print((char)ascii);  
delay(100);
 s= s + ((char)ascii) + "";

//s=s + ascii; }

void MyParser::OnScanFinished() {

 float a=s.toFloat();
 a=a*60;
 int uplimit=ceil(a)-1;
 int downlimit=floor(a)-1;
 Serial.println(uplimit);  
 FastLED.clear();
 if(uplimit==downlimit){
   leds[uplimit] = CRGB::White;
 }else{
   leds[uplimit] = CRGB::Red;  // 设置第1个led灯的颜色Red
   leds[downlimit] = CRGB::Green;  // 设置第2个led灯的颜色Red
 }
 FastLED.show();
 s="";

}

USB Usb; USBHub Hub(&Usb); HIDUniversal Hid(&Usb); MyParser Parser;

void setup() {

 FastLED.addLeds<NEOPIXEL, DATA_PIN>(leds, NUM_LEDS); // 初始化所有LED灯
 FastLED.setBrightness(  BRIGHTNESS );
 Serial.begin( 115200 );
 Serial.println("Start");
 if (Usb.Init() == -1) {
   Serial.println("OSC did not start.");
 }
 delay( 200 );
 Hid.SetReportParser(0, &Parser);

}

void loop() {

 Usb.Task();

} </c>