1 /* 2 * Author: Alexander Komarov <alexander.komarov (at) intel.com> 3 * Copyright (c) 2014 Intel Corporation. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be 14 * included in all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 */ 24 25 #include <iostream> 26 #include <string> 27 #include <stdexcept> 28 #include <unistd.h> 29 #include <stdlib.h> 30 #include <functional> 31 #include <string.h> 32 #include "lol.h" 33 34 using namespace upm; 35 36 static mraa_gpio_context *m_Ctx; 37 static unsigned char *buffer; 38 39 static mraa_gpio_context c1, c2, c3, c4; 40 41 static int charlie_pairs [12][22] = { 42 {3,124, 4,110, 5,96, 6,82, 7,68, 8,54, 9,40, 10,26, 11,12, -1,-1, -1,-1}, 43 {3,122, 4,108, 5,94, 6,80, 7,66, 8,52, 9,38, 10,24, 11,10, -1,-1, -1,-1}, 44 {3,120, 4,106, 5,92, 6,78, 7,64, 8,50, 9,36, 10,22, 11,8, -1,-1, -1,-1}, 45 {0,125, 1,123, 2,121, 4,98, 5,84, 6,70, 7,56, 8,42, 9,28, 10,14, 11,0}, 46 {0,111, 1,109, 2,107, 3,112, 5,85, 6,71, 7,57, 8,43, 9,29, 10,15, 11,1}, 47 {0,97, 1,95, 2,93, 3,113, 4,99, 6,72, 7,58, 8,44, 9,30, 10,16, 11,2}, 48 {0,83, 1,81, 2,79, 3,114, 4,100,5,86, 7,59, 8,45, 9,31, 10,17, 11,3}, 49 {0,69, 1,67, 2,65, 3,115, 4,101,5,87, 6,73, 8,46, 9,32, 10,18, 11,4}, 50 {0,55, 1,53, 2,51, 3,116, 4,102,5,88, 6,74, 7,60, 9,33, 10,19, 11,5}, 51 {0,41, 1,39, 2,37, 3,117, 4,103,5,89, 6,75, 7,61, 8,47, 10,20, 11,6}, 52 {0,27, 1,25, 2,23, 3,118, 4,104,5,90, 6,76, 7,62, 8,48, 9,34, 11,7}, 53 {0,13, 1,11, 2,9, 3,119, 4,105,5,91, 6,77, 7,63, 8,49, 9,35, 10,21} 54 }; 55 56 void clear_gpio(int gpio) 57 { 58 mraa_gpio_mode(m_Ctx[gpio], MRAA_GPIO_HIZ); 59 mraa_gpio_dir(m_Ctx[gpio], MRAA_GPIO_IN); 60 } 61 62 void clear_prev_cycle(int cycle) 63 { 64 int i; 65 66 // What is prev cycle? 67 cycle--; 68 if (cycle == -1) 69 cycle = 11; 70 71 // Disable all "1"'s 72 for (i = 0; i < 11; i++) { 73 if (charlie_pairs[cycle][i*2] == -1) 74 break; 75 if(buffer[charlie_pairs[cycle][i*2 + 1]]) 76 clear_gpio(charlie_pairs[cycle][i*2]); 77 } 78 79 // Disable "0" 80 clear_gpio(cycle); 81 } 82 83 void set_strong_one(int gpio) 84 { 85 mraa_gpio_dir(m_Ctx[gpio], MRAA_GPIO_OUT); 86 mraa_gpio_mode(m_Ctx[gpio], MRAA_GPIO_STRONG); 87 mraa_gpio_write(m_Ctx[gpio], 1); 88 } 89 90 void set_strong_zero(int gpio) 91 { 92 mraa_gpio_dir(m_Ctx[gpio], MRAA_GPIO_OUT); 93 mraa_gpio_mode(m_Ctx[gpio], MRAA_GPIO_STRONG); 94 mraa_gpio_write(m_Ctx[gpio], 0); 95 } 96 97 98 static void clear_gpios() 99 { 100 int i; 101 for (i = 0; i < 12; i++) 102 clear_gpio(i); 103 } 104 105 void *do_draw(void *arg) 106 { 107 clear_gpios(); 108 109 while (1) { 110 int i, cur; 111 uint8_t cycle = 0; 112 // 12 Cycles of Matrix 113 for (cycle = 0; cycle < 12; cycle++) 114 { 115 if (cycle == 12) cycle = 0; 116 117 clear_prev_cycle(cycle); 118 // set strong/0 on current cycle line 119 set_strong_zero(cycle); 120 121 // draw ones from framebuffer 122 for (i = 0; i < 11; i++) { 123 cur = charlie_pairs[cycle][i*2]; 124 if (cur == -1) break; 125 126 if (buffer[charlie_pairs[cycle][i*2 + 1]]) { 127 set_strong_one(cur); 128 // printf("cycle %d %d %d %d\n", cycle, i, charlie_pairs[cycle][i*2 + 1], 129 // buffer[charlie_pairs[cycle][i*2 + 1]]); 130 } 131 } 132 } 133 134 } 135 } 136 137 LoL::LoL() { 138 int i = 0; 139 mraa_result_t error; 140 for (i = 0; i < 12; i++) 141 { 142 if ( !(m_LoLCtx[i] = mraa_gpio_init(i+2)) ) 143 { 144 throw std::invalid_argument(std::string(__FUNCTION__) + 145 ": mraa_gpio_init() failed, invalid pin?"); 146 return; 147 } 148 149 } 150 151 memset(framebuffer, 0, LOL_X*LOL_Y); 152 153 // I am optimistic and stupid - thread creation 154 // always works in my world 155 buffer = (unsigned char*)framebuffer; 156 m_Ctx = m_LoLCtx; 157 pthread_create (&drawer_thread, NULL, do_draw, NULL); 158 } 159 160 LoL::~LoL() { 161 int i = 0; 162 mraa_result_t error; 163 for (i = 0; i < 12; i++) 164 mraa_gpio_close(m_LoLCtx[i]); 165 } 166 167 unsigned char* LoL::getFramebuffer() { 168 return framebuffer; 169 } 170 171 unsigned char LoL::setPixel(int x, int y, unsigned char pixel) 172 { 173 if (x < 0 || y < 0 || x >= LOL_X || y >= LOL_Y) 174 return -1; 175 176 framebuffer[x + LOL_X*y] = (pixel == 0) ? 0 : 1; 177 return 0; 178 } 179 180 unsigned char LoL::getPixel(int x, int y) 181 { 182 if (x < 0 || y < 0 || x >= LOL_X || y >= LOL_Y) 183 return -1; 184 185 return (framebuffer[x + LOL_X*y] == 0) ? 0 : 1; 186 } 187 188