- 
                Notifications
    You must be signed in to change notification settings 
- Fork 959
Ws2812 example #574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
    
  
     Closed
                    Ws2812 example #574
Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -35,8 +35,8 @@ | |
| #define WS2812_PIN 2 | ||
| #endif | ||
|  | ||
| static inline void put_pixel(uint32_t pixel_grb) { | ||
| pio_sm_put_blocking(pio0, 0, pixel_grb << 8u); | ||
| static inline void put_pixel(PIO pio, uint sm, uint32_t pixel_grb) { | ||
| pio_sm_put_blocking(pio, sm, pixel_grb << 8u); | ||
| } | ||
|  | ||
| static inline uint32_t urgb_u32(uint8_t r, uint8_t g, uint8_t b) { | ||
|  | @@ -54,44 +54,44 @@ static inline uint32_t urgbw_u32(uint8_t r, uint8_t g, uint8_t b, uint8_t w) { | |
| (uint32_t) (b); | ||
| } | ||
|  | ||
| void pattern_snakes(uint len, uint t) { | ||
| void pattern_snakes(PIO pio, uint sm, uint len, uint t) { | ||
| for (uint i = 0; i < len; ++i) { | ||
| uint x = (i + (t >> 1)) % 64; | ||
| if (x < 10) | ||
| put_pixel(urgb_u32(0xff, 0, 0)); | ||
| put_pixel(pio, sm, urgb_u32(0xff, 0, 0)); | ||
| else if (x >= 15 && x < 25) | ||
| put_pixel(urgb_u32(0, 0xff, 0)); | ||
| put_pixel(pio, sm, urgb_u32(0, 0xff, 0)); | ||
| else if (x >= 30 && x < 40) | ||
| put_pixel(urgb_u32(0, 0, 0xff)); | ||
| put_pixel(pio, sm, urgb_u32(0, 0, 0xff)); | ||
| else | ||
| put_pixel(0); | ||
| put_pixel(pio, sm, 0); | ||
| } | ||
| } | ||
|  | ||
| void pattern_random(uint len, uint t) { | ||
| void pattern_random(PIO pio, uint sm, uint len, uint t) { | ||
| if (t % 8) | ||
| return; | ||
| for (uint i = 0; i < len; ++i) | ||
| put_pixel(rand()); | ||
| put_pixel(pio, sm, rand()); | ||
| } | ||
|  | ||
| void pattern_sparkle(uint len, uint t) { | ||
| void pattern_sparkle(PIO pio, uint sm, uint len, uint t) { | ||
| if (t % 8) | ||
| return; | ||
| for (uint i = 0; i < len; ++i) | ||
| put_pixel(rand() % 16 ? 0 : 0xffffffff); | ||
| put_pixel(pio, sm, rand() % 16 ? 0 : 0xffffffff); | ||
| } | ||
|  | ||
| void pattern_greys(uint len, uint t) { | ||
| void pattern_greys(PIO pio, uint sm, uint len, uint t) { | ||
| uint max = 100; // let's not draw too much current! | ||
| t %= max; | ||
| for (uint i = 0; i < len; ++i) { | ||
| put_pixel(t * 0x10101); | ||
| put_pixel(pio, sm, t * 0x10101); | ||
| if (++t >= max) t = 0; | ||
| } | ||
| } | ||
|  | ||
| typedef void (*pattern)(uint len, uint t); | ||
| typedef void (*pattern)(PIO pio, uint sm, uint len, uint t); | ||
| const struct { | ||
| pattern pat; | ||
| const char *name; | ||
|  | @@ -105,12 +105,17 @@ const struct { | |
| int main() { | ||
| //set_sys_clock_48(); | ||
| stdio_init_all(); | ||
| printf("WS2812 Smoke Test, using pin %d", WS2812_PIN); | ||
| printf("WS2812 Smoke Test, using pin %d\n", WS2812_PIN); | ||
|  | ||
| // todo get free sm | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this 'todo' comment can be removed now? 🙂 | ||
| PIO pio = pio0; | ||
| int sm = 0; | ||
| uint offset = pio_add_program(pio, &ws2812_program); | ||
| PIO pio; | ||
| uint sm; | ||
| uint offset; | ||
|  | ||
| // This will find a free pio and state machine for our program and load it for us | ||
| // We use pio_claim_free_sm_and_add_program_for_gpio_range so we can address gpios >= 32 if needed and supported by the hardware | ||
| bool success = pio_claim_free_sm_and_add_program_for_gpio_range(&ws2812_program, &pio, &sm, &offset, WS2812_PIN, 1, true); | ||
| hard_assert(success); | ||
|  | ||
| ws2812_program_init(pio, sm, offset, WS2812_PIN, 800000, IS_RGBW); | ||
|  | ||
|  | @@ -121,9 +126,12 @@ int main() { | |
| puts(pattern_table[pat].name); | ||
| puts(dir == 1 ? "(forward)" : "(backward)"); | ||
| for (int i = 0; i < 1000; ++i) { | ||
| pattern_table[pat].pat(NUM_PIXELS, t); | ||
| pattern_table[pat].pat(pio, sm, NUM_PIXELS, t); | ||
| sleep_ms(10); | ||
| t += dir; | ||
| } | ||
| } | ||
|  | ||
| // This will free resources and unload our program | ||
| pio_remove_program_and_unclaim_sm(&ws2812_program, pio, sm, offset); | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.