MakerMGCR555
Published © GPL3+

Music Map Player

A simple project to create a music player that plays the most suitable songs for every moment.

BeginnerFull instructions provided3 hours149
Music Map Player

Things used in this project

Hardware components

Cypress CapSense CY8CKIT-041-41XX
Product status: Discontinued
×1
Raspberry Pi 4 Model B
Raspberry Pi 4 Model B
×1
Power supply 5v 3A USB C
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1

Software apps and online services

PSoC Creator
Cypress PSoC Creator
Raspbian
Raspberry Pi Raspbian
Thonny
A simple Python IDE for beginners

Story

Read more

Schematics

Connection

It's a simple connection between the touchpad and the Raspberry.

Code

MusicSearch

C/C++
Cypress PSoC™ 4100S Capsense Pioneer Kit - The code is from the example: CE224821_PSoC4_Capsense_Touchpad_Gestures_CSD with an UART block used to send data to the Raspberry pi 4
#include "project.h"

/* PWM duty cycles for zero and fifty percent */
#define PWM_LED_OFF         10001
#define PWM_LED_HALF_POWER  5000

/* Scales x and y value of the touchpad to PWM compare value */
#define PWM_SCALAR          100

/*  */
#define LED_OFF             1
#define LED_ON              0

int main(void)
{
    /* Stores the current touchpad X and Y values */
    uint32 Xcord;
    uint16 Ycord;
    
    /* Stores the current gesture */
    uint32 gesture;
    
    UART_1_Start();    //prova
    
    CyGlobalIntEnable; /* Enable global interrupts. */
    
    /* Starts all Componenets */
    EZI2C_Start();
    CapSense_Start();
    PWM_Blue_Start();
    PWM_Green_Start();
    
    /* Set up communication data buffer to CapSense data structure to be exposed to
       I2C master */
    EZI2C_EzI2CSetBuffer1(sizeof(CapSense_dsRam), sizeof(CapSense_dsRam), (uint8 *)&CapSense_dsRam);
    
    /* Sets up a callback function using sysTick timer isr, the callback function
       is part of the CySysTickSetCallback API */
    CapSense_dsRam.timestampInterval = 2u;
    CySysTickStart();
    CySysTickSetCallback(0u, CapSense_IncrementGestureTimestamp);
    
    CapSense_ScanAllWidgets();
    
    for(;;)
    {
        /* Checks if the scan was completed before trying to process data */
        if(CapSense_NOT_BUSY == CapSense_IsBusy())
        {
            CapSense_ProcessAllWidgets();
            
            /* Stores the current detected gesture */
            gesture = CapSense_DecodeWidgetGestures(CapSense_TOUCHPAD0_WDGT_ID);
            
            /* Stores current finger position on the touchpad */            
            Xcord = CapSense_GetXYCoordinates(CapSense_TOUCHPAD0_WDGT_ID);
            Ycord = Xcord >> 16;
            Xcord = (uint16)Xcord;

            UART_1_SpiUartWriteTxData((uint16)Xcord);  
            UART_1_SpiUartWriteTxData((uint16)Ycord);

            /* Controls specific LEDs based on the gesture that was captured */
            switch(gesture)
            {
            case CapSense_ONE_FINGER_SINGLE_CLICK:
                PWM_Blue_WriteCompare(PWM_LED_HALF_POWER);
                PWM_Green_WriteCompare(PWM_LED_HALF_POWER);
                break;
            case CapSense_ONE_FINGER_EDGE_SWIPE_LEFT:
                /* If Led was off turn it to half power, if on turn it off */
                PWM_Blue_WriteCompare(PWM_Blue_ReadCompare() == PWM_LED_OFF ? PWM_LED_HALF_POWER: PWM_LED_OFF);
                break;
            case CapSense_ONE_FINGER_EDGE_SWIPE_RIGTH:
                /* If Led was off turn it to half power, if on turn it off */
                PWM_Green_WriteCompare(PWM_Green_ReadCompare() == PWM_LED_OFF ? PWM_LED_HALF_POWER: PWM_LED_OFF);
                break;
            case CapSense_ONE_FINGER_ROTATE_CW:
                /* Turns off the red LED */
                Red_LED_Write(LED_OFF);
                break;
            case CapSense_ONE_FINGER_ROTATE_CCW:
                /* Turns on the red LED */
                Red_LED_Write(LED_ON);
                break;
            default:
                /* Check if the touchpad was touched */
                if(CapSense_GetXYCoordinates(CapSense_TOUCHPAD0_WDGT_ID) != CapSense_TOUCHPAD_NO_TOUCH)
                {
                    /* Change the PWM compare value based on finger position as long as the light was not off */
                    PWM_Blue_WriteCompare(PWM_Blue_ReadCompare() == PWM_LED_OFF ? PWM_LED_OFF : (Xcord * PWM_SCALAR));
                    PWM_Green_WriteCompare(PWM_Green_ReadCompare() == PWM_LED_OFF ? PWM_LED_OFF : (Ycord * PWM_SCALAR));
                }
                break;
            }
            
            /* Required to maintain sychronization with tuner interface */
            CapSense_RunTuner();
            
            /* Initiates the next scan of all widgets */
            CapSense_ScanAllWidgets();
        }
    }
}

MapMusicPlayer

Python
Python code for the Raspberry
import os
import math  
import pygame
import serial
port = serial.Serial("/dev/ttyACM0", baudrate=115200, timeout=3.0)


SONG_END = pygame.USEREVENT + 1
pygame.mixer.init()

def init():

    directory = "/home/pi/Desktop/musica/"      
    songs = []
    os.chdir(directory)
    x=0
    y=0
    
    while True:
        rcv=port.read(2)
        if ((rcv[0] !=10) and (rcv[0] !=255) and (rcv[1] !=255)):
            x=rcv[0]
            x=int(x)
            y=rcv[1]
            y=int(y)
        # create a new list with the songs in the directory    
            for root, folders, files in os.walk(directory):
                folders.sort()
                files.sort()
                for file in files:
                    print(file)
                    if file.endswith(".mp3"):
                        songs.append(os.path.join(root, file))
                            
            #play a song from the list until it became empty
            while songs:
                play_songs(songs,x,y)

            print("Queue is empty!")

def play_songs(songs,x,y):

    curr_song = min_distance(songs,x,y)
    print(curr_song)
    print("Now playing: " + curr_song)
    pygame.mixer.music.load(curr_song)
    pygame.mixer.music.play()

    while pygame.mixer.music.get_busy():
        pygame.time.Clock().tick(100)

    pygame.mixer.music.set_endevent(SONG_END)

    if pygame.mixer.music.get_endevent() == SONG_END:
        songs.remove(curr_song)



def min_distance(songs,x,y):
    min_dist = 1000
    for song in songs:
        xs=song[24:26]
        xs=int(xs)
        ys=song[27:29]
        ys=int(ys)
        dist=math.sqrt((pow((x-xs),2))+pow((y-ys),2))
        if (dist<min_dist):
            min_song = song
            min_dist = dist
    return min_song


if __name__ == '__main__':
    while True:
        init()

Credits

MakerMG

MakerMG

3 projects • 0 followers
CR555

CR555

2 projects • 6 followers

Comments