int main(int argc, char *argv[]) { int ret = 0; setup_handlers(); if (ws2811_init(&ledstring)) { return -1; } while (1) { matrix_raise(); matrix_bottom(); matrix_render(); if (ws2811_render(&ledstring)) { ret = -1; break; } // 15 frames /sec usleep(1000000 / 15); } ws2811_fini(&ledstring); return ret; }
// Constructor LedDeviceWS281x::LedDeviceWS281x(const int gpio, const int leds, const uint32_t freq, const int dmanum, const int pwmchannel) { initialized = false; led_string.freq = freq; led_string.dmanum = dmanum; if (pwmchannel != 0 && pwmchannel != 1) { std::cout << "WS281x: invalid PWM channel; must be 0 or 1." << std::endl; throw -1; } chan = pwmchannel; led_string.channel[chan].gpionum = gpio; led_string.channel[chan].invert = 0; led_string.channel[chan].count = leds; led_string.channel[chan].brightness = 255; led_string.channel[chan].strip_type = WS2811_STRIP_RGB; led_string.channel[!chan].gpionum = 0; led_string.channel[!chan].invert = 0; led_string.channel[!chan].count = 0; led_string.channel[!chan].brightness = 0; led_string.channel[!chan].strip_type = WS2811_STRIP_RGB; if (ws2811_init(&led_string) < 0) { std::cout << "Unable to initialize ws281x library." << std::endl; throw -1; } initialized = true; }
int main(int argc, char* argv[]){ if(argc < 2){ printf("Not enough input arguments!\n"); printf("Usage: %s filename\n", argv[0]); return -1; } loadImage(&image, argv[1]); cropToSquare(&image); uint16_t numSlices = (REFRESH_RATE * 60) / ROTATION_RATE; ws2811_init(&ledstring); setup_handlers(); unsigned int updatesCompleted = 0; double th; #ifdef PERFCOUNT struct timespec start, end; printf("Update rate:\n"); if(clock_gettime(CLOCK_REALTIME, &start)) { EPRINT("Error: Couldnt get clock time!"); } #endif while(running) { th = (updatesCompleted % numSlices) * 2 * M_PI / numSlices; generateSlice(ledstring.channel[0].leds, th, image.height); if (ws2811_render(&ledstring)) { EPRINT("Error: Rendering string didnt return 0!\n"); break; } updatesCompleted += 1; #ifdef PERFCOUNT if(!(updatesCompleted % 300)) { if(clock_gettime(CLOCK_REALTIME, &end)) { EPRINT("Error: Couldnt get clock time!"); } double freq = 300000.0/((end.tv_sec - start.tv_sec)*1000 + (end.tv_nsec - start.tv_nsec)/1000000); printf("\t%.2f Hz\r", freq); fflush(stdout); start = end; } #endif } // Clear the led strip before exiting for(int i = 0; i < LED_COUNT; i++) { ledstring.channel[0].leds[i] = 0; } ws2811_render(&ledstring); ws2811_fini(&ledstring); }
int main(void) { halInit(); chSysInit(); chThdSleepMilliseconds(1000); hw_init_gpio(); LED_RED_OFF(); LED_GREEN_OFF(); conf_general_init(); ledpwm_init(); mc_configuration mcconf; conf_general_read_mc_configuration(&mcconf); mc_interface_init(&mcconf); commands_init(); comm_usb_init(); app_configuration appconf; conf_general_read_app_configuration(&appconf); app_init(&appconf); timeout_init(); timeout_configure(appconf.timeout_msec, appconf.timeout_brake_current); #if CAN_ENABLE comm_can_init(); #endif #if WS2811_ENABLE ws2811_init(); led_external_init(); #endif #if ENCODER_ENABLE encoder_init(); #endif #if SERVO_OUT_ENABLE #if SERVO_OUT_SIMPLE servo_simple_init(); #else servo_init(); #endif #endif // Threads chThdCreateStatic(periodic_thread_wa, sizeof(periodic_thread_wa), NORMALPRIO, periodic_thread, NULL); chThdCreateStatic(sample_send_thread_wa, sizeof(sample_send_thread_wa), NORMALPRIO - 1, sample_send_thread, NULL); chThdCreateStatic(timer_thread_wa, sizeof(timer_thread_wa), NORMALPRIO, timer_thread, NULL); for(;;) { chThdSleepMilliseconds(5000); } }
int main(void) { LEDS_init(); LEDon(LED_BLUE); ws2811_init(); clearAllLED(); //xTaskCreate(animation_task, (signed char*)"animation", 16, 0, 1, 0); //vTaskStartScheduler(); animation_task((void *) 0); return -1; }
int main(void) { SystemInit(); ws2811_init(); rtc_init(); RTC_TimeTypeDef time; color defaultColor = BLACK; color secondColor = BLUE; color minuteColor = GREEN; color hourColor = RED; while(1) { setAllLED_Color(&defaultColor); rtc_getTime(&time); uint8_t lastSecond= time.RTC_Seconds; uint8_t secondLed = time.RTC_Seconds & 10 ; addColorToLED(secondLed, &secondColor); // uint8_t minuteLed = time.RTC_Minutes; // addColorToLED(minuteLed, &minuteColor); // // uint8_t hourLed = time.RTC_Hours*5; // addColorToLED(hourLed, &hourColor); startFadingLED(secondLed,1,100, 64); while(time.RTC_Seconds == lastSecond){ rtc_getTime(&time); } // for(uint8_t i = 0; i<=100; i++) // { // setAllLED_Color(&defaultColor); // //setBrightnessOfALL(i); // updateLED(); // delay_ms(10); // } } }
bool LedDeviceWS281x::init(const QJsonObject &deviceConfig) { LedDevice::init(deviceConfig); QString whiteAlgorithm = deviceConfig["whiteAlgorithm"].toString("white_off"); _whiteAlgorithm = RGBW::stringToWhiteAlgorithm(whiteAlgorithm); Debug( _log, "whiteAlgorithm : %s", QSTRING_CSTR(whiteAlgorithm)); Debug( _log, "rgbw : %d", deviceConfig["rgbw"].toBool(false) ); if (_whiteAlgorithm == RGBW::INVALID) { Error(_log, "unknown whiteAlgorithm %s", QSTRING_CSTR(whiteAlgorithm)); return false; } _channel = deviceConfig["pwmchannel"].toInt(0); if (_channel != 0 && _channel != 1) { throw std::runtime_error("WS281x: invalid PWM channel; must be 0 or 1."); } _led_string.freq = deviceConfig["freq"].toInt(800000ul); _led_string.dmanum = deviceConfig["dma"].toInt(5); _led_string.channel[_channel].gpionum = deviceConfig["gpio"].toInt(18); _led_string.channel[_channel].count = deviceConfig["leds"].toInt(256); _led_string.channel[_channel].invert = deviceConfig["invert"].toInt(0); _led_string.channel[_channel].strip_type = (deviceConfig["rgbw"].toBool(false) ? SK6812_STRIP_GRBW : WS2811_STRIP_RGB); _led_string.channel[_channel].brightness = 255; _led_string.channel[!_channel].gpionum = 0; _led_string.channel[!_channel].invert = _led_string.channel[_channel].invert; _led_string.channel[!_channel].count = 0; _led_string.channel[!_channel].brightness = 0; _led_string.channel[!_channel].strip_type = WS2811_STRIP_RGB; Debug( _log, "ws281x strip type : %d", _led_string.channel[_channel].strip_type ); if (ws2811_init(&_led_string) < 0) { throw std::runtime_error("Unable to initialize ws281x library."); } return true; }
int main(int argc, char **argv) { int shader = 0; if (argc >= 3){ if(sscanf (argv[2], "%i", &anim_delay)!=1){ printf ("Error, delay must be an integer \n"); return 0; } } int newbrightness = 0; if (argc >= 4){ if(sscanf (argv[3], "%i", &newbrightness)!=1){ printf ("Error, brightness must be an integer \n"); return 0; }else{ setBrightness(newbrightness); } } if (argc == 2){ if(sscanf (argv[1], "%i", &newbrightness)==1){ setBrightness(newbrightness); shader = 1; } } int i; for (i = 0; i < 64; i++) { struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_handler = unicorn_exit; sigaction(i, &sa, NULL); } setvbuf(stdout, NULL, _IONBF, 0); if(ws2811_init(&ledstring)) { return -1; } clearLEDBuffer(); if(argc < 2){ shader = 1; } if(shader){ run_shader(); }else{ read_png_file(argv[1]); while(1){ process_file(); if (height/8 == 1){ break; } } } unicorn_exit(0); return 0; }
int matrix_init() { return ws2811_init(&leds); }
int RPIWS281xOutput::Init(char *configStr) { LogDebug(VB_CHANNELOUT, "RPIWS281xOutput::Init('%s')\n", configStr); std::vector<std::string> configElems = split(configStr, ';'); for (int i = 0; i < configElems.size(); i++) { std::vector<std::string> elem = split(configElems[i], '='); if (elem.size() < 2) continue; if (elem[0] == "string1GPIO") { m_string1GPIO = atoi(elem[1].c_str()); } else if (elem[0] == "string1Pixels") { m_string1Pixels = atoi(elem[1].c_str()); if (m_string1Pixels) m_string1GPIO = 18; } else if (elem[0] == "string1ColorOrder") { m_string1ColorOrder = elem[1].c_str(); } else if (elem[0] == "string2GPIO") { m_string2GPIO = atoi(elem[1].c_str()); } else if (elem[0] == "string2Pixels") { m_string2Pixels = atoi(elem[1].c_str()); if (m_string2Pixels) m_string2GPIO = 19; } else if (elem[0] == "string2ColorOrder") { m_string2ColorOrder = elem[1].c_str(); } } if (!m_string1GPIO && !m_string2GPIO) { LogErr(VB_CHANNELOUT, "Invalid Config Str: %s\n", configStr); return 0; } if (m_string1GPIO && !m_string1Pixels) { LogErr(VB_CHANNELOUT, "Invalid String 1 Pixel Count: %s\n", configStr); return 0; } if (m_string2GPIO && !m_string2Pixels) { LogErr(VB_CHANNELOUT, "Invalid String 2 Pixel Count: %s\n", configStr); return 0; } ledstring.freq = 800000; // Hard code this for now ledstring.dmanum = 5; ledstring.channel[0].gpionum = m_string1GPIO; ledstring.channel[0].count = m_string1Pixels; if (m_string1ColorOrder == "RGB") { ledstring.channel[0].strip_type = WS2811_STRIP_RGB; } else if (m_string1ColorOrder == "RBG") { ledstring.channel[0].strip_type = WS2811_STRIP_RBG; } else if (m_string1ColorOrder == "GRB") { ledstring.channel[0].strip_type = WS2811_STRIP_GRB; } else if (m_string1ColorOrder == "GBR") { ledstring.channel[0].strip_type = WS2811_STRIP_GBR; } else if (m_string1ColorOrder == "BRG") { ledstring.channel[0].strip_type = WS2811_STRIP_BRG; } else if (m_string1ColorOrder == "BGR") { ledstring.channel[0].strip_type = WS2811_STRIP_BGR; } ledstring.channel[0].invert = 0; ledstring.channel[0].brightness = 255; ledstring.channel[1].gpionum = m_string2GPIO; ledstring.channel[1].count = m_string2Pixels; if (m_string2ColorOrder == "RGB") { ledstring.channel[1].strip_type = WS2811_STRIP_RGB; } else if (m_string2ColorOrder == "RBG") { ledstring.channel[1].strip_type = WS2811_STRIP_RBG; } else if (m_string2ColorOrder == "GRB") { ledstring.channel[1].strip_type = WS2811_STRIP_GRB; } else if (m_string2ColorOrder == "GBR") { ledstring.channel[1].strip_type = WS2811_STRIP_GBR; } else if (m_string2ColorOrder == "BRG") { ledstring.channel[1].strip_type = WS2811_STRIP_BRG; } else if (m_string2ColorOrder == "BGR") { ledstring.channel[1].strip_type = WS2811_STRIP_BGR; } ledstring.channel[1].invert = 0; ledstring.channel[1].brightness = 255; SetupCtrlCHandler(); if (ws2811_init(&ledstring)) { LogErr(VB_CHANNELOUT, "ws2811_init() failed\n"); return 0; } return ChannelOutputBase::Init(configStr); }
/* call-seq: * PixelPi::Leds.new( length, gpio, options = {} ) * * Create a new PixelPi::Leds instance that can be used to control a string of * NeoPixels from a RaspberryPi. The length of the pixel string must be given as * well as the GPIO pin number used to control the string. The remaining options * have sensible defaults. * * length - the nubmer of leds in the string * gpio - the GPIO pin number * options - Hash of arguments * :dma - DMA channel defaults to 5 * :frequency - output frequency defaults to 800,000 Hz * :invert - defaults to `false` * :brightness - defaults to 255 */ static VALUE pp_leds_initialize( int argc, VALUE* argv, VALUE self ) { ws2811_t *ledstring; VALUE length, gpio, opts, tmp; int resp; if (TYPE(self) != T_DATA || RDATA(self)->dfree != (RUBY_DATA_FUNC) pp_leds_free) { rb_raise( rb_eTypeError, "expecting a PixelPi::Leds object" ); } Data_Get_Struct( self, ws2811_t, ledstring ); /* parse out the length, gpio, and optional arguments if given */ rb_scan_args( argc, argv, "21", &length, &gpio, &opts ); /* get the number of pixels */ if (TYPE(length) == T_FIXNUM) { ledstring->channel[0].count = FIX2INT(length); if (ledstring->channel[0].count < 0) { rb_raise( rb_eArgError, "length cannot be negative: %d", ledstring->channel[0].count ); } } else { rb_raise( rb_eTypeError, "length must be a number: %s", rb_obj_classname(length) ); } /* get the GPIO number */ if (TYPE(gpio) == T_FIXNUM) { ledstring->channel[0].gpionum = FIX2INT(gpio); if (ledstring->channel[0].gpionum < 0) { rb_raise( rb_eArgError, "GPIO cannot be negative: %d", ledstring->channel[0].gpionum ); } } else { rb_raise( rb_eTypeError, "GPIO must be a number: %s", rb_obj_classname(gpio) ); } if (!NIL_P(opts)) { Check_Type( opts, T_HASH ); /* get the DMA channel */ tmp = rb_hash_lookup( opts, sym_dma ); if (!NIL_P(tmp)) { if (TYPE(tmp) == T_FIXNUM) { ledstring->dmanum = FIX2INT(tmp); if (ledstring->dmanum < 0) { rb_raise( rb_eArgError, "DMA channel cannot be negative: %d", ledstring->dmanum ); } } else { rb_raise( rb_eTypeError, "DMA channel must be a number: %s", rb_obj_classname(tmp) ); } } /* get the frequency */ tmp = rb_hash_lookup( opts, sym_frequency ); if (!NIL_P(tmp)) { if (TYPE(tmp) == T_FIXNUM) { ledstring->freq = FIX2UINT(tmp); } else { rb_raise( rb_eTypeError, "frequency must be a number: %s", rb_obj_classname(tmp) ); } } /* get the brightness */ tmp = rb_hash_lookup( opts, sym_brightness ); if (!NIL_P(tmp)) { if (TYPE(tmp) == T_FIXNUM) { ledstring->channel[0].brightness = (FIX2UINT(tmp) & 0xff); if (ledstring->channel[0].brightness < 0) { rb_raise( rb_eArgError, "brightness cannot be negative: %d", ledstring->channel[0].brightness ); } } else { rb_raise( rb_eTypeError, "brightness must be a number: %s", rb_obj_classname(tmp) ); } } /* get the invert flag */ tmp = rb_hash_lookup( opts, sym_invert ); if (!NIL_P(tmp)) { if (RTEST(tmp)) ledstring->channel[0].invert = 1; else ledstring->channel[0].invert = 0; } } /* initialize the DMA and PWM cycle */ resp = ws2811_init( ledstring ); if (resp < 0) { rb_raise( ePixelPiError, "Leds could not be initialized: %d", resp ); } return self; }
//initializes the ws2811 driver code //setup freq=400,dma=5,channels=1,channel_1_gpio=18,channel_1_invert=0,channel_1_count=250,channel_1_brightness=255,channel_2_... void setup_ledstring(char * args){ char key[MAX_KEY_LEN], value[MAX_VAL_LEN]; int key_index, value_index; if (ledstring.device!=NULL) ws2811_fini(&ledstring); ledstring.device = NULL; ledstring.freq = WS2811_TARGET_FREQ; ledstring.dmanum = 5; ledstring.channel[0].gpionum = 18; ledstring.channel[0].invert = 0; ledstring.channel[0].count = 1; ledstring.channel[0].brightness = 255; ledstring.channel[1].gpionum = 0; ledstring.channel[1].invert = 0; ledstring.channel[1].count = 0; ledstring.channel[1].brightness = 0; //char * key = strtok(args, " ="); //char c = args; if (debug) printf("Setup\n"); if (args!=NULL){ while (*args){ //first we get the key args = read_key(args, key,MAX_KEY_LEN); if (*args!=0) *args++; //now read the value part args = read_val(args, value,MAX_VAL_LEN); if (*args!=0) *args++; if (debug) printf("Setting %s=%s\n", key, value); if (strlen(key)>0 && strlen(value) > 0){ if (strcmp(key,"freq")==0){ ledstring.freq = atoi(value); }else if (strcmp(key, "dma")==0){ ledstring.dmanum = atoi(value); }else if (strcmp(key, "channels")==0){ if (value[0]=='1'){ ledstring.channel[1].gpionum=0; ledstring.channel[1].invert = 0; ledstring.channel[1].count = 0; ledstring.channel[1].brightness = 0; }else{ ledstring.channel[1].gpionum=0; ledstring.channel[1].invert = 0; ledstring.channel[1].count = 1; ledstring.channel[1].brightness = 255; } }else if (strcmp(key, "channel_1_gpio")==0){ ledstring.channel[0].gpionum=atoi(value); }else if (strcmp(key, "channel_1_invert")==0){ ledstring.channel[0].invert=atoi(value); }else if (strcmp(key, "channel_1_count")==0){ ledstring.channel[0].count=atoi(value); }else if (strcmp(key, "channel_1_brightness")==0){ ledstring.channel[0].brightness=atoi(value); }else if (strcmp(key, "channel_2_gpio")==0){ ledstring.channel[1].gpionum=atoi(value); }else if (strcmp(key, "channel_2_invert")==0){ ledstring.channel[1].invert=atoi(value); }else if (strcmp(key, "channel_2_count")==0){ ledstring.channel[1].count=atoi(value); }else if (strcmp(key, "channel_2_brightness")==0){ ledstring.channel[1].brightness=atoi(value); } } //args++; } } int size = ledstring.channel[0].count; if (ledstring.channel[1].count> size) size= ledstring.channel[1].count; malloc_command_line(DEFAULT_COMMAND_LINE_SIZE + size * 6); //allocate memory for full render data if (ws2811_init(&ledstring)){ perror("Initialization failed.\n"); return; } }