2016年3月14日 星期一

簡易示波器 part 2



arduino 部分:  重點就是  先  serial read...再serial write

===================================================================

int  firstSensor  =0;

int ledState = LOW;
const int ledPin =  2;      // D2 the number of the LED pin

unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 500;
unsigned char inByte;

void setup()
{
 
  pinMode(ledPin, OUTPUT);  
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

}

unsigned char cnt=0;

void loop()
{
  char buf[12];

 if (Serial.available() > 0)
 {
    // get incoming byte:
    inByte = Serial.read();
    // read first analog input, divide by 4 to make the range 0-255:
    firstSensor = analogRead(A0);
   
  Serial.println(firstSensor);
 
 }

 //Serial.println("Hello, World!");
  unsigned long currentMillis = millis();

  if(currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;  

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;

    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
   
  }

}

======================================================

raspberry pi  部分... 先送serial write.... 再serial read....然後把讀到的資料存

進去ring buffer...然後畫曲線畫出來

======================================================

# -*- coding: utf-8 -*-
# Author: Massimo Menichinelli
# Homepage: http://www.openp2pdesign.org
# License: MIT
#

import wx
import serial


# A new custom class that extends the wx.Frame
class MyFrame(wx.Frame):

    def __init__(self, parent, title):
        super(MyFrame, self).__init__(parent, title=title,
            size=(300, 300))

        # Attach the paint event to the frame
        self.Bind(wx.EVT_PAINT, self.OnPaint)

        # Create a timer for redrawing the frame every 100 milliseconds
        self.Timer = wx.Timer(self)
        self.Timer.Start(100)
        self.Bind(wx.EVT_TIMER, self.OnPaint)

        # Show the frame
        self.Centre()
        self.Show()

        self.index = 0

    def OnPaint(self, event=None):
        # Create the paint surface
        dc = wx.PaintDC(self)

        # Refresh the display
        self.Refresh()

        # Get data from serial port
        arduino.write('0')
        value = arduino.readline()
        try:
            value = int(value)/4
        except:
            value = 0

        #print "value : " + str(value)

     
        for i in range(0,data_size-1,1):
             data_array[i] = data_array[i+1]
        data_array[data_size-1] = (255 - value)


        # Draw the serial data

        # Set up colors:
        thickness = 3
        border_color = "#990000"
        border_color2 = "#999900"
        fill_color = "#FF944D"
        fill_color2 = "#00944D"
        dc.SetPen(wx.Pen(border_color, thickness))
        dc.SetBrush(wx.Brush(fill_color))

        # Draw a line
        #dc.DrawLine(50, 40, 50+value, 40)
        for i in range(0,data_size-1):
            dc.DrawLine(i, data_array[i % data_size], i+1, data_array[(i+1) % data_size])
        # Draw a rectangle
        #dc.DrawRectangle(50,50,value,50)

        #dc.SetPen(wx.Pen(border_color2, thickness))
        #dc.SetBrush(wx.Brush(fill_color2))
        #for i in range(0,data_size-1):
            #dc.DrawLine(i, 255-data_array[i % data_size], i+1, 255-data_array[(i+1) % data_size])



# Main program
if __name__ == '__main__':
    # Connect to serial port first
    try:
        arduino = serial.Serial('/dev/ttyUSB0', 9600)
    except:
        print "Failed to connect"
        exit()
    data_array = []
    data_size = 200

    for i in range(0,data_size):
       data_array.append(255)

    # Create and launch the wx interface
    app = wx.App()
    MyFrame(None, 'Serial data test')
    app.MainLoop()

    # Close the serial connection
    arduino.close()



Reference : http://fabacademy.org/archives/2015/doc/wxpython.html

沒有留言:

張貼留言