packaging: also support rpmbuild from Tizen:2.3
[contrib/mraa.git] / examples / java / SpiMAX7219.java
1 /*
2  * Author: Michael Ring <mail@michael-ring.org>
3  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
4  * Copyright (c) 2014 Intel Corporation.
5  * Author: Petre Eftime <petre.p.eftime@intel.com>
6  * Copyright (c) 2015 Intel Corporation.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 import java.nio.ByteBuffer;
29 import mraa.Result;
30 import mraa.Spi;
31
32 public class SpiMAX7219 {
33     static {
34         try {
35             System.loadLibrary("mraajava");
36         } catch (UnsatisfiedLinkError e) {
37             System.err.println(
38                     "Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" +
39                             e);
40             System.exit(1);
41         }
42     }
43     public static void main(String argv[]) throws InterruptedException {
44         //! [Interesting]
45         Spi spi = new Spi(1);
46
47         spi.frequency(400000);
48         spi.lsbmode(false);
49
50         if(spi.bitPerWord(16) != Result.SUCCESS) {
51             System.err.println("Could not set SPI Device to 16Bit mode, exit...");
52             System.exit(1);
53         }
54
55         spi.write_word(0x0900); //Do not decode bits
56         spi.write_word(0x0a05); // Brightness of LEDs
57         spi.write_word(0x0b07); // Show all Scan Lines
58         spi.write_word(0x0c01); // Display on
59         spi.write_word(0x0f00); // Testmode off
60
61         short dataAA55[] = { 0x01aa, 0x0255, 0x03aa, 0x0455, 0x05aa, 0x0655, 0x07aa, 0x0855 };
62         ByteBuffer buf = ByteBuffer.allocate(dataAA55.length * 2);
63         for (int i = 0; i < dataAA55.length; i++)
64             buf.putShort(dataAA55[i]);
65
66         spi.write(buf.array());
67         Thread.sleep(2000);
68
69         short data55AA[] = { 0x0155, 0x02aa, 0x0355, 0x04aa, 0x0555, 0x06aa, 0x0755, 0x08aa };
70         buf = ByteBuffer.allocate(data55AA.length * 2);
71         for (int i = 0; i < data55AA.length; i++)
72             buf.putShort(data55AA[i]);
73
74         spi.write(buf.array());
75         Thread.sleep(2000);
76
77         short data[] = { 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700, 0x0800 };
78         buf = ByteBuffer.allocate(data.length * 2);
79         for (int i = 0; i < data.length; i++)
80             buf.putShort(data[i]);
81
82         spi.write(buf.array());
83
84         for (int i = 1; i <= 8; i++) {
85             for (int j = 0; j < 8; j++) {
86                 spi.write_word((i << 8) + (1 << j));
87                 Thread.sleep(1000);
88             }
89             spi.write_word(i << 8);
90         }
91         //! [Interesting]
92     };
93 }