Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Imitator;
- import gnu.io.CommPortIdentifier;
- import gnu.io.PortInUseException;
- import gnu.io.SerialPort;
- import gnu.io.UnsupportedCommOperationException;
- import java.awt.AWTException;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.awt.Rectangle;
- import java.awt.Robot;
- import java.awt.Toolkit;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.Enumeration;
- public class ImitatorBrain2 {
- //Arduino communication config
- public static final int DATA_RATE = 9600;
- public static final int TIMEOUT = 2000;
- //delay between next color calculations in [ms]
- private static final long DELAY = 10;
- //leds number on strip
- public static final int LEDS_NUM = 30;
- //number of leds per section
- public static final int LEDS_PER_SECTION = 3;
- //we split strip to sections because of performance reasons
- public static final int SECTIONS = LEDS_NUM / LEDS_PER_SECTION;
- //automatic screen resolution
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- public int X_RES = (int)screenSize.getWidth();
- public int Y_RES = (int)screenSize.getHeight();
- //sections width and height
- public int SECT_WIDTH = X_RES / SECTIONS;
- public int SECT_HEIGHT = Y_RES;
- //for better performance we do not calculate every pixel,
- //but skip some of them
- public static final int SECT_SKIP = 10;
- // robot to read the data from the screen
- private Robot robot;
- // arduino communication
- private SerialPort serial;
- private OutputStream output;
- /**
- * init arduino communication
- */
- private void initSerial() {
- // find the port where teh arduino is connected
- CommPortIdentifier serialPortId = null;
- Enumeration enumComm = CommPortIdentifier.getPortIdentifiers();
- while (enumComm.hasMoreElements() && serialPortId == null) {
- serialPortId = (CommPortIdentifier) enumComm.nextElement();
- }
- try {
- serial = (SerialPort) serialPortId.open(this.getClass().getName(),
- TIMEOUT);
- serial.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8,
- SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
- } catch (PortInUseException | UnsupportedCommOperationException e) {
- e.printStackTrace();
- }
- }
- /**
- * init the robot
- */
- private void initRobot() {
- try {
- robot = new Robot();
- } catch (AWTException e) {
- e.printStackTrace();
- }
- }
- /**
- * init arduino output
- */
- private void initOutputStream() {
- try {
- output = serial.getOutputStream();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * read colors from the screen
- *
- * @return array with colors that will be send to arduino
- * @throws InterruptedException
- */
- //screen je cijeli ekran/rezolucija
- public volatile Color[] leds = new Color[SECTIONS]; // leds je POLJE od 10 radi SECTIONA tipa podatka Color - tu se spremaju svih 10 prosjecnih boja
- //Polje Color leds stavi kao volotile zajednicku varijablu i u jednoj dretvi od 1 do 5 sekcija a druga od 5 do 10
- //drugi volotile stavi BufferedImage screen
- public Color[] getColors(){
- BufferedImage screen = robot.createScreenCapture(new Rectangle(new Dimension(X_RES, Y_RES)));
- class dretva implements Runnable{
- public void run(){
- for (int led = 0; led < (SECTIONS/2); led++) { //probaj tu staviti brojac od 0 do 5 a kod druge dretve od 5 do 10
- BufferedImage sections = screen.getSubimage(led * SECT_WIDTH, 0, SECT_WIDTH, SECT_HEIGHT);
- Color sectionAvgColor = getAvgColor(sections);
- leds[led] = sectionAvgColor;
- }
- }
- }
- dretva posao = new dretva(); //napravi novu dretvu koja ce brojat od pola do kraja i stavi broj
- Thread thread = new Thread(posao);
- thread.start();
- for (int led = (SECTIONS/2); led < SECTIONS; led++) { //probaj tu staviti brojac od 0 do 5 a kod druge dretve od 5 do 10
- BufferedImage section = screen.getSubimage(led * SECT_WIDTH, 0, SECT_WIDTH, SECT_HEIGHT);
- Color sectionAvgColor = getAvgColor(section);
- leds[led] = sectionAvgColor;
- }
- try {
- thread.join();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return leds;
- }
- /**
- * calculate average color for section
- * @throws InterruptedException
- */
- private Color getAvgColor(BufferedImage imgSection){
- int width = imgSection.getWidth(); //sekcija sirine
- int height = imgSection.getHeight(); //sekcija visine
- int r = 0, g = 0, b = 0; //rgb boje
- int loops = 0;
- for (int x = 0; x < width; x += SECT_SKIP) { //brojac od nultog pixela do pola sirine
- for (int y = 0; y < height; y += SECT_SKIP) { //brojac do visine u istoj petlji
- int rgb = imgSection.getRGB(x, y);
- Color color = new Color(rgb);
- r += color.getRed();
- g += color.getGreen();
- b += color.getBlue();
- loops++;
- }
- }
- r = r / loops;
- g = g / loops;
- b = b / loops;
- return new Color(r, g, b);
- }
- /**
- * Send the data to Arduino
- */
- public void sendColors(Color[] leds) {
- try {
- output.write(0xff);
- for (int i = 0; i < SECTIONS; i++) {
- output.write(leds[i].getRed());
- output.write(leds[i].getGreen());
- output.write(leds[i].getBlue());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- //Main Loop
- private void loop(){
- while (true) {
- Color[] leds = getColors();
- sendColors(leds);
- try {
- Thread.sleep(DELAY);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args){
- ImitatorBrain2 imitator = new ImitatorBrain2();
- imitator.initRobot();
- imitator.initSerial();
- imitator.initOutputStream();
- imitator.loop();
- }
- /*
- public class dretva implements Runnable{
- ImitatorBrain2 IMITATOR = new ImitatorBrain2();
- public void run(){
- screen = robot.createScreenCapture(new Rectangle(new Dimension(X_RES, Y_RES)));
- for (int led = 0; led < (SECTIONS/2); led++) { //probaj tu staviti brojac od 0 do 5 a kod druge dretve od 5 do 10
- BufferedImage sections = screen.getSubimage(led * SECT_WIDTH, 0, SECT_WIDTH, SECT_HEIGHT);
- Color sectionAvgColor = getAvgColor(sections);
- leds[led] = sectionAvgColor;
- }
- }
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement