From 2c8196051f231b528c46e852c51aa1f9da741c90 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 21 Apr 2021 13:15:39 +0200 Subject: [PATCH] test --- leds.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ nerves.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 leds.py create mode 100644 nerves.py diff --git a/leds.py b/leds.py new file mode 100644 index 0000000..64abbf6 --- /dev/null +++ b/leds.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + + +''' + +sudo aot install python3-pip +sudo pip3 install rpi_ws281x adafruit-circuitpython-neopixel +sudo python3 -m pip install --force-reinstall adafruit-blinka + +SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries +SPDX-License-Identifier: MIT + +''' + +# Simple test for NeoPixels on Raspberry Pi +import time +import board +import neopixel + + +# Choose an open pin connected to the Data In of the NeoPixel strip, i.e. board.D18 +# NeoPixels must be connected to D10, D12, D18 or D21 to work. +pixel_pin = board.D18 + +# The number of NeoPixels +num_pixels = 6 + +# The order of the pixel colors - RGB or GRB. Some NeoPixels have red and green reversed! +# For RGBW NeoPixels, simply change the ORDER to RGBW or GRBW. +ORDER = neopixel.GRB + +pixels = neopixel.NeoPixel( + pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER +) + + +def wheel(pos): + # Input a value 0 to 255 to get a color value. + # The colours are a transition r - g - b - back to r. + if pos < 0 or pos > 255: + r = g = b = 0 + elif pos < 85: + r = int(pos * 3) + g = int(255 - pos * 3) + b = 0 + elif pos < 170: + pos -= 85 + r = int(255 - pos * 3) + g = 0 + b = int(pos * 3) + else: + pos -= 170 + r = 0 + g = int(pos * 3) + b = int(255 - pos * 3) + return (r, g, b) if ORDER in (neopixel.RGB, neopixel.GRB) else (r, g, b, 0) + + +def rainbow_cycle(wait): + for j in range(255): + for i in range(num_pixels): + pixel_index = (i * 256 // num_pixels) + j + pixels[i] = wheel(pixel_index & 255) + pixels.show() + time.sleep(wait) + + +while True: + # Comment this line out if you have RGBW/GRBW NeoPixels + pixels.fill((255, 0, 0)) + # Uncomment this line if you have RGBW/GRBW NeoPixels + # pixels.fill((255, 0, 0, 0)) + pixels.show() + time.sleep(1) + + # Comment this line out if you have RGBW/GRBW NeoPixels + pixels.fill((0, 255, 0)) + # Uncomment this line if you have RGBW/GRBW NeoPixels + # pixels.fill((0, 255, 0, 0)) + pixels.show() + time.sleep(1) + + # Comment this line out if you have RGBW/GRBW NeoPixels + pixels.fill((0, 0, 255)) + # Uncomment this line if you have RGBW/GRBW NeoPixels + # pixels.fill((0, 0, 255, 0)) + pixels.show() + time.sleep(1) + + rainbow_cycle(0.001) # rainbow cycle with 1ms delay per step diff --git a/nerves.py b/nerves.py new file mode 100644 index 0000000..8d11708 --- /dev/null +++ b/nerves.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +''' +Nerves + +sniff packets from interface en0 using python module scapy (2.3.1) +generate led color for bhoreal in usb midi mode depending on packet port number +send led number + color to PD patch boreal.pd +in OSC format : /bhoreal/in lednumber ledcolor +color is midi parameter so 0 to 127. +v0.3 +By Sam Neurohack +LICENCE : CC +''' + +from OSC import OSCClient, OSCMessage +from time import sleep +import types +import random +from scapy.all import * + +client = OSCClient() +msg = OSCMessage() + +counter = 0 + +def sendled(zzzport): + global counter + + zzz = zzzport % 127 + # zzz = led color + msg = OSCMessage() + msg.setAddress("/bhoreal/in") + msg.append(counter) + msg.append(zzz) + try: + client.sendto(msg, ('127.0.0.1', 9002)) + msg.clearData() + except: + print 'Connection refused' + pass + sleep(0.001) + counter += 1 + if counter > 63: + counter = 0 + + + +def print_summary(pkt): + if IP in pkt: + ip_src=pkt[IP].src + ip_dst=pkt[IP].dst + + + if TCP in pkt: + tcp_sport=pkt[TCP].sport + tcp_dport=pkt[TCP].dport + + if tcp_sport < 50000: + print " IP src " + str(ip_src) + " TCP sport " + str(tcp_sport) + sendled(tcp_sport) + if tcp_dport < 50000: + print " IP dst " + str(ip_dst) + " TCP dport " + str(tcp_dport) + sendled(tcp_dport) + if UDP in pkt: + udp_sport=pkt[UDP].sport + udp_dport=pkt[UDP].dport + + if udp_sport < 50000: + print " IP src " + str(ip_src) + " UDP sport " + str(udp_sport) + sendled(udp_sport) + + if udp_dport < 50000: + print " IP dst " + str(ip_dst) + " UDP dport " + str(udp_dport) + sendled(udp_dport) + + + if ARP in pkt and pkt[ARP].op in (1,2): + print " ARP" + sendled(67) + + + +def handle_error(self,request,client_address): # All callbacks + pass + +sniff(iface='en0', prn=print_summary, store=0) \ No newline at end of file