packaging: also support rpmbuild from Tizen:2.3
[contrib/mraa.git] / examples / spi_max7219.c
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  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #include "mraa.h"
27 #include <unistd.h>
28 #include <stdint.h>
29
30 int
31 main(int argc, char** argv)
32 {
33     //! [Interesting]
34     mraa_spi_context spi;
35     spi = mraa_spi_init(1);
36     if (spi == NULL) {
37         printf("Initialization of spi failed, check syslog for details, exit...\n");
38         exit(1);
39     }
40
41     printf("SPI initialised successfully\n");
42
43     mraa_spi_frequency(spi, 400000);
44     mraa_spi_lsbmode(spi, 0);
45
46     // The MAX7219/21 Chip needs the data in word size
47     if (mraa_spi_bit_per_word(spi, 16) != MRAA_SUCCESS) {
48         printf("Could not set SPI Device to 16Bit mode, exit...\n");
49         exit(1);
50     };
51
52     mraa_spi_write_word(spi, 0x0900); // Do not decode bits
53     mraa_spi_write_word(spi, 0x0a05); // Brightness of LEDs
54     mraa_spi_write_word(spi, 0x0b07); // Show all Scan Lines
55     mraa_spi_write_word(spi, 0x0c01); // Display on
56     mraa_spi_write_word(spi, 0x0f00); // Testmode off
57
58     // Display Pattern on the display
59     uint16_t dataAA55[] = { 0x01aa, 0x0255, 0x03aa, 0x0455, 0x05aa, 0x0655, 0x07aa, 0x0855 };
60     mraa_spi_write_buf_word(spi, dataAA55, 16);
61
62     sleep(2);
63
64     // Display inverted Pattern
65     uint16_t data55AA[] = { 0x0155, 0x02aa, 0x0355, 0x04aa, 0x0555, 0x06aa, 0x0755, 0x08aa };
66     mraa_spi_write_buf_word(spi, data55AA, 16);
67
68     sleep(2);
69
70     // Clear the display
71     uint16_t data[] = { 0x0100, 0x0200, 0x0300, 0x0400, 0x0500, 0x0600, 0x0700, 0x0800 };
72     mraa_spi_write_buf_word(spi, data, 16);
73
74     int i;
75     int j;
76     // cycle through all LED's
77     for (i = 1; i <= 8; i++) {
78         for (j = 0; j < 8; j++) {
79             mraa_spi_write_word(spi, (i << 8) + (1 << j));
80             sleep(1);
81         }
82         mraa_spi_write_word(spi, i << 8);
83     }
84
85     mraa_spi_stop(spi);
86
87     //! [Interesting]
88 }