Exemple #1
0
/*
Issues a command to lock/unlock the screen whenever the lock_status toggles.

@param lock_status The current lock status
@param previous_status The previous lock status
@return lock_status The new lock status.
*/
bool execute_status(bool lock_status, bool previous_status, key_device_t* key) {
    if(lock_status == previous_status) {
        return lock_status;
    }

    if(lock_status == FALSE) {
        bus_send_message("LOCK"); // log after check.
        log_event("<execute_status>","Locked screen, no valid keys nearby",INFO);
    } else {
        bus_send_message("UNLOCK"); // same here
        char* user = key -> user;
        char* msg = (char*)malloc(sizeof(char) * (USR_LEN + 50));
        strcat_mult_str(6,msg,"Unlocked screen, valid key nearby (",user,")");

        log_event("<execute_status>",msg,INFO);

        if(GREET_USER) {
            char greet_id[18];
            ba2str(&(key->addr),greet_id);
            play_greeting(greet_id);
        }

        free(msg);
    }
    return lock_status;
}
// check button state and send messages if a button is pressed.
static void check_buttons (void)
{
    const uint8_t registers[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    uint8_t temp, msglen, msg[8];
    static uint8_t regidx = 0, light = 0;
    
    // check button
    g_buttons = PIND & (1<<BTN_TEST | 1<<BTN_EXP);
    temp = g_buttons ^ g_oldbuttons;
    g_oldbuttons = g_buttons;
    if ((g_buttons & 1<<BTN_TEST) && (temp & 1<<BTN_TEST)) {
        regidx++;
        if (regidx > 7) regidx = 0;
        
        msg[0] = eCMD_REQUEST_REG;
        msg[1] = registers[regidx];
        msglen = 2;
        bus_send_message(&g_bus, get_receiver(1), msglen, msg);
    }
    else if ((g_buttons & 1<<BTN_EXP) && (temp & 1<<BTN_EXP)) {
        if (light)  light = 0;
        else        light = 1;
        
        msg[0] = eCMD_SET_REG_8BIT;
        msg[1] = MOD_eReg_FirstAppSpecific;
        msg[2] = light;
        msglen = 3;
        bus_send_message(&g_bus, get_receiver(1), msglen, msg);
    }
}
static void send_temperature(uint16_t temperature)
{
    uint8_t msg[4];
    msg[0] = eCMD_STATE_16BIT;
    msg[1] = APP_eReg_CurrentTemperature;
    msg[2] = temperature >> 8;
    msg[3] = temperature & 0x00FF;
    bus_send_message(&g_bus, 0x0000, 4, msg);
}