Hackster is hosting Hackster Holidays, Ep. 4: Livestream & Giveaway Drawing. Start streaming on Wednesday!Stream Hackster Holidays, Ep. 4 on Wednesday!
clzfranco
Created December 19, 2019 © GPL3+

Simon says

We present the retro game Simon Says. Alexa generates different color sequences and the user must press the EV3 sensors in the correct order

IntermediateFull instructions provided4 hours35
Simon says

Things used in this project

Story

Read more

Schematics

Simon Says Project Walkthrough

This document will be useful for the construction of the "Simon says" project

Code

index.js

JavaScript
The main Skill file
// This sample demonstrates handling intents from an Alexa skill using the Alexa Skills Kit SDK (v2).
// Please visit https://alexa.design/cookbook for additional examples on implementing slots, dialog management,
// session persistence, api calls, and more.
const Alexa = require('ask-sdk-core');
const Util = require('./util');
const EV3 = require('./ev3');
var persistenceAdapter = Util.getPersistenceAdapter();


// The namespace of the custom directive to be sent by this skill
const NAMESPACE = 'Custom.Mindstorms.Gadget';
// The name of the custom directive to be sent this skill
const NAME_CONTROL = 'control';

const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle: async function(handlerInput) {

        let request = handlerInput.requestEnvelope;
        let { apiEndpoint, apiAccessToken } = request.context.System;
        let apiResponse = await EV3.getConnectedEndpoints(apiEndpoint, apiAccessToken);
        
        if ((apiResponse.endpoints || []).length === 0) {
            speakOutput= `I couldn't find an EV3 Brick connected to this Echo device. Please check to make sure your EV3 Brick is connected, and try again.`;
            
            return handlerInput.responseBuilder
            .speak(speakOutput)
            .withShouldEndSession(true)
            .getResponse();

        }
        
        // Store the gadget endpointId to be used in this skill session
        let endpointId = apiResponse.endpoints[0].endpointId || [];
        Util.putSessionAttribute(handlerInput, 'endpointId', endpointId);

        var speakOutput = 'Welcome to simon says, you can say start game or Help. Which would you like to try?';

        const attributesManager = handlerInput.attributesManager;
        
        var {gameRecord} = attributesManager.getSessionAttributes();
        
        if (gameRecord === undefined){
            gameRecord=0;
            Util.putSessionAttribute(handlerInput, 'gameRecord', 0);
        }


        Util.putSessionAttribute(handlerInput, 'level', 1);
        Util.putSessionAttribute(handlerInput, 'score', 0);
        Util.putSessionAttribute(handlerInput, 'counter', 0);

        Util.putAPLmessage(handlerInput, 'Simon Says', 'Current record ' + gameRecord, 'If you want to play just say start.');
        
        // Set the token to track the event handler
        const token = handlerInput.requestEnvelope.request.requestId;
        Util.putSessionAttribute(handlerInput, 'token', token);

        // Set skill duration to 5 minutes (ten 30-seconds interval)
        Util.putSessionAttribute(handlerInput, 'duration', 10);
        
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .addDirective(EV3.buildStartEventHandler(token,60000, {}))
            .getResponse();
    }
};


const StartGameIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'StartGameIntent';
    },
    handle(handlerInput) {
        const attributesManager = handlerInput.attributesManager;
        const attributes = attributesManager.getSessionAttributes();

        var {level} = attributesManager.getSessionAttributes();
        var {score} = attributesManager.getSessionAttributes();
        var {gameRecord} = attributesManager.getSessionAttributes();

        
        var speakOutput = 'Simon says, press ';

        var s= Util.generate_sequence(1);

        var str= Util.sequence2colors(s);
        speakOutput += str;

        Util.putSessionAttribute(handlerInput, 'color', str);
        
        var mainTxt= 'Record '+  gameRecord + '<br>Score: ' + score + '<br>Level: ' + level;
        Util.putAPLmessage(handlerInput, 'Simon Says', mainTxt, 'To start a new game just say start game');

        const directive1 = EV3.build(attributes.endpointId, NAMESPACE, NAME_CONTROL,
            {
                type: 'init',
                sequence: s
            });


        return handlerInput.responseBuilder
            .speak(speakOutput)
            .addDirective(directive1)
            .getResponse();
    }
};


const EventsReceivedRequestHandler = {
    // Checks for a valid token and endpoint.
    canHandle(handlerInput) {
        let { request } = handlerInput.requestEnvelope;
        console.log('Request type: ' + Alexa.getRequestType(handlerInput.requestEnvelope));
        if (request.type !== 'CustomInterfaceController.EventsReceived') return false;

        const attributesManager = handlerInput.attributesManager;
        let sessionAttributes = attributesManager.getSessionAttributes();
        let customEvent = request.events[0];

        // Validate event token
        if (sessionAttributes.token !== request.token) {
            console.log("Event token doesn't match. Ignoring this event");
            return false;
        }

        // Validate endpoint
        let requestEndpoint = customEvent.endpoint.endpointId;
        let endpointId = attributesManager.getSessionAttributes().endpointId || [];
 
        if (requestEndpoint !== sessionAttributes.endpointId) {
            console.log("Event endpoint id doesn't match. Ignoring this event");
            return false;
        }
        return true;
    },
    handle(handlerInput) {

        console.log("******* Received Custom Event ******");
        let customEvent = handlerInput.requestEnvelope.request.events[0];
        let payload = customEvent.payload;
        let name = customEvent.header.name;
        const attributesManager = handlerInput.attributesManager;
        const attributes = handlerInput.attributesManager.getSessionAttributes();  

        var speakOutput = 'Simon says, press ';

        var {counter} = attributesManager.getSessionAttributes();
        var {color} = attributesManager.getSessionAttributes();
        var {level} = attributesManager.getSessionAttributes();
        var {score} = attributesManager.getSessionAttributes();
        var {gameRecord} = attributesManager.getSessionAttributes();
        
        if (name === 'Game') {
                let win = parseInt(payload.win);
                
                if (win){
                    counter++;
                    score+= (level+1)*10;
                    
                    Util.putSessionAttribute(handlerInput, 'counter', counter);
                    Util.putSessionAttribute(handlerInput, 'score', score);
        
        
                    if (counter === 3){
                        speakOutput=  `Congratulations, let's level up. Simon says press `;
                        level++; //desactivado para simulador
                        Util.putSessionAttribute(handlerInput, 'level', level);
                        Util.putSessionAttribute(handlerInput, 'counter', 0);
                    }
        
                    var mainTxt= 'Record '+  gameRecord + '<br>Score: ' + score + '<br>Level: ' + level;
        
                    var s= Util.generate_sequence(level);
                    var str= Util.sequence2colors(s);
                    speakOutput += str;
                    Util.putSessionAttribute(handlerInput, 'color', str);
                    
                    const directive1 = EV3.build(attributes.endpointId, NAMESPACE, NAME_CONTROL,
                        {
                            type: 'init',
                            sequence: s
                        });
                    
                    Util.putAPLmessage(handlerInput, 'Simon Says', mainTxt, 'To start a new game just say start game');
    
                    let response =  handlerInput.responseBuilder
                            .speak(speakOutput)
                            .addDirective(directive1)
                            .getResponse();
                    delete response.shouldEndSession;
                    return response;
        
                }else{
                    
                    speakOutput= `Game over. `;
                    
                   if (score > gameRecord){
                        speakOutput += "Congratulations you set a new record of " + score + ' points. ';
                        
                        mainTxt= 'New record of ' + score + ' points. ';
                        
                        Util.putSessionAttribute(handlerInput, 'gameRecord', score);
        
                        
                    }else
                    if (score === gameRecord){
                        speakOutput += "Congratulations you tied the current record of " + score + ' points. ';
                        mainTxt= 'You tied the record of ' + score + ' points. ';
                    
                    }
                    else{
                        var percentage=score*100/gameRecord;
                        
                        if (percentage >= 80)
                            speakOutput += `Congratulations you're score was very close to the game record. You have ` + score + ' points. ';
                        else
                        if (percentage >= 60)
                            speakOutput += `Well done. You're score was ` + score + ' points. ';
                        else
                            speakOutput += `You're score was ` + score + ' points. ';
                        
                        mainTxt=  `You're score was ` + score + ' points. ';
                        
                }
    
                speakOutput+= `If you want to play again say new game.`;
                
                Util.putSessionAttribute(handlerInput, 'level', 1);
                Util.putSessionAttribute(handlerInput, 'score', 0);
                Util.putSessionAttribute(handlerInput, 'counter', 0);
    
                Util.putAPLmessage(handlerInput, 'Simon Says', mainTxt, 'To start a new game just say start game');
            }

            
        }

        return handlerInput.responseBuilder
            .speak(speakOutput, "REPLACE_ALL")
            .reprompt(speakOutput)
            .getResponse();
    }
};

const ExpiredRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'CustomInterfaceController.Expired'
    },
    handle(handlerInput) {
        console.log("======== Custom Event Expiration Input ==========");

        // Set the token to track the event handler
        const token = handlerInput.requestEnvelope.request.requestId;
        Util.putSessionAttribute(handlerInput, 'token', token);

        const attributesManager = handlerInput.attributesManager;
        let duration = attributesManager.getSessionAttributes().duration || 0;
        if (duration > 0) {
            Util.putSessionAttribute(handlerInput, 'duration', --duration);

            // Extends skill session
            const speechOutput = `${duration} minutes remaining.`;
            return handlerInput.responseBuilder
                .addDirective(EV3.buildStartEventHandler(token, 60000, {}))
                .speak(speechOutput)
                .getResponse();
        }
        else {
            // End skill session
            return handlerInput.responseBuilder
                .speak("Skill duration expired. Goodbye.")
                .withShouldEndSession(true)
                .getResponse();
        }
    }
};


const HelpIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.HelpIntent';
    },
    handle(handlerInput) {
        const speakOutput = 'I will say a sequence of colors and you have to press the buttons in the correct order. To begin the game just say start.';


        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};
const CancelAndStopIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && (Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.CancelIntent'
                || Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.StopIntent');
    },
    handle(handlerInput) {
        console.log(`*********** CancelAndStopIntentHandler `);

        const speakOutput = 'Goodbye!';
        return handlerInput.responseBuilder
            .speak(speakOutput)
            .addDirective(EV3.buildStopEventHandlerDirective(handlerInput))
            .withShouldEndSession(true)
            .getResponse();
            
    }
};
const SessionEndedRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'SessionEndedRequest';
    },
    handle(handlerInput) {
        
        console.log(`*********** Session ended: ${JSON.stringify(handlerInput.requestEnvelope)}`);
        // Any cleanup logic goes here.
        return handlerInput.responseBuilder
        .addDirective(EV3.buildStopEventHandlerDirective(handlerInput))
        .getResponse();
    }
};

// The intent reflector is used for interaction model testing and debugging.
// It will simply repeat the intent the user said. You can create custom handlers
// for your intents by defining them above, then also adding them to the request
// handler chain below.
const IntentReflectorHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest';
    },
    handle(handlerInput) {
        const intentName = Alexa.getIntentName(handlerInput.requestEnvelope);
        const speakOutput = `You just triggered ${intentName}`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            //.reprompt('add a reprompt if you want to keep the session open for the user to respond')
            .getResponse();
    }
};

// Generic error handling to capture any syntax or routing errors. If you receive an error
// stating the request handler chain is not found, you have not implemented a handler for
// the intent being invoked or included it in the skill builder below.
const ErrorHandler = {
    canHandle() {
        return true;
    },
    handle(handlerInput, error) {
        console.log(`~~~~ Error handled: ${error.stack}`);
        const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .addDirective(EV3.buildStopEventHandlerDirective(handlerInput))
            .getResponse();
    }
};

const FallbackIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'AMAZON.FallbackIntent';
    },
    handle(handlerInput) {
        console.log(`~~~~ Fallback handled: `);
        const speakOutput = `Sorry, I had trouble doing what you asked. Please try again.`;

        return handlerInput.responseBuilder
            .speak(speakOutput)
            .reprompt(speakOutput)
            .getResponse();
    }
};

/* *
 * Below we use async and await ( more info: javascript.info/async-await )
 * It's a way to wrap promises and waait for the result of an external async operation
 * Like getting and saving the persistent attributes
 * */
const LoadAttributesRequestInterceptor = {
    async process(handlerInput) {
        const {attributesManager, requestEnvelope} = handlerInput;
        if (Alexa.isNewSession(requestEnvelope)){ //is this a new session? this check is not enough if using auto-delegate (more on next module)
            const persistentAttributes = await attributesManager.getPersistentAttributes() || {};
            console.log('Loading from persistent storage: ' + JSON.stringify(persistentAttributes));
            //copy persistent attribute to session attributes
            attributesManager.setSessionAttributes(persistentAttributes); // ALL persistent attributtes are now session attributes
        }
    }
};

// If you disable the skill and reenable it the userId might change and you loose the persistent attributes saved below as userId is the primary key
const SaveAttributesResponseInterceptor = {
    async process(handlerInput, response) {
        if (!response) return; // avoid intercepting calls that have no outgoing response due to errors
        const {attributesManager, requestEnvelope} = handlerInput;
        const sessionAttributes = attributesManager.getSessionAttributes();
        const shouldEndSession = (typeof response.shouldEndSession === "undefined" ? true : response.shouldEndSession); //is this a session end?
        if (shouldEndSession || Alexa.getRequestType(requestEnvelope) === 'SessionEndedRequest') { // skill was stopped or timed out
           
            // we make ALL session attributes persistent
            console.log('********** Saving SCORE to persistent storage:' + JSON.stringify(sessionAttributes));
            attributesManager.setPersistentAttributes(sessionAttributes);
            await attributesManager.savePersistentAttributes();
        }
    }
};



// The SkillBuilder acts as the entry point for your skill, routing all request and response
// payloads to the handlers above. Make sure any new handlers or interceptors you've
// defined are included below. The order matters - they're processed top to bottom.
exports.handler = Alexa.SkillBuilders.custom()
    .addRequestHandlers(
        LaunchRequestHandler,
        HelpIntentHandler,
        CancelAndStopIntentHandler,
        SessionEndedRequestHandler,
        StartGameIntentHandler,
        ExpiredRequestHandler,
        EventsReceivedRequestHandler,
        FallbackIntentHandler,
        IntentReflectorHandler, // make sure IntentReflectorHandler is last so it doesn't override your custom intent handlers
    )
    .addErrorHandlers(
        ErrorHandler,
    )
    .addRequestInterceptors(
        LoadAttributesRequestInterceptor)
    .addResponseInterceptors(
        SaveAttributesResponseInterceptor)
    .withPersistenceAdapter(persistenceAdapter)
    .lambda();

ev3.js

JavaScript
Alexa EV3 communication functions
'use strict';

const Https = require('https');
const AWS = require('aws-sdk');
const Escape = require('lodash/escape');

/**
 * Builds a directive to start the EventHandler.
 * @param token - a unique identifier to track the event handler
 * @param {number} timeout - the duration to wait before sending back the expiration
 * payload to the skill.
 * @param payload - the expiration json payload
 * @see {@link https://developer.amazon.com/docs/alexa-gadgets-toolkit/receive-custom-event-from-gadget.html#start}
 */
exports.buildStartEventHandler = function (token, timeout = 30000, payload)  {
    return {
        type: "CustomInterfaceController.StartEventHandler",
        token: token,
        expiration : {
            durationInMilliseconds: timeout,
            expirationPayload: payload
        }
    };
};

/**
 *
 * Builds a directive to stops the active event handler.
 * The event handler is identified by the cached token in the session attribute.
 * @param {string} handlerInput - the context from Alexa Service
 * @see {@link https://developer.amazon.com/docs/alexa-gadgets-toolkit/receive-custom-event-from-gadget.html#stop}
 */
exports.buildStopEventHandlerDirective = function (handlerInput) {

    let token = handlerInput.attributesManager.getSessionAttributes().token || '';
    return {
        "type": "CustomInterfaceController.StopEventHandler",
        "token": token
    }
};


/**
 * Build a custom directive payload to the gadget with the specified endpointId
 * @param {string} endpointId - the gadget endpoint Id
 * @param {string} namespace - the namespace of the skill
 * @param {string} name - the name of the skill within the scope of this namespace
 * @param {object} payload - the payload data
 * @see {@link https://developer.amazon.com/docs/alexa-gadgets-toolkit/send-gadget-custom-directive-from-skill.html#respond}
 */
exports.build = function (endpointId, namespace, name, payload) {
    // Construct the custom directive that needs to be sent
    // Gadget should declare the capabilities in the discovery response to
    // receive the directives under the following namespace.
    return {
        type: 'CustomInterfaceController.SendDirective',
        header: {
            name: name,
            namespace: namespace
        },
        endpoint: {
            endpointId: endpointId
        },
        payload
    };
};

/**
 * To get a list of all the gadgets that meet these conditions,
 * Call the Endpoint Enumeration API with the apiEndpoint and apiAccessToken to
 * retrieve the list of all connected gadgets.
 *
 * @param {string} apiEndpoint - the Endpoint API url
 * @param {string} apiAccessToken  - the token from the session object in the Alexa request
 * @see {@link https://developer.amazon.com/docs/alexa-gadgets-toolkit/send-gadget-custom-directive-from-skill.html#call-endpoint-enumeration-api}
 */
exports.getConnectedEndpoints = function(apiEndpoint, apiAccessToken) {

    // The preceding https:// need to be stripped off before making the call
    apiEndpoint = (apiEndpoint || '').replace('https://', '');
    return new Promise(((resolve, reject) => {

        const options = {
            host: apiEndpoint,
            path: '/v1/endpoints',
            method: 'GET',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + apiAccessToken
            }
        };

        const request = Https.request(options, (response) => {
            response.setEncoding('utf8');
            let returnData = '';
            response.on('data', (chunk) => {
                returnData += chunk;
            });

            response.on('end', () => {
                resolve(JSON.parse(returnData));
            });

            response.on('error', (error) => {
                reject(error);
            });
        });
        request.end();
    }));
};

util.js

JavaScript
Useful Alexa functions, which include APL functions, and sequence generators
'use strict';

const Https = require('https');
const AWS = require('aws-sdk');
const Escape = require('lodash/escape');

const s3SigV4Client = new AWS.S3({
    signatureVersion: 'v4'
});


module.exports= {

    getS3PreSignedUrl(s3ObjectKey) {
    
        const bucketName = process.env.S3_PERSISTENCE_BUCKET;
        const s3PreSignedUrl = s3SigV4Client.getSignedUrl('getObject', {
            Bucket: bucketName,
            Key: s3ObjectKey,
            Expires: 60*1 // the Expires is capped for 1 minute
        });
        console.log(`Util.s3PreSignedUrl: ${s3ObjectKey} URL ${s3PreSignedUrl}`);
        return s3PreSignedUrl;
    
    },

    getPersistenceAdapter(tableName) {
    // This function is an indirect way to detect if this is part of an Alexa-Hosted skill
    function isAlexaHosted() {
        return process.env.S3_PERSISTENCE_BUCKET;
    }
    if (isAlexaHosted()) {
        const {S3PersistenceAdapter} = require('ask-sdk-s3-persistence-adapter');
        return new S3PersistenceAdapter({
            bucketName: process.env.S3_PERSISTENCE_BUCKET
        });
    } else {
        // IMPORTANT: don't forget to give DynamoDB access to the role you're using to run this lambda (via IAM policy)
        const {DynamoDbPersistenceAdapter} = require('ask-sdk-dynamodb-persistence-adapter');
        return new DynamoDbPersistenceAdapter({
            tableName: tableName || 'simon_says',
            createTable: true
        });
    }
    },

   
   putSessionAttribute(handlerInput, key, value) {
    const attributesManager = handlerInput.attributesManager;
    let sessionAttributes = attributesManager.getSessionAttributes();
    sessionAttributes[key] = value;
    attributesManager.setSessionAttributes(sessionAttributes);
},

   supportsAPL(handlerInput) {
        const {supportedInterfaces} = handlerInput.requestEnvelope.context.System.device;
        return !!supportedInterfaces['Alexa.Presentation.APL'];
    },    
    

  putAPLmessage(handlerInput, headerTxt, mainTxt, hintTxt){
    
     if (this.supportsAPL(handlerInput)){
    
        const {Viewport} = handlerInput.requestEnvelope.context;
        const resolution = Viewport.pixelWidth + 'x' + Viewport.pixelHeight;
          
        handlerInput.responseBuilder
                .addDirective({
                    type: 'Alexa.Presentation.APL.RenderDocument',
                    document:  require('./documents/launch.json'),
                    datasources: {
                     launchData: {
                                    type: 'object',
                                    properties: {
                                        headerTitle: headerTxt,
                                        mainText: mainTxt,
                                        hintString: hintTxt,
                                        //logoImage: isBirthday ? null : Viewport.pixelWidth > 480 ? util.getS3PreSignedUrl('Media/full_icon_512.png') : util.getS3PreSignedUrl('Media/full_icon_108.png'),
                                        backgroundImage: this.getS3PreSignedUrl('Media/simon_'+resolution+'.png') ,
                                        backgroundOpacity: "0.5"
                                    },
                                    transformers: [{
                                        inputPath: 'hintString',
                                        transformer: 'textToHint',
                                    }]
                                }
                      }
                    
                });  
     }
     
  },    
    
  generate_sequence (level){
//    var numOfColors= level+1;
    var numOfColors= level;
    var s=[],x;

    for (var i=0; i<numOfColors; i++){
        x=Math.floor(Math.random() * 4);
        s.push(x)
    }
    return s;
  },



  sequence2colors(s){
    //var c=[]
    const colores=['red','green','blue','yellow'];
    var color;
    var str='';

    for (var i=0; i<s.length; i++){
        color= colores[s[i]];
        if (str !== '')
            str+= ', ';
        str+= color;
    }
    return str;
   }    
    
}

package.json

JSON
File with the skill dependencies
{
  "name": "simon-says",
  "version": "1.1.0",
  "description": "alexa utility for quickly building skills",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Amazon Alexa",
  "license": "ISC",
  "dependencies": {
    "ask-sdk-core": "^2.6.0",
    "ask-sdk-model": "^1.18.0",
    "aws-sdk": "^2.326.0",
    "ask-sdk-s3-persistence-adapter": "^2.7.0",
    "ask-sdk-dynamodb-persistence-adapter": "^2.7.0"
  }
}

launch.json

JSON
APL template for the Simon says skill
{
    "type": "APL",
    "version": "1.1",
    "theme": "dark",
    "import": [
        {
            "name": "alexa-viewport-profiles",
            "version": "1.0.0"
        },
        {
            "name": "alexa-layouts",
            "version": "1.0.0"
        },
        {
            "name": "alexa-styles",
            "version": "1.0.0"
        }
    ],
    "layouts": {
        "LaunchScreen": {
            "description": "A basic launch screen with a text and logo",
            "parameters": [
                {
                    "name": "mainText",
                    "type": "string"
                },
                {
                    "name": "logo",
                    "type": "string"
                }
            ],
            "items": [
                {
                    "type": "Container",
                    "width": "100%",
                    "height": "100%",
                    "justifyContent": "center",
                    "alignItems": "center",
                    "item": [
                        {
                            "type": "Image",
                            "source": "${logo}",
                            "width": "20vw",
                            "height": "20vw",
                            "scale": "best-fill"
                        },
                        {
                            "type": "Text",
                            "text": "${mainText}",
                            "style": "textStyleDisplay5",
                            "textAlign": "center",
                            "paddingTop": "30dp",
                            "color": "white"
                        }
                    ]
                }
            ]
        }
    },
    "mainTemplate": {
        "parameters": [
            "payload"
        ],
        "items": [
            {
                "type": "Container",
                "direction": "column",
                "items": [
                    {
                        "type": "Image",
                        "source": "${payload.launchData.properties.backgroundImage}",
                        "scale": "best-fill",
                        "width": "100vw",
                        "height": "100vh",
                        "opacity": "${payload.launchData.properties.backgroundOpacity}"
                    },
                    {
                        "type": "Container",
                        "position": "absolute",
                        "width": "100vw",
                        "height": "100vh",
                        "direction": "column",
                        "items": [
                            {
                                "headerTitle": "${payload.launchData.properties.headerTitle}",
                                "type": "AlexaHeader"
                            },
                            {
                                "when": "${@viewportProfile == @hubRoundSmall}",
                                "type": "Container",
                                "width": "100vw",
                                "height": "60vh",
                                "position": "relative",
                                "alignItems": "center",
                                "justifyContent": "center",
                                "direction": "column",
                                "items": [
                                    {
                                        "type": "LaunchScreen",
                                        "mainText": "${payload.launchData.properties.mainText}",
                                        "logo": "${payload.launchData.properties.logoImage}"
                                    }
                                ]
                            },
                            {
                                "when": "${@viewportProfile == @hubLandscapeSmall || @viewportProfile == @hubLandscapeMedium || @viewportProfile == @hubLandscapeLarge || @viewportProfile == @tvLandscapeXLarge}",
                                "type": "Container",
                                "width": "100vw",
                                "height": "70vh",
                                "direction": "column",
                                "alignItems": "center",
                                "justifyContent": "center",
                                "items": [
                                    {
                                        "type": "LaunchScreen",
                                        "mainText": "${payload.launchData.properties.mainText}",
                                        "logo": "${payload.launchData.properties.logoImage}"
                                    }
                                ]
                            },
                            {
                                "footerHint": "${payload.launchData.properties.hintString}",
                                "type": "AlexaFooter",
                                "when": "${@viewportProfile == @hubLandscapeSmall || @viewportProfile == @hubLandscapeMedium || @viewportProfile == @hubLandscapeLarge || @viewportProfile == @tvLandscapeXLarge}"
                            }
                        ]
                    }
                ]
            }
        ]
    }
}

simon.py

Python
The Python main code to be run on the EV3
#!/usr/bin/env python3
# Copyright 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
# 
# You may not use this file except in compliance with the terms and conditions 
# set forth in the accompanying LICENSE.TXT file.
#
# THESE MATERIALS ARE PROVIDED ON AN "AS IS" BASIS. AMAZON SPECIFICALLY DISCLAIMS, WITH 
# RESPECT TO THESE MATERIALS, ALL WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING 
# THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.

import os
import sys
import time
import logging
import json
import random
import threading

from enum import Enum
from agt import AlexaGadget

from ev3dev2.led import Leds
from ev3dev2.sound import Sound
from ev3dev2.sensor.lego import TouchSensor
from random import randrange
from time import sleep

# Set the logging level to INFO to see messages from AlexaGadget
logging.basicConfig(level=logging.INFO, stream=sys.stdout, format='%(message)s')
logging.getLogger().addHandler(logging.StreamHandler(sys.stderr))
logger = logging.getLogger(__name__)


class EventName(Enum):
    """
    The list of custom event name sent from this gadget
    """
    COLOR = "Color"
    GAME= "Game"
    #MOVING= 'Moving'

class MindstormsGadget(AlexaGadget):
    """
    A Mindstorms gadget that performs movement based on voice commands.
    Two types of commands are supported, directional movement and preset.
    """

    def __init__(self):
        """
        Performs Alexa Gadget initialization routines and ev3dev resource allocation.
        """
        super().__init__()

        # Gadget state
        self.patrol_mode = False
        self.color_mode = False
        self.moving_mode= False
        self.player= "1"

        self.playing= False

        # Ev3dev initialization
        self.leds = Leds()
        self.sound = Sound()
        self.tsg = TouchSensor('in1')
        self.tsr = TouchSensor('in2') 
        self.tsy = TouchSensor('in3')
        self.tsb = TouchSensor('in4')
        self.sequence=[]


        # Start threads
        threading.Thread(target=self._touch_thread, daemon=True).start()


    def on_connected(self, device_addr):
        """
        Gadget connected to the paired Echo device.
        :param device_addr: the address of the device we connected to
        """
        self.leds.set_color("LEFT", "GREEN")
        self.leds.set_color("RIGHT", "GREEN")
        logger.info("{} connected to Echo device".format(self.friendly_name))        
        #print('Color Thread ON')
        #logger.info("Ya me conecte")


    def on_disconnected(self, device_addr):
        """
        Gadget disconnected from the paired Echo device.
        :param device_addr: the address of the device we disconnected from
        """
        self.leds.set_color("LEFT", "BLACK")
        self.leds.set_color("RIGHT", "BLACK")
        logger.info("{} disconnected from Echo device".format(self.friendly_name))
        self.color_mode = False

    def on_custom_mindstorms_gadget_control(self, directive):
        """
        Handles the Custom.Mindstorms.Gadget control directive.
        :param directive: the custom directive with the matching namespace and name
        """
        try:
            payload = json.loads(directive.payload.decode("utf-8"))
            print("Control payload: {}".format(payload), file=sys.stderr)
            control_type = payload["type"]
            print('control type: ',control_type)

            if control_type == "move":

                # Expected params: [direction, duration, speed]
                self._move(payload["direction"], int(payload["duration"]), int(payload["speed"]))

            if control_type == "command":
                # Expected params: [command]
                self._activate(payload["command"])

            if control_type == "init":
                # Expected params: [command]
                print('*******************************************')

                self.sequence= payload["sequence"]
                print('New sequence received: ', end =" ")
                print(self.sequence)

                c= self.sequence2colors(self.sequence)
                #print(c)
                self.playing= True

            if control_type == "start":
                # Expected params: [command]
                print('start received')
                self._start()


        except KeyError:
            print("Missing expected parameters: {}".format(directive), file=sys.stderr)


    def _send_event(self, name: EventName, payload):
        """
        Sends a custom event to trigger a sentry action.
        :param name: the name of the custom event
        :param payload: the sentry JSON payload
        """
        self.send_custom_event('Custom.Mindstorms.Gadget', name.value, payload)

    #returns the names of the colors from the sequence
    def sequence2colors(self, s):
        l=[]
        for x in s:
            if x==0:
                l.append('red')
            elif x==1:
                l.append('green')
            elif x==2:
                l.append('blue')
            elif x==3:
                l.append('yellow')        
        return l

    #detects which color was pressed
    def color_sensor(self):
        c=-1
        r=self.tsr.value()
        g=self.tsg.value()
        b=self.tsb.value()
        y=self.tsy.value()

        if r and not (g or b or y):
            c= 0
        elif g and not (r or b or y):
            c= 1
        elif b and not (r or g or y):
            c= 2
        elif y and not (r or g or b):
            c= 3
        elif r or g or b or y:
            c= -2
        return c

    #returns a sound for each color
    def color2sound(self,color):
        if color==0:
            t=209
        elif color==1:
            t=415
        elif color==2:
            t=523
        elif color==3:
            t=310
        else:
            t=0

        return t

    #the main thread, this thread will be activated once the Alexa skill sends a
    #new sequence

    def _touch_thread(self):

        s= self.sequence
        colors= self.sequence2colors(s)
        print('New game')
        print(colors)        
        i=0

        while True:
            while self.playing:
                c=-1

                #detect push
                while c == -1:
                    c= self.color_sensor()
                    sleep(0.01)
                
                t= self.color2sound(c)

                self.sound.play_tone(t, 0.1, play_type=1)

                print('push ',c)

                #detect release
                c2=0
                while c2 != -1:
                    c2= self.color_sensor()
                    sleep(0.01)

                print('release ',c)

                #incorrect sequence, send error to Alexa
                if self.sequence[i] != c:
                    print('lose ','i= ',i,', s[i]= ', self.sequence[i],', c= ',c)            
                    self.playing= False
                    self._send_event(EventName.GAME, {'win': 0})
                    i=-1

                #correct sequence, send ok to Alexa
                elif i== len(self.sequence) -1:
                    print('correct!!!')
                    self.playing= False
                    self._send_event(EventName.GAME, {'win': 1})
                    i=-1

                i= i+1
            time.sleep(1)


if __name__ == '__main__':
    print('Simon says ...')

    gadget = MindstormsGadget()

    # Set LCD font and turn off blinking LEDs
    os.system('setfont Lat7-Terminus12x6')
    gadget.leds.set_color("LEFT", "BLACK")
    gadget.leds.set_color("RIGHT", "BLACK")

    # Startup sequence
    gadget.sound.play_song((('C4', 'e'), ('D4', 'e'), ('E5', 'q')))
    gadget.leds.set_color("LEFT", "GREEN")
    gadget.leds.set_color("RIGHT", "GREEN")

    # Gadget main entry point
    gadget.main()

    # Shutdown sequence
    gadget.sound.play_song((('E5', 'e'), ('C4', 'e')))
    gadget.leds.set_color("LEFT", "BLACK")
    gadget.leds.set_color("RIGHT", "BLACK")

simon.ini

INI
File with gadget settings
# Copyright 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
# 
# You may not use this file except in compliance with the terms and conditions 
# set forth in the accompanying LICENSE.TXT file.
#
# THESE MATERIALS ARE PROVIDED ON AN "AS IS" BASIS. AMAZON SPECIFICALLY DISCLAIMS, WITH 
# RESPECT TO THESE MATERIALS, ALL WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING 
# THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.

[GadgetSettings]

#Use your AmazonId and AlexaGadgetSecret

amazonId = 
alexaGadgetSecret = 

[GadgetCapabilities]
Custom.Mindstorms.Gadget = 1.0

Credits

clzfranco

clzfranco

2 projects • 2 followers

Comments