Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. package Imitator;
  2. import gnu.io.CommPortIdentifier;
  3. import gnu.io.PortInUseException;
  4. import gnu.io.SerialPort;
  5. import gnu.io.UnsupportedCommOperationException;
  6. import java.awt.AWTException;
  7. import java.awt.Color;
  8. import java.awt.Dimension;
  9. import java.awt.Rectangle;
  10. import java.awt.Robot;
  11. import java.awt.Toolkit;
  12. import java.awt.image.BufferedImage;
  13. import java.io.IOException;
  14. import java.io.OutputStream;
  15. import java.util.Enumeration;
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. public class ImitatorBrain2 {
  24.  
  25.  
  26. //Arduino communication config
  27. public static final int DATA_RATE = 9600;
  28. public static final int TIMEOUT = 2000;
  29. //delay between next color calculations in [ms]
  30. private static final long DELAY = 10;
  31. //leds number on strip
  32. public static final int LEDS_NUM = 30;
  33. //number of leds per section
  34. public static final int LEDS_PER_SECTION = 3;
  35. //we split strip to sections because of performance reasons
  36. public static final int SECTIONS = LEDS_NUM / LEDS_PER_SECTION;
  37. //automatic screen resolution
  38. Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  39. public int X_RES = (int)screenSize.getWidth();
  40. public int Y_RES = (int)screenSize.getHeight();
  41. //sections width and height
  42. public int SECT_WIDTH = X_RES / SECTIONS;
  43. public int SECT_HEIGHT = Y_RES;
  44. //for better performance we do not calculate every pixel,
  45. //but skip some of them
  46. public static final int SECT_SKIP = 10;
  47. // robot to read the data from the screen
  48. private Robot robot;
  49. // arduino communication
  50. private SerialPort serial;
  51. private OutputStream output;
  52.  
  53. /**
  54. * init arduino communication
  55. */
  56. private void initSerial() {
  57. // find the port where teh arduino is connected
  58. CommPortIdentifier serialPortId = null;
  59. Enumeration enumComm = CommPortIdentifier.getPortIdentifiers();
  60. while (enumComm.hasMoreElements() && serialPortId == null) {
  61. serialPortId = (CommPortIdentifier) enumComm.nextElement();
  62. }
  63. try {
  64. serial = (SerialPort) serialPortId.open(this.getClass().getName(),
  65. TIMEOUT);
  66. serial.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8,
  67. SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  68. } catch (PortInUseException | UnsupportedCommOperationException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72.  
  73. /**
  74. * init the robot
  75. */
  76. private void initRobot() {
  77. try {
  78. robot = new Robot();
  79. } catch (AWTException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83. /**
  84. * init arduino output
  85. */
  86. private void initOutputStream() {
  87. try {
  88. output = serial.getOutputStream();
  89. } catch (IOException e) {
  90. e.printStackTrace();
  91. }
  92. }
  93. /**
  94. * read colors from the screen
  95. *
  96. * @return array with colors that will be send to arduino
  97. * @throws InterruptedException
  98. */
  99.  
  100. //screen je cijeli ekran/rezolucija
  101. public volatile Color[] leds = new Color[SECTIONS]; // leds je POLJE od 10 radi SECTIONA tipa podatka Color - tu se spremaju svih 10 prosjecnih boja
  102.  
  103.  
  104. //Polje Color leds stavi kao volotile zajednicku varijablu i u jednoj dretvi od 1 do 5 sekcija a druga od 5 do 10
  105. //drugi volotile stavi BufferedImage screen
  106. public Color[] getColors(){
  107. BufferedImage screen = robot.createScreenCapture(new Rectangle(new Dimension(X_RES, Y_RES)));
  108.  
  109. class dretva implements Runnable{
  110.  
  111. public void run(){
  112. for (int led = 0; led < (SECTIONS/2); led++) { //probaj tu staviti brojac od 0 do 5 a kod druge dretve od 5 do 10
  113. BufferedImage sections = screen.getSubimage(led * SECT_WIDTH, 0, SECT_WIDTH, SECT_HEIGHT);
  114. Color sectionAvgColor = getAvgColor(sections);
  115. leds[led] = sectionAvgColor;
  116. }
  117. }
  118.  
  119. }
  120.  
  121. dretva posao = new dretva(); //napravi novu dretvu koja ce brojat od pola do kraja i stavi broj
  122. Thread thread = new Thread(posao);
  123. thread.start();
  124.  
  125. for (int led = (SECTIONS/2); led < SECTIONS; led++) { //probaj tu staviti brojac od 0 do 5 a kod druge dretve od 5 do 10
  126. BufferedImage section = screen.getSubimage(led * SECT_WIDTH, 0, SECT_WIDTH, SECT_HEIGHT);
  127. Color sectionAvgColor = getAvgColor(section);
  128. leds[led] = sectionAvgColor;
  129. }
  130. try {
  131. thread.join();
  132. } catch (InterruptedException e) {
  133. // TODO Auto-generated catch block
  134. e.printStackTrace();
  135. }
  136.  
  137.  
  138. return leds;
  139. }
  140.  
  141. /**
  142. * calculate average color for section
  143. * @throws InterruptedException
  144. */
  145.  
  146.  
  147.  
  148. private Color getAvgColor(BufferedImage imgSection){
  149. int width = imgSection.getWidth(); //sekcija sirine
  150. int height = imgSection.getHeight(); //sekcija visine
  151. int r = 0, g = 0, b = 0; //rgb boje
  152. int loops = 0;
  153. for (int x = 0; x < width; x += SECT_SKIP) { //brojac od nultog pixela do pola sirine
  154. for (int y = 0; y < height; y += SECT_SKIP) { //brojac do visine u istoj petlji
  155. int rgb = imgSection.getRGB(x, y);
  156. Color color = new Color(rgb);
  157. r += color.getRed();
  158. g += color.getGreen();
  159. b += color.getBlue();
  160. loops++;
  161. }
  162. }
  163. r = r / loops;
  164. g = g / loops;
  165. b = b / loops;
  166.  
  167. return new Color(r, g, b);
  168. }
  169.  
  170. /**
  171. * Send the data to Arduino
  172. */
  173. public void sendColors(Color[] leds) {
  174. try {
  175. output.write(0xff);
  176. for (int i = 0; i < SECTIONS; i++) {
  177. output.write(leds[i].getRed());
  178. output.write(leds[i].getGreen());
  179. output.write(leds[i].getBlue());
  180. }
  181. } catch (IOException e) {
  182. e.printStackTrace();
  183. }
  184. }
  185.  
  186.  
  187.  
  188. //Main Loop
  189. private void loop(){
  190. while (true) {
  191. Color[] leds = getColors();
  192. sendColors(leds);
  193. try {
  194. Thread.sleep(DELAY);
  195. } catch (InterruptedException e) {
  196. e.printStackTrace();
  197. }
  198. }
  199. }
  200.  
  201.  
  202. public static void main(String[] args){
  203. ImitatorBrain2 imitator = new ImitatorBrain2();
  204. imitator.initRobot();
  205. imitator.initSerial();
  206. imitator.initOutputStream();
  207. imitator.loop();
  208. }
  209.  
  210. /*
  211. public class dretva implements Runnable{
  212. ImitatorBrain2 IMITATOR = new ImitatorBrain2();
  213.  
  214. public void run(){
  215. screen = robot.createScreenCapture(new Rectangle(new Dimension(X_RES, Y_RES)));
  216. for (int led = 0; led < (SECTIONS/2); led++) { //probaj tu staviti brojac od 0 do 5 a kod druge dretve od 5 do 10
  217. BufferedImage sections = screen.getSubimage(led * SECT_WIDTH, 0, SECT_WIDTH, SECT_HEIGHT);
  218. Color sectionAvgColor = getAvgColor(sections);
  219. leds[led] = sectionAvgColor;
  220. }
  221. }
  222.  
  223. }
  224. */
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement