|
楼主 |
发表于 2019-1-16 14:36:41
|
显示全部楼层
把以上範例改以VUSB代替串口:
Arduino程序: - // Oscilloscope
- // Input defined by ANALOG_IN
- // SIG_OUT true puts a 2kHz wave on DIGITAL_OUT for testing.
- #include <HIDSerial.h>
- HIDSerial serial;
- #define ANALOG_IN 0
- #define DIGITAL_OUT 13
- bool SIG_OUT = true;
- void setup() {
- analogReadResolution(8); //use 1-byte A/D
- serial.begin(); //HID
- //Serial.begin(9600);
- //Serial.begin(115200);
- // Generate a signal to examine (testing)
- if (SIG_OUT) {
- pinMode(DIGITAL_OUT, OUTPUT);
- // initialize timer1
- noInterrupts(); // disable all interrupts
- TCCR1A = 0;
- TCCR1B = 0;
- TCNT1 = 0;
- OCR1A = 31250; // compare match register 16MHz/256/2Hz
- TCCR1B |= (1 << WGM12); // CTC mode
- TCCR1B |= (1 << CS12); // 256 prescaler
- TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
- interrupts(); // enable all interrupts
- }
- }
- // Interrupt based
- ISR(TIMER1_COMPA_vect) {
- digitalWrite(DIGITAL_OUT, digitalRead(DIGITAL_OUT) ^ 1); // Toggle
- }
- //byte val = 0;
- void loop() {
- byte val = analogRead(ANALOG_IN);
- /*
- Serial.write( 0xff );
- Serial.write( (val >> 8) & 0xff );
- Serial.write( val & 0xff );
- */
- if (val==0) val=1; //use 1-byte only and reserve 0 for special use
- serial.write( val );
- serial.poll();
- }
复制代码
配合使用的Processing程序: - /*
- * Oscilloscope
- * Gives a visual rendering of analog pin in realtime.
- *
- * ---------------- IMPROVEMENTS ------------------
- * Updates by John Porter, 2/7/2014
- * Added ability to move waveform left or right.
- * Added gridlines (bounds and minor).
- * Added ability to pause/resume.
- * Added ability to measure time.
- * General usability improvements.
- *
- * --------------- ORIGINAL PROJECT ---------------
- * This project is part of Accrochages
- * See http://accrochages.drone.ws
- * (c) 2008 Sofian Audry ([email protected])
- * ------------------------------------------------
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- // * ------------------ HOT KEYS ------------------
- final char T_UP = 'w'; // Translate waveform up
- final char T_DOWN = 's'; // down
- final char T_LEFT = 'a'; // left
- final char T_RIGHT = 'd'; // right
- final char Z_IN = 'c'; // Horizontal zoom in
- final char Z_OUT = 'z'; // out
- final char S_IN = 'e'; // Vertical scale in
- final char S_OUT = 'q'; // out
- final char MGL_UP = 'r'; // Minor grid lines increase
- final char MGL_DOWN = 'f'; // decrease
- final char TOG_PAUSE = 'p'; // Toggle pause (unpause resets waveform)
- final char RESET_AXIS = ' '; // Reset axis settings
- final char MEAS_TIME = 'x'; // Adds and/or highlights vertical bars (time measurement)
- final char BAR_LEFT = ','; // Move highlighted vertical bar left (can also mouse click)
- final char BAR_RIGHT = '.'; // right
- // * ----------------------------------------------
- // * --------------- STARTING STATE ---------------
- float zoom = 1.0;
- float scale = 0.5;
- int centerV = 0;
- int centerH = 0;
- int gridLines = 0;
- int com_port = 1; // Index number in Serial.list
- // * ----------------------------------------------
- int val; // Data received from the serial port
- long valTime; // Time data was received
- int[] values;
- long[] times;
- float voltage;
- float measTime = 0;
- int timeMode = 0;
- int[] timeBars = {0, 0};
- PFont f;
- boolean pause;
- /*****************************************************
- Host software for HID-class USB serial communication.
- It allows connecting to a HID serial device (16c0:05df),
- sending (up to 32 bytes per packet) and receiving
- strings from the device. The code uses a slightly modified
- version of the g4p library for user interface, and the
- HIDAPI library for handling HID requests.
-
- The code was initially written by Chaniel Chadowitz
- and modified by Ray Wang (Rayshobby LLC). The code is published
- under Creative Commons Attribution-ShareAlike 3.0 license.
- *************************************************************/
- import g4p_controls.*;
- import com.codeminders.hidapi.HIDDeviceInfo;
- import com.codeminders.hidapi.HIDManager;
- import com.codeminders.hidapi.HIDDevice;
- import java.io.IOException;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.awt.Font;
- GButton connectButton;
- boolean paused;
- String serialText = new String();
- int VENDOR_ID = 0x16c0;
- int PRODUCT_ID = 0x05df;
- HIDDevice device = null;
- Boolean device_initialized = false;
- byte value1 = 0;
- int value = 0;
- // Setup
- void setup() {
- size(1280, 480);
- G4P.setGlobalColorScheme(5);
- connectButton = new GButton(this, 10, 15, 100, 20, "Connect");
- connectButton.setTextBold();
- device_initialized = false;
- values = new int[width];
- times = new long[width];
- timeBars[0] = width/3;
- timeBars[1] = 2*width/3;
- pause = false;
- smooth();
- f = createFont("Arial", 16, true);
- }
- // Get a y-value for the datapoint, varies based on axis settings
- int getY(int val) {
- return (int)(height/2 -(val-512+centerV)*scale / 1023.0f * (height - 1));
- }
- // Push the values in the data array
- void pushValue(int value) {
- for (int i=0; i<width-1; i++)
- values[i] = values[i+1];
- values[width-1] = value;
- }
- // Push the timestamps in the time array
- void pushTime(long time) {
- for (int i=0; i<width-1; i++)
- times[i] = times[i+1];
- times[width-1] = time;
- }
- // Draw waveform
- void drawLines() {
- int x0 = 0, x1 = 0, y0 = 0, y1 = 0;
- stroke(255, 255, 0);
- for (int i=0; i<width; i++) {
- x1 = round(width - ((width-i) * zoom) + centerH);
- y1 = getY(values[i]*4);
- if (i > 1)
- line(x0, y0, x1, y1);
- x0 = x1;
- y0 = y1;
- }
- }
- // Draw gridlines (bounds, minor)
- void drawGrid() {
- // Get scaled values for bounds
- int pFive = getY(1023);
- int zero = getY(0);
- // Draw voltage bounds
- stroke(255, 0, 0);
- line(0, pFive-1, width, pFive-1);
- line(0, zero+1, width, zero+1);
- // Add voltage bound text
- textFont(f, 10);
- fill(255, 0, 0);
- text("+5V", 5, pFive+12);
- text(" 0V", 5, zero-4);
- // Draw minor grid lines
- int gridVal = 0;
- stroke(75, 75, 75);
- for (int i = 0; i < gridLines; i++) {
- gridVal = getY(round((i+1.0)*(1023.0 / (gridLines+1.0))));
- line(0, gridVal, width, gridVal);
- }
- // Add minor grid line text
- if (gridLines > 0) {
- textFont(f, 16);
- fill(204, 102, 0);
- float scaleVal = truncate(5.0f / (gridLines+1), 3);
- text("Grid: " + scaleVal + "V", 1170, height-12);
- }
- // Print difference between vertical 'time' bars
- if (timeMode > 0) {
- textFont(f, 16);
- fill(204, 102, 0);
- int idx0 = round(width + (timeBars[0] - width - centerH)/zoom);
- int idx1 = round(width + (timeBars[1] - width - centerH)/zoom);
- // Ensure time bars are over a recorded portion of the waveform
- if (idx1 < 0 || idx0 < 0 || idx1 > (width-1) || idx0 > (width-1) || times[idx1] == 0 || times[idx0] == 0)
- text("Time: N/A", 30, height-12);
- else {
- float timeDiff = truncate((times[idx1] - times[idx0])/2000000.0, 2);
- text("Time: " + timeDiff + "ms", 30, height-12);
- }
- }
- }
- // Draw vertical 'time bars' (seperate from above for better layering)
- void drawVertLines() {
- stroke(75, 75, 75);
- if (timeMode == 1) {
- line(timeBars[1], 0, timeBars[1], height);
- stroke(100, 100, 255);
- line(timeBars[0], 0, timeBars[0], height);
- } else if (timeMode == 2) {
- line(timeBars[0], 0, timeBars[0], height);
- stroke(100, 255, 100);
- line(timeBars[1], 0, timeBars[1], height);
- }
- }
- // Truncate a floating point number
- float truncate(float x, int digits) {
- float temp = pow(10.0, digits);
- return round( x * temp ) / temp;
- }
- // When a key is pressed down or held...
- void keyPressed() {
- switch (key) {
- case T_UP:
- centerV += 10/scale;
- break; // Move waveform up
- case T_DOWN:
- centerV -= 10/scale;
- break; // Move waveform down
- case T_RIGHT:
- centerH += 10/scale;
- break; // Move waveform right
- case T_LEFT:
- centerH -= 10/scale;
- break; // Move waveform left
- case MGL_UP: // Increase minor grid lines
- if (gridLines < 49)
- gridLines += 1;
- break;
- case MGL_DOWN: // Decrease minor grid lines
- if (gridLines > 0)
- gridLines -= 1;
- break;
- case BAR_LEFT: // Move the time bar left (also mouse click)
- if (timeMode == 1 && timeBars[0] > 0)
- timeBars[0] -= 1;
- else if (timeMode == 2 && timeBars[1] > 0)
- timeBars[1] -= 1;
- break;
- case BAR_RIGHT: // Move the time bar right (also mouse click)
- if (timeMode == 1 && timeBars[0] < width-1)
- timeBars[0] += 1;
- else if (timeMode == 2 && timeBars[1] < width-1)
- timeBars[1] += 1;
- break;
- }
- }
- // When a key is released...
- void keyReleased() {
- println(key+": "+(int)key);
- switch (key) {
- case Z_IN: // Zoom horizontal
- zoom *= 2.0f;
- if ( (int) (width / zoom) <= 1 )
- zoom /= 2.0f;
- break;
- case Z_OUT: // Zoom horizontal
- zoom /= 2.0f;
- if (zoom < 1.0f)
- zoom *= 2.0f;
- break;
- case S_IN:
- scale*=2;
- break; // Scale vertical
- case S_OUT:
- scale /= 2;
- break; // Scale vertical
- case RESET_AXIS: // Reset all scaling
- centerV = 0;
- centerH = 0;
- scale = 0.5;
- zoom = 1;
- gridLines = 0;
- break;
- case MEAS_TIME:
- timeMode = (timeMode + 1) % 3;
- break; // Change the vertical bars (off, left bar, right bar)
- case TOG_PAUSE: // Toggle waveform pausing
- if (pause) {
- centerH = 0;
- for (int i=0; i<width; i++) {
- values[i] = 0; // Clear data on resume
- times[i] = 0;
- }
- }
- pause = !pause;
- }
- }
- // Use mouse clicks to quickly move vertical bars (if highlighted)
- void mousePressed() {
- if (timeMode == 1)
- timeBars[0] = mouseX;
- else if (timeMode == 2)
- timeBars[1] = mouseX;
- }
- // Primary drawing function
- void draw() {
- background(0);
- if ( device != null && !paused ) {
- deviceRead();
- }
- drawGrid();
- // Get current voltage, time of reading
- val = value;
- // valTime = System.nanoTime();
- // If not paused
- if (!pause && val != -1) {
- // Push value/time onto array
- pushValue(val);
- pushTime(valTime);
- // Print current voltage reading
- textFont(f, 16);
- fill(204, 102, 0);
- voltage = truncate(3.3*val / 256, 2);
- text("Voltage: " + voltage + "V", 1170, 30);
- }
- drawLines();
- drawVertLines();
- }
- public void deviceInitialize() {
- if (!device_initialized) {
- device_initialized = true;
- com.codeminders.hidapi.ClassPathLibraryLoader
- .loadNativeHIDLibrary();
- }
- }
- public void deviceFindFirst() {
- deviceInitialize();
- HIDDeviceInfo[] infos = deviceFindAllDescriptors();
- if (infos.length > 0) {
- try {
- device = infos[0].open();
- }
- catch (Exception e) {
- device = null;
- }
- }
- }
- public HIDDeviceInfo[] deviceFindAllDescriptors() {
- deviceInitialize();
- List<HIDDeviceInfo> devlist = new ArrayList<HIDDeviceInfo>();
- try {
- HIDManager hidManager = HIDManager.getInstance();
- HIDDeviceInfo[] infos = hidManager.listDevices();
- for (HIDDeviceInfo info : infos) {
- if (info.getVendor_id() == VENDOR_ID
- && info.getProduct_id() == PRODUCT_ID) {
- devlist.add(info);
- }
- }
- }
- catch (Exception e) {
- }
- return devlist.toArray(new HIDDeviceInfo[devlist.size()]);
- }
- public void deviceRead() {
- try {
- device.disableBlocking();
- byte[] data = new byte[10];
- int read = device.read(data);
- if (read > 0) {
- for (int i=0; i<read; i++) {
- if (data[i]!=0) {
- value1 = data[i];
- //convert signed byte to unsigned byte
- if (value1 >=0)
- {
- value = value1;
- } else {
- value = int (value1 + 255);
- }
- }
- }
- }
- }
- catch(IOException ioe) {
- //ioe.printStackTrace();
- System.out.println("deviceRead error");
- }
- }
- public void handleButtonEvents(GButton button, GEvent event) {
- if ( event == GEvent.CLICKED ) {
- if ( button.equals(connectButton) ) {
- if (device!=null) {
- try {
- device.close();
- }
- catch (IOException ioe) {
- ioe.printStackTrace();
- }
- device = null;
- connectButton.setText("Connect");
- connectButton.setTextBold();
- value = 0;
- } else {
- deviceFindFirst();
- connectButton.setText("Disconnect");
- connectButton.setTextBold();
- }
- }
- }
- }
复制代码 |
|