repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_stop_timer
static void led_driver_stop_timer() { LL_TIM_DisableCounter(TIM2); LL_TIM_DisableUpdateEvent(TIM2); LL_TIM_DisableDMAReq_UPDATE(TIM2); furi_hal_bus_disable(FuriHalBusTIM2); }
/** * @brief Stops the timer for led transitions. * @param led_driver The led driver to initialize. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/flipboard/modules/js_rgbleds/led_driver.c#L185-L190
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_spin_lock
static void led_driver_spin_lock(LedDriver* led_driver) { const uint32_t prev_timer = DWT->CYCCNT; const uint32_t wait_time = LED_DRIVER_SETINEL_WAIT_MS * SystemCoreClock / 1000; do { /* Make sure it's started (allow 100 ticks), but then check for sentinel value. */ if(TIM2->ARR == LED_DRIVER_TIMER_SETINEL && DWT->CYCCNT - prev_timer > 100) { break; } // We should have seen it above, but just in case we also have a timeout. if((DWT->CYCCNT - prev_timer > wait_time)) { FURI_LOG_E( TAG, "0x%04x not found (ARR 0x%08lx, read %lu)", LED_DRIVER_TIMER_SETINEL, TIM2->ARR, led_driver->read_pos); led_driver->read_pos = led_driver->write_pos - 1; break; } } while(true); }
/** * @brief Waits for the DMA to complete. * @param led_driver The led driver to use. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/flipboard/modules/js_rgbleds/led_driver.c#L196-L218
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_transmit
void led_driver_transmit(LedDriver* led_driver) { furi_assert(led_driver); furi_assert(!led_driver->read_pos); furi_assert(!led_driver->write_pos); furi_hal_gpio_init(led_driver->gpio, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); furi_hal_gpio_write(led_driver->gpio, false); const uint32_t bit_set = led_driver->gpio->pin << GPIO_BSRR_BS0_Pos; const uint32_t bit_reset = led_driver->gpio->pin << GPIO_BSRR_BR0_Pos; // Always start with HIGH led_driver->gpio_buf[0] = bit_set; led_driver->gpio_buf[1] = bit_reset; for(size_t i = 0; i < LED_DRIVER_BUFFER_SIZE; i++) { led_driver->timer_buffer[i] = LED_DRIVER_TIMER_SETINEL; } for(size_t i = 0; i < led_driver->count_leds; i++) { led_driver_add_color(led_driver, led_driver->led_data[i]); } led_driver_add_period(led_driver, LED_DRIVER_TDONE); led_driver->dma_led_transition_timer.NbData = led_driver->write_pos + 1; FURI_CRITICAL_ENTER(); led_driver_start_dma(led_driver); led_driver_start_timer(); led_driver_spin_lock(led_driver); led_driver_stop_timer(); led_driver_stop_dma(); FURI_CRITICAL_EXIT(); memset(led_driver->timer_buffer, LED_DRIVER_TIMER_SETINEL, LED_DRIVER_BUFFER_SIZE); led_driver->read_pos = 0; led_driver->write_pos = 0; }
/** * @brief Send the LED data to the LEDs. * @param led_driver The led driver to use. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/flipboard/modules/js_rgbleds/led_driver.c#L259-L299
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_free
void rgbleds_free(RgbLeds* leds) { if(leds->led_driver) { led_driver_free(leds->led_driver); } free(leds->color); free(leds); }
/** * @brief Frees a RgbLeds struct. * @param leds The RgbLeds struct to free. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/flipboard/modules/js_rgbleds/rgbleds.c#L38-L44
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_reset
void rgbleds_reset(RgbLeds* leds) { for(int i = 0; i < leds->num_leds; i++) { leds->color[i] = 0x000000; } }
/** * @brief Resets the LEDs to their default color pattern (off). * @details This method resets the LEDs data to their default color pattern (off). * You must still call rgbleds_update to update the LEDs. * @param leds The RgbLeds struct to reset. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/flipboard/modules/js_rgbleds/rgbleds.c#L52-L56
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_set
bool rgbleds_set(RgbLeds* leds, uint16_t led, uint32_t color) { if(led > leds->num_leds) { return false; } leds->color[led] = color; return true; }
/** * @brief Sets the color of the LEDs. * @details This method sets the color of the LEDs. * @param leds The RgbLeds struct to set the color of. * @param led The LED index to set the color of. * @param color The color to set the LED to (Hex value: RRGGBB). * @return True if the LED was set, false if the LED was out of range. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/flipboard/modules/js_rgbleds/rgbleds.c#L66-L73
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_get
uint32_t rgbleds_get(RgbLeds* leds, uint16_t led) { if(led > leds->num_leds) { return 0; } return leds->color[led]; }
/** * @brief Gets the color of the LEDs. * @details This method gets the color of the LEDs. * @param leds The RgbLeds struct to get the color of. * @param led The LED index to get the color of. * @return The color of the LED (Hex value: RRGGBB). */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/flipboard/modules/js_rgbleds/rgbleds.c#L82-L88
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_set_brightness
void rgbleds_set_brightness(RgbLeds* leds, uint8_t brightness) { leds->brightness = brightness; }
/** * @brief Sets the brightness of the LEDs. * @details This method sets the brightness of the LEDs. * @param leds The RgbLeds struct to set the brightness of. * @param brightness The brightness to set the LEDs to (0-255). */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/flipboard/modules/js_rgbleds/rgbleds.c#L96-L98
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
adjust_color_brightness
static uint32_t adjust_color_brightness(uint32_t color, uint8_t brightness) { uint32_t red = (color & 0xFF0000) >> 16; uint32_t green = (color & 0x00FF00) >> 8; uint32_t blue = (color & 0x0000FF); red = (red * brightness) / 255; green = (green * brightness) / 255; blue = (blue * brightness) / 255; return (red << 16) | (green << 8) | blue; }
/** * @brief Adjusts the brightness of a color. * @details This method adjusts the brightness of a color. * @param color The color to adjust. * @param brightness The brightness to adjust the color to (0-255). * @return The adjusted color. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/flipboard/modules/js_rgbleds/rgbleds.c#L107-L117
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_update
void rgbleds_update(RgbLeds* leds) { for(int i = 0; i < leds->num_leds; i++) { uint32_t color = adjust_color_brightness(leds->color[i], leds->brightness); led_driver_set_led(leds->led_driver, i, color); } led_driver_transmit(leds->led_driver); }
/** * @brief Updates the LEDs. * @details This method changes the LEDs to the colors set by rgbleds_set. * @param leds The RgbLeds struct to update. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/flipboard/modules/js_rgbleds/rgbleds.c#L124-L131
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
imu_thread
static int32_t imu_thread(void* context) { furi_assert(context); ImuThread* imu = context; // float yaw_last = 0.f; // float pitch_last = 0.f; // float diff_x = 0.f; // float diff_y = 0.f; calibrate_gyro(imu); icm42688p_accel_config(imu->icm42688p, AccelFullScale16G, ACCEL_GYRO_RATE); icm42688p_gyro_config(imu->icm42688p, GyroFullScale2000DPS, ACCEL_GYRO_RATE); imu->processed_data.q0 = 1.f; imu->processed_data.q1 = 0.f; imu->processed_data.q2 = 0.f; imu->processed_data.q3 = 0.f; icm42688_fifo_enable(imu->icm42688p, imu_irq_callback, imu); while(1) { uint32_t events = furi_thread_flags_wait(FLAGS_ALL, FuriFlagWaitAny, FuriWaitForever); if(events & ImuStop) { break; } if(events & ImuNewData) { uint16_t data_pending = icm42688_fifo_get_count(imu->icm42688p); ICM42688PFifoPacket data; while(data_pending--) { icm42688_fifo_read(imu->icm42688p, &data); imu_process_data(imu, &data); } } } icm42688_fifo_disable(imu->icm42688p); return 0; }
// static float imu_angle_diff(float a, float b) { // float diff = a - b; // if(diff > 180.f) // diff -= 360.f; // else if(diff < -180.f) // diff += 360.f; // return diff; // }
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/vgm_sensor/js_vgm/imu.c#L126-L166
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
imu_madgwick_filter
static void imu_madgwick_filter( ImuProcessedData* out, ICM42688PScaledData* accel, ICM42688PScaledData* gyro) { float recipNorm; float s0, s1, s2, s3; float qDot1, qDot2, qDot3, qDot4; float _2q0, _2q1, _2q2, _2q3, _4q0, _4q1, _4q2, _8q1, _8q2, q0q0, q1q1, q2q2, q3q3; // Rate of change of quaternion from gyroscope qDot1 = 0.5f * (-out->q1 * gyro->x - out->q2 * gyro->y - out->q3 * gyro->z); qDot2 = 0.5f * (out->q0 * gyro->x + out->q2 * gyro->z - out->q3 * gyro->y); qDot3 = 0.5f * (out->q0 * gyro->y - out->q1 * gyro->z + out->q3 * gyro->x); qDot4 = 0.5f * (out->q0 * gyro->z + out->q1 * gyro->y - out->q2 * gyro->x); // Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation) if(!((accel->x == 0.0f) && (accel->y == 0.0f) && (accel->z == 0.0f))) { // Normalise accelerometer measurement recipNorm = imu_inv_sqrt(accel->x * accel->x + accel->y * accel->y + accel->z * accel->z); accel->x *= recipNorm; accel->y *= recipNorm; accel->z *= recipNorm; // Auxiliary variables to avoid repeated arithmetic _2q0 = 2.0f * out->q0; _2q1 = 2.0f * out->q1; _2q2 = 2.0f * out->q2; _2q3 = 2.0f * out->q3; _4q0 = 4.0f * out->q0; _4q1 = 4.0f * out->q1; _4q2 = 4.0f * out->q2; _8q1 = 8.0f * out->q1; _8q2 = 8.0f * out->q2; q0q0 = out->q0 * out->q0; q1q1 = out->q1 * out->q1; q2q2 = out->q2 * out->q2; q3q3 = out->q3 * out->q3; // Gradient decent algorithm corrective step s0 = _4q0 * q2q2 + _2q2 * accel->x + _4q0 * q1q1 - _2q1 * accel->y; s1 = _4q1 * q3q3 - _2q3 * accel->x + 4.0f * q0q0 * out->q1 - _2q0 * accel->y - _4q1 + _8q1 * q1q1 + _8q1 * q2q2 + _4q1 * accel->z; s2 = 4.0f * q0q0 * out->q2 + _2q0 * accel->x + _4q2 * q3q3 - _2q3 * accel->y - _4q2 + _8q2 * q1q1 + _8q2 * q2q2 + _4q2 * accel->z; s3 = 4.0f * q1q1 * out->q3 - _2q1 * accel->x + 4.0f * q2q2 * out->q3 - _2q2 * accel->y; recipNorm = imu_inv_sqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude s0 *= recipNorm; s1 *= recipNorm; s2 *= recipNorm; s3 *= recipNorm; // Apply feedback step qDot1 -= FILTER_BETA * s0; qDot2 -= FILTER_BETA * s1; qDot3 -= FILTER_BETA * s2; qDot4 -= FILTER_BETA * s3; } // Integrate rate of change of quaternion to yield quaternion out->q0 += qDot1 * (1.0f / FILTER_SAMPLE_FREQ); out->q1 += qDot2 * (1.0f / FILTER_SAMPLE_FREQ); out->q2 += qDot3 * (1.0f / FILTER_SAMPLE_FREQ); out->q3 += qDot4 * (1.0f / FILTER_SAMPLE_FREQ); // Normalise quaternion recipNorm = imu_inv_sqrt( out->q0 * out->q0 + out->q1 * out->q1 + out->q2 * out->q2 + out->q3 * out->q3); out->q0 *= recipNorm; out->q1 *= recipNorm; out->q2 *= recipNorm; out->q3 *= recipNorm; }
/* Simple madgwik filter, based on: https://github.com/arduino-libraries/MadgwickAHRS/ */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js-old/vgm_sensor/js_vgm/imu.c#L201-L273
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_init_dma_gpio_update
static void led_driver_init_dma_gpio_update(LedDriver* led_driver, const GpioPin* gpio) { led_driver->gpio = gpio; // Memory to Peripheral led_driver->dma_gpio_update.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH; // Peripheral (GPIO - We populate GPIO port's BSRR register) led_driver->dma_gpio_update.PeriphOrM2MSrcAddress = (uint32_t)&gpio->port->BSRR; led_driver->dma_gpio_update.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT; led_driver->dma_gpio_update.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD; // Memory (State to set GPIO) led_driver->dma_gpio_update.MemoryOrM2MDstAddress = (uint32_t)led_driver->gpio_buf; led_driver->dma_gpio_update.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT; led_driver->dma_gpio_update.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_WORD; // Data led_driver->dma_gpio_update.Mode = LL_DMA_MODE_CIRCULAR; led_driver->dma_gpio_update.NbData = 2; // We cycle between two (HIGH/LOW)values // When to perform data exchange led_driver->dma_gpio_update.PeriphRequest = LL_DMAMUX_REQ_TIM2_UP; led_driver->dma_gpio_update.Priority = LL_DMA_PRIORITY_VERYHIGH; }
/** * @brief Initializes the DMA for GPIO pin toggle via BSRR. * @param led_driver The led driver to initialize. * @param gpio The GPIO pin to toggle. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/led_driver.c#L25-L44
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_init_dma_led_transition_timer
static void led_driver_init_dma_led_transition_timer(LedDriver* led_driver) { // Timer that triggers based on user data. led_driver->dma_led_transition_timer.Direction = LL_DMA_DIRECTION_MEMORY_TO_PERIPH; // Peripheral (Timer - We populate TIM2's ARR register) led_driver->dma_led_transition_timer.PeriphOrM2MSrcAddress = (uint32_t)&TIM2->ARR; led_driver->dma_led_transition_timer.PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT; led_driver->dma_led_transition_timer.PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_WORD; // Memory (Timings) led_driver->dma_led_transition_timer.MemoryOrM2MDstAddress = (uint32_t)led_driver->timer_buffer; led_driver->dma_led_transition_timer.MemoryOrM2MDstIncMode = LL_DMA_MEMORY_INCREMENT; led_driver->dma_led_transition_timer.MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_HALFWORD; // Data led_driver->dma_led_transition_timer.Mode = LL_DMA_MODE_NORMAL; led_driver->dma_led_transition_timer.NbData = LED_DRIVER_BUFFER_SIZE; // When to perform data exchange led_driver->dma_led_transition_timer.PeriphRequest = LL_DMAMUX_REQ_TIM2_UP; led_driver->dma_led_transition_timer.Priority = LL_DMA_PRIORITY_HIGH; }
/** * @brief Initializes the DMA for the LED timings via ARR. * @param led_driver The led driver to initialize. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/led_driver.c#L50-L68
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_free
void led_driver_free(LedDriver* led_driver) { furi_assert(led_driver); furi_hal_gpio_init_simple(led_driver->gpio, GpioModeAnalog); free(led_driver->led_data); free(led_driver); }
/** * @brief Frees a led driver. * @details Frees a led driver. * @param led_driver The led driver to free. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/led_driver.c#L94-L100
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_set_led
uint32_t led_driver_set_led(LedDriver* led_driver, uint32_t index, uint32_t rrggbb) { furi_assert(led_driver); if(index >= led_driver->count_leds) { return 0xFFFFFFFF; } uint32_t previous = led_driver->led_data[index]; led_driver->led_data[index] = rrggbb; return previous; }
/** * @brief Sets the LED at the given index to the given color. * @note You must still call led_driver_transmit to actually update the LEDs. * @param led_driver The led driver to use. * @param index The index of the LED to set. * @param rrggbb The color to set the LED to (0xrrggbb format). * @return The previous color of the LED (0xrrggbb format). */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/led_driver.c#L110-L119
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_get_led
uint32_t led_driver_get_led(LedDriver* led_driver, uint32_t index) { furi_assert(led_driver); if(index >= led_driver->count_leds) { return 0xFFFFFFFF; } return led_driver->led_data[index]; }
/** * @brief Gets the LED at the given index. * @param led_driver The led driver to use. * @param index The index of the LED to get. * @return The color of the LED (0xrrggbb format). */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/led_driver.c#L127-L134
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_start_dma
static void led_driver_start_dma(LedDriver* led_driver) { furi_assert(led_driver); LL_DMA_Init(DMA1, LL_DMA_CHANNEL_1, &led_driver->dma_gpio_update); LL_DMA_Init(DMA1, LL_DMA_CHANNEL_2, &led_driver->dma_led_transition_timer); LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_1); LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_2); }
/** * @brief Initializes the DMA for GPIO pin toggle and led transititions. * @param led_driver The led driver to initialize. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/led_driver.c#L140-L148
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_stop_dma
static void led_driver_stop_dma() { LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_1); LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_2); LL_DMA_ClearFlag_TC1(DMA1); LL_DMA_ClearFlag_TC2(DMA1); }
/** * @brief Stops the DMA for GPIO pin toggle and led transititions. * @param led_driver The led driver to initialize. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/led_driver.c#L154-L159
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_start_timer
static void led_driver_start_timer() { furi_hal_bus_enable(FuriHalBusTIM2); LL_TIM_SetCounterMode(TIM2, LL_TIM_COUNTERMODE_UP); LL_TIM_SetClockDivision(TIM2, LL_TIM_CLOCKDIVISION_DIV1); LL_TIM_SetPrescaler(TIM2, 0); // Updated by led_driver->dma_led_transition_timer.PeriphOrM2MSrcAddress LL_TIM_SetAutoReload(TIM2, LED_DRIVER_TIMER_SETINEL); LL_TIM_SetCounter(TIM2, 0); LL_TIM_EnableCounter(TIM2); LL_TIM_EnableUpdateEvent(TIM2); LL_TIM_EnableDMAReq_UPDATE(TIM2); LL_TIM_GenerateEvent_UPDATE(TIM2); }
/** * @brief Starts the timer for led transitions. * @param led_driver The led driver to initialize. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/led_driver.c#L165-L179
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_stop_timer
static void led_driver_stop_timer() { LL_TIM_DisableCounter(TIM2); LL_TIM_DisableUpdateEvent(TIM2); LL_TIM_DisableDMAReq_UPDATE(TIM2); furi_hal_bus_disable(FuriHalBusTIM2); }
/** * @brief Stops the timer for led transitions. * @param led_driver The led driver to initialize. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/led_driver.c#L185-L190
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_spin_lock
static void led_driver_spin_lock(LedDriver* led_driver) { const uint32_t prev_timer = DWT->CYCCNT; const uint32_t wait_time = LED_DRIVER_SETINEL_WAIT_MS * SystemCoreClock / 1000; do { /* Make sure it's started (allow 100 ticks), but then check for sentinel value. */ if(TIM2->ARR == LED_DRIVER_TIMER_SETINEL && DWT->CYCCNT - prev_timer > 100) { break; } // We should have seen it above, but just in case we also have a timeout. if((DWT->CYCCNT - prev_timer > wait_time)) { FURI_LOG_E( TAG, "0x%04x not found (ARR 0x%08lx, read %lu)", LED_DRIVER_TIMER_SETINEL, TIM2->ARR, led_driver->read_pos); led_driver->read_pos = led_driver->write_pos - 1; break; } } while(true); }
/** * @brief Waits for the DMA to complete. * @param led_driver The led driver to use. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/led_driver.c#L196-L218
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
led_driver_transmit
void led_driver_transmit(LedDriver* led_driver) { furi_assert(led_driver); furi_assert(!led_driver->read_pos); furi_assert(!led_driver->write_pos); furi_hal_gpio_init(led_driver->gpio, GpioModeOutputPushPull, GpioPullNo, GpioSpeedVeryHigh); furi_hal_gpio_write(led_driver->gpio, false); const uint32_t bit_set = led_driver->gpio->pin << GPIO_BSRR_BS0_Pos; const uint32_t bit_reset = led_driver->gpio->pin << GPIO_BSRR_BR0_Pos; // Always start with HIGH led_driver->gpio_buf[0] = bit_set; led_driver->gpio_buf[1] = bit_reset; for(size_t i = 0; i < LED_DRIVER_BUFFER_SIZE; i++) { led_driver->timer_buffer[i] = LED_DRIVER_TIMER_SETINEL; } for(size_t i = 0; i < led_driver->count_leds; i++) { led_driver_add_color(led_driver, led_driver->led_data[i]); } led_driver_add_period(led_driver, LED_DRIVER_TDONE); led_driver->dma_led_transition_timer.NbData = led_driver->write_pos + 1; FURI_CRITICAL_ENTER(); led_driver_start_dma(led_driver); led_driver_start_timer(); led_driver_spin_lock(led_driver); led_driver_stop_timer(); led_driver_stop_dma(); FURI_CRITICAL_EXIT(); memset(led_driver->timer_buffer, LED_DRIVER_TIMER_SETINEL, LED_DRIVER_BUFFER_SIZE); led_driver->read_pos = 0; led_driver->write_pos = 0; }
/** * @brief Send the LED data to the LEDs. * @param led_driver The led driver to use. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/led_driver.c#L259-L299
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_free
void rgbleds_free(RgbLeds* leds) { if(leds->led_driver) { led_driver_free(leds->led_driver); } free(leds->color); free(leds); }
/** * @brief Frees a RgbLeds struct. * @param leds The RgbLeds struct to free. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/rgbleds.c#L38-L44
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_reset
void rgbleds_reset(RgbLeds* leds) { for(int i = 0; i < leds->num_leds; i++) { leds->color[i] = 0x000000; } }
/** * @brief Resets the LEDs to their default color pattern (off). * @details This method resets the LEDs data to their default color pattern (off). * You must still call rgbleds_update to update the LEDs. * @param leds The RgbLeds struct to reset. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/rgbleds.c#L52-L56
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_set
bool rgbleds_set(RgbLeds* leds, uint16_t led, uint32_t color) { if(led > leds->num_leds) { return false; } leds->color[led] = color; return true; }
/** * @brief Sets the color of the LEDs. * @details This method sets the color of the LEDs. * @param leds The RgbLeds struct to set the color of. * @param led The LED index to set the color of. * @param color The color to set the LED to (Hex value: RRGGBB). * @return True if the LED was set, false if the LED was out of range. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/rgbleds.c#L66-L73
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_get
uint32_t rgbleds_get(RgbLeds* leds, uint16_t led) { if(led > leds->num_leds) { return 0; } return leds->color[led]; }
/** * @brief Gets the color of the LEDs. * @details This method gets the color of the LEDs. * @param leds The RgbLeds struct to get the color of. * @param led The LED index to get the color of. * @return The color of the LED (Hex value: RRGGBB). */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/rgbleds.c#L82-L88
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_set_brightness
void rgbleds_set_brightness(RgbLeds* leds, uint8_t brightness) { leds->brightness = brightness; }
/** * @brief Sets the brightness of the LEDs. * @details This method sets the brightness of the LEDs. * @param leds The RgbLeds struct to set the brightness of. * @param brightness The brightness to set the LEDs to (0-255). */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/rgbleds.c#L96-L98
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
adjust_color_brightness
static uint32_t adjust_color_brightness(uint32_t color, uint8_t brightness) { uint32_t red = (color & 0xFF0000) >> 16; uint32_t green = (color & 0x00FF00) >> 8; uint32_t blue = (color & 0x0000FF); red = (red * brightness) / 255; green = (green * brightness) / 255; blue = (blue * brightness) / 255; return (red << 16) | (green << 8) | blue; }
/** * @brief Adjusts the brightness of a color. * @details This method adjusts the brightness of a color. * @param color The color to adjust. * @param brightness The brightness to adjust the color to (0-255). * @return The adjusted color. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/rgbleds.c#L107-L117
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rgbleds_update
void rgbleds_update(RgbLeds* leds) { for(int i = 0; i < leds->num_leds; i++) { uint32_t color = adjust_color_brightness(leds->color[i], leds->brightness); led_driver_set_led(leds->led_driver, i, color); } led_driver_transmit(leds->led_driver); }
/** * @brief Updates the LEDs. * @details This method changes the LEDs to the colors set by rgbleds_set. * @param leds The RgbLeds struct to update. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/flipboard/modules/js_rgbleds/rgbleds.c#L124-L131
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
imu_thread
static int32_t imu_thread(void* context) { furi_assert(context); ImuThread* imu = context; // float yaw_last = 0.f; // float pitch_last = 0.f; // float diff_x = 0.f; // float diff_y = 0.f; calibrate_gyro(imu); icm42688p_accel_config(imu->icm42688p, AccelFullScale16G, ACCEL_GYRO_RATE); icm42688p_gyro_config(imu->icm42688p, GyroFullScale2000DPS, ACCEL_GYRO_RATE); imu->processed_data.q0 = 1.f; imu->processed_data.q1 = 0.f; imu->processed_data.q2 = 0.f; imu->processed_data.q3 = 0.f; icm42688_fifo_enable(imu->icm42688p, imu_irq_callback, imu); while(1) { uint32_t events = furi_thread_flags_wait(FLAGS_ALL, FuriFlagWaitAny, FuriWaitForever); if(events & ImuStop) { break; } if(events & ImuNewData) { uint16_t data_pending = icm42688_fifo_get_count(imu->icm42688p); ICM42688PFifoPacket data; while(data_pending--) { icm42688_fifo_read(imu->icm42688p, &data); imu_process_data(imu, &data); } } } icm42688_fifo_disable(imu->icm42688p); return 0; }
// static float imu_angle_diff(float a, float b) { // float diff = a - b; // if(diff > 180.f) // diff -= 360.f; // else if(diff < -180.f) // diff += 360.f; // return diff; // }
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/vgm_sensor/js_vgm/imu.c#L126-L166
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
imu_madgwick_filter
static void imu_madgwick_filter( ImuProcessedData* out, ICM42688PScaledData* accel, ICM42688PScaledData* gyro) { float recipNorm; float s0, s1, s2, s3; float qDot1, qDot2, qDot3, qDot4; float _2q0, _2q1, _2q2, _2q3, _4q0, _4q1, _4q2, _8q1, _8q2, q0q0, q1q1, q2q2, q3q3; // Rate of change of quaternion from gyroscope qDot1 = 0.5f * (-out->q1 * gyro->x - out->q2 * gyro->y - out->q3 * gyro->z); qDot2 = 0.5f * (out->q0 * gyro->x + out->q2 * gyro->z - out->q3 * gyro->y); qDot3 = 0.5f * (out->q0 * gyro->y - out->q1 * gyro->z + out->q3 * gyro->x); qDot4 = 0.5f * (out->q0 * gyro->z + out->q1 * gyro->y - out->q2 * gyro->x); // Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation) if(!((accel->x == 0.0f) && (accel->y == 0.0f) && (accel->z == 0.0f))) { // Normalise accelerometer measurement recipNorm = imu_inv_sqrt(accel->x * accel->x + accel->y * accel->y + accel->z * accel->z); accel->x *= recipNorm; accel->y *= recipNorm; accel->z *= recipNorm; // Auxiliary variables to avoid repeated arithmetic _2q0 = 2.0f * out->q0; _2q1 = 2.0f * out->q1; _2q2 = 2.0f * out->q2; _2q3 = 2.0f * out->q3; _4q0 = 4.0f * out->q0; _4q1 = 4.0f * out->q1; _4q2 = 4.0f * out->q2; _8q1 = 8.0f * out->q1; _8q2 = 8.0f * out->q2; q0q0 = out->q0 * out->q0; q1q1 = out->q1 * out->q1; q2q2 = out->q2 * out->q2; q3q3 = out->q3 * out->q3; // Gradient decent algorithm corrective step s0 = _4q0 * q2q2 + _2q2 * accel->x + _4q0 * q1q1 - _2q1 * accel->y; s1 = _4q1 * q3q3 - _2q3 * accel->x + 4.0f * q0q0 * out->q1 - _2q0 * accel->y - _4q1 + _8q1 * q1q1 + _8q1 * q2q2 + _4q1 * accel->z; s2 = 4.0f * q0q0 * out->q2 + _2q0 * accel->x + _4q2 * q3q3 - _2q3 * accel->y - _4q2 + _8q2 * q1q1 + _8q2 * q2q2 + _4q2 * accel->z; s3 = 4.0f * q1q1 * out->q3 - _2q1 * accel->x + 4.0f * q2q2 * out->q3 - _2q2 * accel->y; recipNorm = imu_inv_sqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude s0 *= recipNorm; s1 *= recipNorm; s2 *= recipNorm; s3 *= recipNorm; // Apply feedback step qDot1 -= FILTER_BETA * s0; qDot2 -= FILTER_BETA * s1; qDot3 -= FILTER_BETA * s2; qDot4 -= FILTER_BETA * s3; } // Integrate rate of change of quaternion to yield quaternion out->q0 += qDot1 * (1.0f / FILTER_SAMPLE_FREQ); out->q1 += qDot2 * (1.0f / FILTER_SAMPLE_FREQ); out->q2 += qDot3 * (1.0f / FILTER_SAMPLE_FREQ); out->q3 += qDot4 * (1.0f / FILTER_SAMPLE_FREQ); // Normalise quaternion recipNorm = imu_inv_sqrt( out->q0 * out->q0 + out->q1 * out->q1 + out->q2 * out->q2 + out->q3 * out->q3); out->q0 *= recipNorm; out->q1 *= recipNorm; out->q2 *= recipNorm; out->q3 *= recipNorm; }
/* Simple madgwik filter, based on: https://github.com/arduino-libraries/MadgwickAHRS/ */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/js/vgm_sensor/js_vgm/imu.c#L201-L273
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
basic_demo_input_callback
static void basic_demo_input_callback(InputEvent* input_event, FuriMessageQueue* queue) { furi_assert(queue); DemoEvent event = {.type = DemoEventTypeKey, .input = *input_event}; furi_message_queue_put(queue, &event, FuriWaitForever); }
// Invoked when input (button press) is detected. We queue a message and then return to the caller.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/basic/basic_view_port_demo_app.c#L45-L49
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
basic_demo_render_callback
static void basic_demo_render_callback(Canvas* canvas, void* ctx) { // Attempt to aquire context, so we can read the data. DemoContext* demo_context = ctx; if(furi_mutex_acquire(demo_context->mutex, 200) != FuriStatusOk) { return; } DemoData* data = demo_context->data; furi_string_printf(data->buffer, "Basic ViewPort demo"); canvas_set_font(canvas, FontPrimary); canvas_draw_str_aligned( canvas, 15, 25, AlignLeft, AlignTop, furi_string_get_cstr(data->buffer)); furi_string_printf(data->buffer, "OK pressed"); furi_string_cat_printf(data->buffer, " %ld times.", data->counter); canvas_set_font(canvas, FontSecondary); canvas_draw_str_aligned( canvas, 15, 40, AlignLeft, AlignTop, furi_string_get_cstr(data->buffer)); // Release the context, so other threads can update the data. furi_mutex_release(demo_context->mutex); }
// Invoked by the draw callback to render the screen. We render our UI on the callback thread.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/basic/basic_view_port_demo_app.c#L52-L74
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
message
void message(char* message) { FURI_LOG_I(TAG, message); furi_delay_ms(10); }
///////////////////////////////////////////////////////////////// // Routine for logging messages with a delay. /////////////////////////////////////////////////////////////////
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/knob_component/knob_demo_app.c#L29-L32
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_set_callback
static void knob_set_callback(Knob* knob, KnobCallback callback, void* callback_context) { with_view_model( knob->view, KnobModel * model, { model->callback_context = callback_context; model->callback = callback; }, true); }
// Set a callback to invoke when knob has an event. // @knob is a pointer to our Knob instance. // @callback is a function to invoke when we have custom events.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/knob_component/knob_demo_app.c#L59-L68
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_input_callback
static bool knob_input_callback(InputEvent* input_event, void* ctx) { message("knob_input_callback"); Knob* knob = (Knob*)ctx; bool handled = false; if(input_event->type == InputTypePress && input_event->key == InputKeyUp) { bool updated = false; with_view_model( knob->view, KnobModel * model, { if(model->counter) { model->counter--; updated = true; } }, updated); handled = true; } else if(input_event->type == InputTypePress && input_event->key == InputKeyDown) { with_view_model( knob->view, KnobModel * model, { model->counter++; }, true); // Render new data. handled = true; } else if(input_event->type == InputTypePress && input_event->key == InputKeyOk) { with_view_model( knob->view, KnobModel * model, { if(model->callback) { message("invoking callback"); model->callback(model->callback_context, KnobEventDone); } else { message("no callback set; use knob_set_callback first."); } }, false); // No new data. handled = true; } return handled; }
// Invoked when input (button press) is detected. // @input_even is the event the occured. // @ctx is a pointer to our Knob instance.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/knob_component/knob_demo_app.c#L73-L116
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_render_callback
static void knob_render_callback(Canvas* canvas, void* ctx) { message("knob_render_callback"); KnobModel* model = ctx; furi_string_printf(model->buffer, "Knob demo %d", model->counter); canvas_set_font(canvas, FontPrimary); if(model->heading) { canvas_draw_str_aligned(canvas, 64, 5, AlignCenter, AlignTop, model->heading); } canvas_draw_str_aligned( canvas, 15, 30, AlignLeft, AlignTop, furi_string_get_cstr(model->buffer)); }
// Invoked by the draw callback to render the knob. // @canvas is the canvas to draw on. // @ctx is our model.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/knob_component/knob_demo_app.c#L121-L135
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_free
void knob_free(Knob* knob) { message("knob_free"); furi_assert(knob); with_view_model( knob->view, KnobModel * model, { furi_string_free(model->buffer); }, true); view_free(knob->view); free(knob); }
// Free a Knob instance. // @knob pointer to a Knob instance.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/knob_component/knob_demo_app.c#L165-L172
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_get_counter
uint32_t knob_get_counter(Knob* knob) { message("knob_get_counter"); furi_assert(knob); uint32_t value = 0; with_view_model( knob->view, KnobModel * model, { value = model->counter; }, false); return value; }
// Gets the current counter value for a given Knob instance. // @knob pointer to a Knob instance.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/knob_component/knob_demo_app.c#L184-L193
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_set_counter
void knob_set_counter(Knob* knob, uint32_t count) { with_view_model( knob->view, KnobModel * model, { model->counter = count; }, true); }
// Set the counter value for a given Knob instance. // @knob pointer to a Knob instance.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/knob_component/knob_demo_app.c#L197-L200
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_set_heading
void knob_set_heading(Knob* knob, char* heading) { with_view_model( knob->view, KnobModel * model, { model->heading = heading; }, true); }
// Sets the heading for displaying our knob. // @knob pointer to a Knob instance. // @heading the kind of knob.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/knob_component/knob_demo_app.c#L205-L208
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
minimal_viewport_demo_app
int32_t minimal_viewport_demo_app(void* p) { UNUSED(p); FuriMessageQueue* queue = furi_message_queue_alloc(8, sizeof(AppEvent)); ViewPort* view_port = view_port_alloc(); view_port_input_callback_set(view_port, input_callback, queue); view_port_draw_callback_set(view_port, render_callback, NULL); Gui* gui = furi_record_open(RECORD_GUI); gui_add_view_port(gui, view_port, GuiLayerFullscreen); bool processing = true; AppEvent event; do { if(furi_message_queue_get(queue, &event, FuriWaitForever) == FuriStatusOk) { if(event.input.type == InputTypeShort && event.input.key == InputKeyBack) { processing = false; } } } while(processing); view_port_enabled_set(view_port, false); gui_remove_view_port(gui, view_port); view_port_free(view_port); furi_record_close(RECORD_GUI); furi_message_queue_free(queue); return 0; }
//static ViewPort* view_port = view_port_alloc();
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/minimal_viewport/minimal_viewport_app.c#L21-L44
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
message
void message(char* message) { FURI_LOG_I(TAG, message); furi_delay_ms(10); }
///////////////////////////////////////////////////////////////// // Routine for logging messages with a delay. /////////////////////////////////////////////////////////////////
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/scenes/scenes_demo_app.c#L30-L33
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_set_callback
static void knob_set_callback(Knob* knob, KnobCallback callback, void* callback_context) { with_view_model( knob->view, KnobModel * model, { model->callback_context = callback_context; model->callback = callback; }, true); }
// Set a callback to invoke when knob has an event. // @knob is a pointer to our Knob instance. // @callback is a function to invoke when we have custom events.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/scenes/scenes_demo_app.c#L60-L69
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_input_callback
static bool knob_input_callback(InputEvent* input_event, void* ctx) { message("knob_input_callback"); Knob* knob = (Knob*)ctx; bool handled = false; if(input_event->type == InputTypePress && input_event->key == InputKeyUp) { bool updated = false; with_view_model( knob->view, KnobModel * model, { if(model->counter) { model->counter--; updated = true; } }, updated); handled = true; } else if(input_event->type == InputTypePress && input_event->key == InputKeyDown) { with_view_model( knob->view, KnobModel * model, { model->counter++; }, true); // Render new data. handled = true; } else if(input_event->type == InputTypePress && input_event->key == InputKeyOk) { with_view_model( knob->view, KnobModel * model, { if(model->callback) { message("invoking callback"); model->callback(model->callback_context, KnobEventDone); } else { message("no callback set; use knob_set_callback first."); } }, false); // No new data. handled = true; } return handled; }
// Invoked when input (button press) is detected. // @input_even is the event the occured. // @ctx is a pointer to our Knob instance.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/scenes/scenes_demo_app.c#L74-L117
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_render_callback
static void knob_render_callback(Canvas* canvas, void* ctx) { message("knob_render_callback"); KnobModel* model = ctx; furi_string_printf(model->buffer, "Knob demo %d", model->counter); canvas_set_font(canvas, FontPrimary); if(model->heading) { canvas_draw_str_aligned(canvas, 64, 5, AlignCenter, AlignTop, model->heading); } canvas_draw_str_aligned( canvas, 15, 30, AlignLeft, AlignTop, furi_string_get_cstr(model->buffer)); }
// Invoked by the draw callback to render the knob. // @canvas is the canvas to draw on. // @ctx is our model.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/scenes/scenes_demo_app.c#L122-L136
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_free
void knob_free(Knob* knob) { message("knob_free"); furi_assert(knob); with_view_model( knob->view, KnobModel * model, { furi_string_free(model->buffer); }, true); view_free(knob->view); free(knob); }
// Free a Knob instance. // @knob pointer to a Knob instance.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/scenes/scenes_demo_app.c#L166-L173
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_get_counter
uint32_t knob_get_counter(Knob* knob) { message("knob_get_counter"); furi_assert(knob); uint32_t value = 0; with_view_model( knob->view, KnobModel * model, { value = model->counter; }, false); return value; }
// Gets the current counter value for a given Knob instance. // @knob pointer to a Knob instance.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/scenes/scenes_demo_app.c#L185-L194
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_set_counter
void knob_set_counter(Knob* knob, uint32_t count) { with_view_model( knob->view, KnobModel * model, { model->counter = count; }, true); }
// Set the counter value for a given Knob instance. // @knob pointer to a Knob instance.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/scenes/scenes_demo_app.c#L198-L201
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
knob_set_heading
void knob_set_heading(Knob* knob, char* heading) { with_view_model( knob->view, KnobModel * model, { model->heading = heading; }, true); }
// Sets the heading for displaying our knob. // @knob pointer to a Knob instance. // @heading the kind of knob.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/plugins/scenes/scenes_demo_app.c#L206-L209
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
genie_navigation_submenu_callback
uint32_t genie_navigation_submenu_callback(void* context) { UNUSED(context); return GenieViewSubmenu; }
/** * @brief Callback for navigation events * @details This function is called when user press back button. We return VIEW_NONE to * indicate that we want to exit the application. * @param context The context * @return next view id */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/genie_submenu.c#L36-L39
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
genie_navigation_exit_callback
static uint32_t genie_navigation_exit_callback(void* context) { UNUSED(context); return VIEW_NONE; }
/** * @brief Callback for navigation events * @details This function is called when user press back button. We return VIEW_NONE to * indicate that we want to exit the application. * @param context The context * @return next view id */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/genie_submenu.c#L48-L51
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_genie_set_sn_and_btn
static void subghz_protocol_genie_set_sn_and_btn(SubGhzBlockGeneric* instance) { uint64_t key = subghz_protocol_blocks_reverse_key(instance->data, instance->data_count_bit); uint32_t key_fix = key >> 32; instance->serial = key_fix & 0x0FFFFFFF; instance->btn = key_fix >> 28; }
/** * Set serial number and button number from data * @param instance Pointer to a SubGhzBlockGeneric* instance */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/genie.c#L128-L133
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_genie_storage_file_read16
static uint16_t subghz_protocol_genie_storage_file_read16(File* file) { uint16_t read = 0; char buffer[2] = {0}; storage_file_read(file, buffer, 2); read |= (buffer[0] << 8); read |= buffer[1]; return read; }
/** * Read 16-bits from file * @param file Pointer to a File instance * @return 16-bit unsigned integer */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/genie.c#L140-L147
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_genie_storage_file_read32
static uint32_t subghz_protocol_genie_storage_file_read32(File* file) { uint32_t read = 0; char buffer[4] = {0}; storage_file_read(file, buffer, 4); read = (buffer[0] << 24); read |= (buffer[1] << 16); read |= (buffer[2] << 8); read |= buffer[3]; return read; }
/** * Read 32-bits from file * @param file Pointer to a File instance * @return 32-bit unsigned integer */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/genie.c#L154-L163
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_genie_storage_file_write16
static bool subghz_protocol_genie_storage_file_write16(File* file, uint16_t data) { char buffer[2] = {0}; buffer[0] = (data >> 8) & 0xFF; buffer[1] = data & 0xFF; return storage_file_write(file, buffer, 2) == 2; }
/** * Write 16-bits to file * @param file Pointer to a File instance * @param data 16-bit unsigned integer * @return true On success */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/genie.c#L171-L176
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_genie_next_code_from_file
static uint64_t subghz_protocol_genie_next_code_from_file( uint32_t code_low, uint32_t code_high, bool update_index) { Storage* storage = furi_record_open(RECORD_STORAGE); char buffer[256] = {0}; snprintf(buffer, 128, "%s/%08lX%s", GENIE_SAVE_FOLDER, code_low, GENIE_FILE_EXT); uint64_t result = 0xFFFFFFFFFFFFFFFF; File* file = NULL; do { if(!storage) { FURI_LOG_E(TAG, "Failed to access storage"); break; } file = storage_file_alloc(storage); if(!file) { FURI_LOG_E(TAG, "Failed to alloc file"); break; } if(!storage_file_exists(storage, buffer)) { FURI_LOG_E(TAG, "File %s does not exist, rerun Genie Recorder app!", buffer); break; } if(storage_file_open(file, buffer, FSAM_READ_WRITE, FSOM_OPEN_EXISTING)) { if(!storage_file_seek(file, GENIE_VERSION, true)) { FURI_LOG_E(TAG, "Failed to seek to %d", GENIE_VERSION); break; } uint16_t version = subghz_protocol_genie_storage_file_read16(file); if((version >> 8) > GENIE_MAJOR_VERSION) { FURI_LOG_E(TAG, "Unsupported version: %04X", version); break; } if(!storage_file_seek(file, GENIE_SN, true)) { FURI_LOG_E(TAG, "Failed to seek to %d", GENIE_SN); break; } uint32_t low = subghz_protocol_genie_storage_file_read32(file); if(low != code_low) { FURI_LOG_E(TAG, "Btn/SN mismatch. Expected: %08lX, got: %08lX", code_low, low); break; } if(!storage_file_seek(file, GENIE_LAST_SENT, true)) { FURI_LOG_E(TAG, "Failed to seek to %d", GENIE_SN); break; } uint16_t last_sent = subghz_protocol_genie_storage_file_read16(file); last_sent -= last_sent % (COUNT_OF(buffer) / 4); result = code_high; bool found = false; int j = 0; for(int i = 0; i <= 65536; i++) { if(last_sent % (COUNT_OF(buffer) / 4) == 0) { if(!storage_file_seek(file, GENIE_DATA + (last_sent * 4), true)) { FURI_LOG_E(TAG, "Failed to seek to %d", GENIE_DATA + (last_sent * 4)); break; } if(!storage_file_read(file, buffer, COUNT_OF(buffer))) { FURI_LOG_E(TAG, "Failed to read %d", COUNT_OF(buffer)); break; } j = 0; } uint32_t high = (buffer[j++] << 24); high |= (buffer[j++] << 16); high |= (buffer[j++] << 8); high |= buffer[j++]; if(found && high != 0) { result = high; break; } found |= (high == code_high); if(last_sent == 0xFFFF) { last_sent = 0; } else { last_sent++; } } if(found && update_index) { if(!storage_file_seek(file, GENIE_LAST_SENT, true)) { FURI_LOG_E(TAG, "Failed to seek to %d", GENIE_SN); break; } if(!subghz_protocol_genie_storage_file_write16(file, last_sent)) { FURI_LOG_E(TAG, "Failed to write last sent. %d", last_sent); break; } } else if(!found) { FURI_LOG_E(TAG, "Code not found: %08lX", code_high); break; } } else { FURI_LOG_E(TAG, "Failed to open file"); break; } } while(false); if(file) { storage_file_close(file); storage_file_free(file); } furi_record_close(RECORD_STORAGE); return result; }
/** * Finds next code from the .gne file associated with the given code_low. * @param code_low 32-bit unsigned integer (static part of code) * @param code_high 32-bit unsigned integer (dynamic part of code) * @param update_index If true, the index of the last sent code will be updated. * @return 64-bit unsigned integer (next code) or 0xFFFFFFFFFFFFFFFF if not found */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/genie.c#L185-L307
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_genie_find_next_code
static void subghz_protocol_genie_find_next_code(SubGhzProtocolEncoderGenie* instance, bool counter_up) { instance->generic.data_count_bit = 64; instance->generic.cnt = 0x0000; if(counter_up) { uint32_t code_found_hi = instance->generic.data >> 32; uint32_t code_found_lo = instance->generic.data & 0x00000000ffffffff; uint64_t next_code = subghz_protocol_genie_next_code_from_file(code_found_lo, code_found_hi, true); FURI_LOG_D( TAG, "Low: %08lX High: %08lX New-High: %08lX", code_found_lo, code_found_hi, (uint32_t)next_code); instance->generic.cnt = next_code & 0xffff; // We don't know counter value, just use bottom of code. if((next_code & 0xFFFFFFFF) == 0xFFFFFFFF) { instance->generic.data = ((uint64_t)code_found_hi) << 32 | code_found_lo; } else { instance->generic.data = ((uint64_t)next_code) << 32 | code_found_lo; } } }
/** * Finds next code for this remote. * @param instance Pointer to a SubGhzProtocolEncoderGenie* instance * @param counter_up attempt to find next code if the value is true */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/genie.c#L314-L340
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_genie_gen_data
static bool subghz_protocol_genie_gen_data( SubGhzProtocolEncoderGenie* instance, uint8_t btn, bool counter_up) { uint32_t fix = (uint32_t)btn << 28 | instance->generic.serial; uint32_t hop = 0; uint64_t code_found_reverse; subghz_protocol_genie_find_next_code(instance, counter_up); code_found_reverse = subghz_protocol_blocks_reverse_key( instance->generic.data, instance->generic.data_count_bit); hop = code_found_reverse & 0x00000000ffffffff; if(hop) { uint64_t yek = (uint64_t)fix << 32 | hop; instance->generic.data = subghz_protocol_blocks_reverse_key(yek, instance->generic.data_count_bit); } return true; }
/** * Key generation from simple data * @param instance Pointer to a SubGhzProtocolEncoderGenie* instance * @param btn Button number, 4 bit * @param counter_up increasing the counter if the value is true */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/genie.c#L348-L369
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_encoder_genie_get_upload
static bool subghz_protocol_encoder_genie_get_upload( SubGhzProtocolEncoderGenie* instance, uint8_t btn, bool counter_up) { furi_assert(instance); // Generate next key if(!subghz_protocol_genie_gen_data(instance, btn, counter_up)) { return false; } size_t index = 0; size_t size_upload = 11 * 2 + 2 + (instance->generic.data_count_bit * 2) + 4; if(size_upload > instance->encoder.size_upload) { FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer."); return false; } else { instance->encoder.size_upload = size_upload; } //Send header for(uint8_t i = 11; i > 0; i--) { instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)subghz_protocol_genie_const.te_short); instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)subghz_protocol_genie_const.te_short); } instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)subghz_protocol_genie_const.te_short); instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)subghz_protocol_genie_const.te_short * 10); //Send key data for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) { if(bit_read(instance->generic.data, i - 1)) { //send bit 1 instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)subghz_protocol_genie_const.te_short); instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)subghz_protocol_genie_const.te_long); } else { //send bit 0 instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)subghz_protocol_genie_const.te_long); instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)subghz_protocol_genie_const.te_short); } } // +send 2 status bit instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)subghz_protocol_genie_const.te_short); instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)subghz_protocol_genie_const.te_long); // send end instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)subghz_protocol_genie_const.te_short); instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)subghz_protocol_genie_const.te_short * 40); return true; }
/** * Generating an upload from data. * @param instance Pointer to a SubGhzProtocolEncoderGenie instance * @return true On success */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/genie.c#L376-L436
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_keeloq_common_encrypt
inline uint32_t subghz_protocol_keeloq_common_encrypt(const uint32_t data, const uint64_t key) { uint32_t x = data, r; for(r = 0; r < 528; r++) x = (x >> 1) ^ ((bit(x, 0) ^ bit(x, 16) ^ (uint32_t)bit(key, r & 63) ^ bit(KEELOQ_NLF, g5(x, 1, 9, 20, 26, 31))) << 31); return x; }
/** Simple Learning Encrypt * @param data - 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter * @param key - manufacture (64bit) * @return keeloq encrypt data */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/keeloq_common.c#L15-L22
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_keeloq_common_decrypt
inline uint32_t subghz_protocol_keeloq_common_decrypt(const uint32_t data, const uint64_t key) { uint32_t x = data, r; for(r = 0; r < 528; r++) x = (x << 1) ^ bit(x, 31) ^ bit(x, 15) ^ (uint32_t)bit(key, (15 - r) & 63) ^ bit(KEELOQ_NLF, g5(x, 0, 8, 19, 25, 30)); return x; }
/** Simple Learning Decrypt * @param data - keeloq encrypt data * @param key - manufacture (64bit) * @return 0xBSSSCCCC, B(4bit) key, S(10bit) serial&0x3FF, C(16bit) counter */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/keeloq_common.c#L29-L35
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_keeloq_common_normal_learning
inline uint64_t subghz_protocol_keeloq_common_normal_learning(uint32_t data, const uint64_t key) { uint32_t k1, k2; data &= 0x0FFFFFFF; data |= 0x20000000; k1 = subghz_protocol_keeloq_common_decrypt(data, key); data &= 0x0FFFFFFF; data |= 0x60000000; k2 = subghz_protocol_keeloq_common_decrypt(data, key); return ((uint64_t)k2 << 32) | k1; // key - shifrovanoya }
/** Normal Learning * @param data - serial number (28bit) * @param key - manufacture (64bit) * @return manufacture for this serial number (64bit) */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/keeloq_common.c#L42-L54
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_keeloq_common_secure_learning
inline uint64_t subghz_protocol_keeloq_common_secure_learning( uint32_t data, uint32_t seed, const uint64_t key) { uint32_t k1, k2; data &= 0x0FFFFFFF; k1 = subghz_protocol_keeloq_common_decrypt(data, key); k2 = subghz_protocol_keeloq_common_decrypt(seed, key); return ((uint64_t)k1 << 32) | k2; }
/** Secure Learning * @param data - serial number (28bit) * @param seed - seed number (32bit) * @param key - manufacture (64bit) * @return manufacture for this serial number (64bit) */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/keeloq_common.c#L63-L74
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_keeloq_common_magic_xor_type1_learning
inline uint64_t subghz_protocol_keeloq_common_magic_xor_type1_learning(uint32_t data, uint64_t xor) { data &= 0x0FFFFFFF; return (((uint64_t)data << 32) | data) ^ xor; }
/** Magic_xor_type1 Learning * @param data - serial number (28bit) * @param xor - magic xor (64bit) * @return manufacture for this serial number (64bit) */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/keeloq_common.c#L82-L86
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_keeloq_common_magic_serial_type1_learning
inline uint64_t subghz_protocol_keeloq_common_magic_serial_type1_learning(uint32_t data, uint64_t man) { return (man & 0xFFFFFFFF) | ((uint64_t)data << 40) | ((uint64_t)(((data & 0xff) + ((data >> 8) & 0xFF)) & 0xFF) << 32); }
/** Magic_serial_type1 Learning * @param data - serial number (28bit) * @param man - magic man (64bit) * @return manufacture for this serial number (64bit) */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/keeloq_common.c#L94-L98
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_keeloq_common_magic_serial_type2_learning
inline uint64_t subghz_protocol_keeloq_common_magic_serial_type2_learning(uint32_t data, uint64_t man) { uint8_t* p = (uint8_t*)&data; uint8_t* m = (uint8_t*)&man; m[7] = p[0]; m[6] = p[1]; m[5] = p[2]; m[4] = p[3]; return man; }
/** Magic_serial_type2 Learning * @param data - btn+serial number (32bit) * @param man - magic man (64bit) * @return manufacture for this serial number (64bit) */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/keeloq_common.c#L106-L115
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
subghz_protocol_keeloq_common_magic_serial_type3_learning
inline uint64_t subghz_protocol_keeloq_common_magic_serial_type3_learning(uint32_t data, uint64_t man) { return (man & 0xFFFFFFFFFF000000) | (data & 0xFFFFFF); }
/** Magic_serial_type3 Learning * @param data - serial number (24bit) * @param man - magic man (64bit) * @return manufacture for this serial number (64bit) */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/genie-recorder/protocols/keeloq_common.c#L123-L126
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rolling_flaws_navigation_exit_callback
uint32_t rolling_flaws_navigation_exit_callback(void* context) { UNUSED(context); return VIEW_NONE; }
/** * @brief Callback for navigation events * @details This function is called when user press back button. We return VIEW_NONE to * indicate that we want to exit the application. * @param context The context * @return next view id */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/rolling-flaws/rolling_flaws.c#L110-L113
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rolling_flaws_navigation_submenu_callback
uint32_t rolling_flaws_navigation_submenu_callback(void* context) { UNUSED(context); return RollingFlawsViewSubmenu; }
/** * @brief Callback for navigation events * @details This function is called when user press back button. We return VIEW_NONE to * indicate that we want to exit the application. * @param context The context * @return next view id */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/rolling-flaws/rolling_flaws.c#L122-L126
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rolling_flaws_navigation_submenu_stop_receiving_callback
uint32_t rolling_flaws_navigation_submenu_stop_receiving_callback(void* context) { RollingFlaws* app = (RollingFlaws*)context; stop_listening(app->subghz); return RollingFlawsViewSubmenu; }
/** * @brief Callback for navigation events * @details This function is called when user press back button. We return VIEW_NONE to * indicate that we want to exit the application. * @param context The context * @return next view id */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/rolling-flaws/rolling_flaws.c#L135-L140
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
my_app_navigation_exit_callback
static uint32_t my_app_navigation_exit_callback(void* context) { UNUSED(context); return VIEW_NONE; }
/** * @brief Callback for navigation events * @details This function is called when user press back button. We return VIEW_NONE to * indicate that we want to exit the application. * @param context The context * @return next view id */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/signal_send_demo/app.c#L61-L64
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
my_app_navigation_submenu_callback
static uint32_t my_app_navigation_submenu_callback(void* context) { UNUSED(context); return MyAppViewSubmenu; }
/** * @brief Callback for navigation events * @details This function is called when user press back button. We return VIEW_NONE to * indicate that we want to exit the application. * @param context The context * @return next view id */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/signal_send_demo/app.c#L73-L76
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
my_app_submenu_callback
static void my_app_submenu_callback(void* context, uint32_t index) { MyApp* app = (MyApp*)context; switch(index) { case MyAppSubmenuIndexConfigure: view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewConfigure); break; case MyAppSubmenuIndexFlipTheWorld: { // Copy our model data to the view. MyModel* model = view_get_model(app->view_flip_the_world); model->setting_1_index = app->model->setting_1_index; view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewFlipTheWorld); } break; case MyAppSubmenuIndexAbout: view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewAbout); break; default: FURI_LOG_E(TAG, "Unknown submenu index %lu", index); break; } }
/** * @brief Callback for most of the submenu events (About, Configure, etc.) * @details This function is called when user press a submenu item. We switch to the * appropriate view. * @param context The context * @param[in] index The index of the menu item. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/signal_send_demo/app.c#L85-L107
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
my_app_submenu_send_signal_callback
static void my_app_submenu_send_signal_callback(void* context, uint32_t index) { MyApp* app = (MyApp*)context; uint32_t freq = setting_1_values[app->model->setting_1_index]; // Show a "sending signal" screen while we send the signal. view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewSendSignal); switch(index) { case MyAppSubmenuIndexSendPricetonSignal: { // Send Princeton signal send_princeton(0x967AB4, freq); } break; case MyAppSubmenuIndexSendNiceFloSignal: { // Send Nice FLO signal send_nice_flo(0xDA1, freq); } break; default: FURI_LOG_E(TAG, "Unknown submenu index %lu", index); break; } // Small delay of a few milliseconds so people can read the screen, before we switch back. furi_delay_ms(400); view_dispatcher_switch_to_view(app->view_dispatcher, MyAppViewSubmenu); }
/** * @brief Callback for submenu events that send a signal * @details This function is called when user press a submenu item. We send the signal, * show a "sending signal" screen, and then switch back to the submenu. * @param context The context * @param[in] index The index of the menu item. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/apps/signal_send_demo/app.c#L116-L140
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
fmf2sub_navigation_exit_callback
static uint32_t fmf2sub_navigation_exit_callback(void* _context) { UNUSED(_context); return VIEW_NONE; }
/** * @brief Callback for exiting the application. * @details This function is called when user press back button. We return VIEW_NONE to * indicate that we want to exit the application. * @param _context The context - unused * @return next view id (VIEW_NONE) */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/fmf_to_sub/app.c#L92-L95
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
fmf2sub_navigation_submenu_callback
static uint32_t fmf2sub_navigation_submenu_callback(void* _context) { UNUSED(_context); return Fmf2SubViewSubmenu; }
/** * @brief Callback for returning to submenu. * @details This function is called when user press back button. We return ViewSubmenu to * indicate that we want to navigate to the submenu. * @param _context The context - unused * @return next view id (ViewSubmenu) */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/fmf_to_sub/app.c#L104-L107
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
fmf2sub_submenu_callback
static void fmf2sub_submenu_callback(void* context, uint32_t index) { Fmf2SubApp* app = (Fmf2SubApp*)context; switch(index) { case Fmf2SubSubmenuIndexConfigure: view_dispatcher_switch_to_view(app->view_dispatcher, Fmf2SubViewConfigure); break; case Fmf2SubSubmenuIndexConvert: view_dispatcher_switch_to_view(app->view_dispatcher, Fmf2SubViewConvert); break; case Fmf2SubSubmenuIndexAbout: view_dispatcher_switch_to_view(app->view_dispatcher, Fmf2SubViewAbout); break; default: break; } }
/** * @brief Handle submenu item selection. * @details This function is called when user selects an item from the submenu. * @param context The context - Fmf2SubApp object. * @param index The Fmf2SubSubmenuIndex item that was clicked. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/fmf_to_sub/app.c#L115-L130
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
fmf2sub_view_convert_draw_callback
static void fmf2sub_view_convert_draw_callback(Canvas* canvas, void* model) { Fmf2SubConvertModel* my_model = (Fmf2SubConvertModel*)model; canvas_set_font(canvas, FontSecondary); canvas_draw_str(canvas, 1, 10, "Press OK to select Flipper"); canvas_draw_str(canvas, 1, 20, "Music File (.FMF) to convert"); canvas_draw_str(canvas, 1, 30, "to Sub-GHz format (.SUB)."); canvas_draw_str(canvas, 10, 40, "FlipBoard buttons:"); canvas_set_font(canvas, FontPrimary); canvas_draw_str(canvas, 90, 40, setting_button_names[my_model->setting_button_index]); if(my_model->data.notes && my_model->state == Fmf2SubStateConverting) { canvas_draw_str(canvas, 1, 50, furi_string_get_cstr(my_model->data.notes)); } else { canvas_draw_str(canvas, 40, 50, setting_button_values[my_model->setting_button_index]); } canvas_set_font(canvas, FontPrimary); if(my_model->state == Fmf2SubStateLoading) { canvas_draw_str(canvas, 1, 60, "Loading..."); } else if(my_model->state == Fmf2SubStateError) { canvas_draw_str(canvas, 1, 60, "Error!"); } else if(my_model->state == Fmf2SubStateConverting) { canvas_draw_str(canvas, 1, 60, "Converting..."); } else if(my_model->state == Fmf2SubStateConverted) { canvas_draw_str(canvas, 1, 60, "Saved in Sub-GHz folder"); } else { canvas_draw_str(canvas, 1, 60, "Press OK to choose file"); } }
/** * @brief Callback for drawing the convert screen. * @details This function is called when the screen needs to be redrawn, like when the model gets updated. * @param canvas The canvas to draw on. * @param model The model - MyModel object. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/fmf_to_sub/app.c#L229-L257
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
fmf2sub_view_convert_enter_callback
static void fmf2sub_view_convert_enter_callback(void* context) { UNUSED(context); }
/** * @brief Callback when the user starts the convert screen. * @details This function is called when the user enters the convert screen. * @param context The context - Fmf2SubApp object. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/fmf_to_sub/app.c#L264-L266
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
fmf2sub_view_convert_exit_callback
static void fmf2sub_view_convert_exit_callback(void* context) { Fmf2SubApp* app = (Fmf2SubApp*)context; with_view_model( app->view_convert, Fmf2SubConvertModel * model, { model->state = Fmf2SubStateIdle; if(model->data.notes) { furi_string_free(model->data.notes); model->data.notes = NULL; } }, false); }
/** * @brief Callback when the user exits the convert screen. * @details This function is called when the user exits the convert screen. * @param context The context - Fmf2SubApp object. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/fmf_to_sub/app.c#L273-L286
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
fmf2sub_view_convert_custom_event_callback
static bool fmf2sub_view_convert_custom_event_callback(uint32_t event, void* context) { Fmf2SubApp* app = (Fmf2SubApp*)context; switch(event) { case Fmf2SubEventIdRedrawScreen: // Redraw screen by passing true to last parameter of with_view_model. { bool redraw = true; with_view_model( app->view_convert, Fmf2SubConvertModel * _model, { UNUSED(_model); }, redraw); return true; } case Fmf2SubEventIdOkPressed: { with_view_model( app->view_convert, Fmf2SubConvertModel * model, { model->state = Fmf2SubStateIdle; }, false); DialogsFileBrowserOptions browser_options; dialog_file_browser_set_basic_options(&browser_options, "", &I_fmf_10x10); browser_options.hide_dot_files = true; browser_options.hide_ext = false; browser_options.base_path = FMF_LOAD_PATH; furi_string_set(app->file_path, browser_options.base_path); if(dialog_file_browser_show( app->dialogs, app->file_path, app->file_path, &browser_options)) { view_dispatcher_send_custom_event(app->view_dispatcher, Fmf2SubEventIdLoadFile); } return true; } case Fmf2SubEventIdLoadFile: { with_view_model( app->view_convert, Fmf2SubConvertModel * model, { model->state = Fmf2SubStateLoading; fmf2sub_load_fmf_file(app, model); }, true); return true; } case Fmf2SubEventIdCreateSub: { with_view_model( app->view_convert, Fmf2SubConvertModel * model, { model->state = Fmf2SubStateConverting; FURI_LOG_D(TAG, "Loaded file: %s", furi_string_get_cstr(app->file_path)); FURI_LOG_D(TAG, "BPM: %ld", model->data.bpm); FURI_LOG_D(TAG, "Duration: %ld", model->data.duration); FURI_LOG_D(TAG, "Octave: %ld", model->data.octave); FURI_LOG_D(TAG, "Notes: %s", furi_string_get_cstr(model->data.notes)); fmf2sub_save_sub_file(app, model); }, true); return true; } default: return false; } }
/** * @brief Callback for custom events. * @details This function is called when a custom event is sent to the view dispatcher. * @param event The event id - Fmf2SubEventId value. * @param context The context - Fmf2SubApp object. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/fmf_to_sub/app.c#L630-L690
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
fmf2sub_view_convert_input_callback
static bool fmf2sub_view_convert_input_callback(InputEvent* event, void* context) { Fmf2SubApp* app = (Fmf2SubApp*)context; UNUSED(app); if(event->type == InputTypeShort) { if(event->key == InputKeyLeft) { bool redraw = true; with_view_model( app->view_convert, Fmf2SubConvertModel * model, { if(model->setting_button_index > 0) { model->setting_button_index--; variable_item_set_current_value_text( app->variable_item_button, setting_button_names[model->setting_button_index]); variable_item_set_current_value_index( app->variable_item_button, model->setting_button_index); } }, redraw); } else if(event->key == InputKeyRight) { bool redraw = true; with_view_model( app->view_convert, Fmf2SubConvertModel * model, { if(model->setting_button_index + 1 < (uint8_t)COUNT_OF(setting_button_values)) { model->setting_button_index++; variable_item_set_current_value_text( app->variable_item_button, setting_button_names[model->setting_button_index]); variable_item_set_current_value_index( app->variable_item_button, model->setting_button_index); } }, redraw); } } else if(event->type == InputTypePress) { if(event->key == InputKeyOk) { view_dispatcher_send_custom_event(app->view_dispatcher, Fmf2SubEventIdOkPressed); return true; } } return false; }
/** * @brief Callback for convert screen input. * @details This function is called when the user presses a button while on the convert screen. * @param event The event - InputEvent object. * @param context The context - Fmf2SubApp object. * @return true if the event was handled, false otherwise. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/fmf_to_sub/app.c#L699-L746
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
fmf2sub_app_free
static void fmf2sub_app_free(Fmf2SubApp* app) { view_dispatcher_remove_view(app->view_dispatcher, Fmf2SubViewTextInput); text_input_free(app->text_input); free(app->temp_buffer); view_dispatcher_remove_view(app->view_dispatcher, Fmf2SubViewAbout); widget_free(app->widget_about); view_dispatcher_remove_view(app->view_dispatcher, Fmf2SubViewConvert); view_free(app->view_convert); view_dispatcher_remove_view(app->view_dispatcher, Fmf2SubViewConfigure); variable_item_list_free(app->variable_item_list_config); view_dispatcher_remove_view(app->view_dispatcher, Fmf2SubViewSubmenu); submenu_free(app->submenu); view_dispatcher_free(app->view_dispatcher); furi_record_close(RECORD_GUI); furi_string_free(app->file_path); furi_record_close(RECORD_DIALOGS); free(app); }
/** * @brief Free the fmf2sub application. * @details This function frees the fmf2sub application resources. * @param app The fmf2sub application object. */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/fmf_to_sub/app.c#L876-L895
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
fmf_to_sub_app
int32_t fmf_to_sub_app(void* _p) { UNUSED(_p); Fmf2SubApp* app = fmf2sub_app_alloc(); view_dispatcher_run(app->view_dispatcher); fmf2sub_app_free(app); return 0; }
/** * @brief Main function for fmf2sub application. * @details This function is the entry point for the fmf2sub application. It should be defined in * application.fam as the entry_point setting. * @param _p Input parameter - unused * @return 0 - Success */
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/fmf_to_sub/app.c#L904-L912
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
isWin
static bool isWin(GameState state) { return (StateWonPaper == state) || (StateWonRock == state) || (StateWonScissors == state); }
// Checks if game state is winner. // @param state GameState to check. // @returns true if game state is a winner.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L22-L24
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
isLoss
static bool isLoss(GameState state) { return (StateLostPaper == state) || (StateLostRock == state) || (StateLostScissors == state); }
// Checks if game state is lost. // @param state GameState to check. // @returns true if game state is a loss.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L29-L31
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
isTie
static bool isTie(GameState state) { return (StateTiePaper == state) || (StateTieRock == state) || (StateTieScissors == state); }
// Checks if game state is tie. // @param state GameState to check. // @returns true if game state is a tie.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L36-L38
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
isResult
static bool isResult(GameState state) { return isWin(state) || isLoss(state) || isTie(state); }
// Checks if game state is result (win/loss/tie). // @param state GameState to check. // @returns true if game state is a win, loss or tie.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L43-L45
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
isFinalMove
static bool isFinalMove(GameState state) { return (StateRock == state) || (StatePaper == state) || (StateScissors == state); }
// Checks if game state is final move (rock/paper/scissors). // @param state GameState to check. // @returns true if game state is a rock, paper, scissors.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L50-L52
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
single_vibro
static void single_vibro() { NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION); notification_message(notification, &sequence_single_vibro); furi_record_close(RECORD_NOTIFICATION); }
// When user makes a move, we briefly pulse the vibro motor.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L55-L59
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
play_note
static void play_note(float frequency, float volume, uint32_t durationPlay, uint32_t durationPause) { furi_hal_speaker_start(frequency, volume); uint32_t n = furi_get_tick(); while(furi_get_tick() < n + durationPlay) { furi_thread_yield(); } furi_hal_speaker_stop(); n = furi_get_tick(); while(furi_get_tick() < n + durationPause) { furi_thread_yield(); } }
// Plays a note. You must acquire the speaker before invoking. // @frequency the frequency of the note in Hz. // @volume the volume of the note from 0.0 to 1.0 // @durationPlay the duration of the note in ms. // @durationPause the duration after the note to be silent.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L66-L78
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
play_song
static void play_song(GameContext* game_context) { if(furi_hal_speaker_acquire(1000)) { GameState state = game_context->data->local_player; const float volume = 1.0f; const uint32_t playQtr = 500; const uint32_t delayQtr = 100; if(isWin(state)) { play_note(523.25f, volume, playQtr, delayQtr); play_note(659.25f, volume, playQtr, delayQtr); play_note(783.99f, volume, playQtr, delayQtr); } else if(isLoss(state)) { play_note(783.99f, volume, playQtr * 2, delayQtr); play_note(523.25f, volume, playQtr, delayQtr); } else if(isTie(state)) { play_note(783.99f, volume, playQtr, delayQtr); play_note(523.25f, volume, playQtr, delayQtr); play_note(783.99f, volume, playQtr, delayQtr); } furi_hal_speaker_stop(); furi_hal_speaker_release(); GameEvent event = {.type = GameEventSongEnded, .tick = furi_get_tick()}; furi_message_queue_put(game_context->queue, &event, FuriWaitForever); } }
// Play a song
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L81-L107
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rps_worker_update_rx_event_callback
static void rps_worker_update_rx_event_callback(void* ctx) { furi_assert(ctx); GameContext* game_context = ctx; GameEvent event = {.type = GameEventDataDetected, .tick = furi_get_tick()}; furi_message_queue_put(game_context->queue, &event, FuriWaitForever); }
// We register this callback to get invoked whenever new subghz data is received. // Queue a GameEventDataDetected message. // @param ctx pointer to a GameContext
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L112-L117
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rps_timer_callback
static void rps_timer_callback(void* ctx) { furi_assert(ctx); GameContext* game_context = ctx; GameEvent event = {.type = GameEventTypeTimer}; furi_message_queue_put(game_context->queue, &event, FuriWaitForever); }
// We register this callback to get invoked whenever the timer triggers. // Queue a GameEventTypeTimer message. // @param ctx pointer to a GameContext
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L122-L127
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rps_receive_data
static void rps_receive_data(GameContext* game_context, uint32_t tick) { char sender_name[MESSAGE_MAX_LEN] = {0}; GameRfPurpose purpose; uint8_t version; unsigned int game_number; Move move = MoveUnknown; char* sender_contact; int index = 0; uint8_t message[MESSAGE_MAX_LEN] = {0}; memset(message, 0x00, MESSAGE_MAX_LEN); int len = (int)subghz_tx_rx_worker_read(game_context->subghz_txrx, message, MESSAGE_MAX_LEN); // Null terminate buffer at the end of message so we can't overrun the buffer. message[MESSAGE_MAX_LEN - 1] = 0; // Sender's Flipper Zero name. while(index < len && message[index] != ':') { sender_name[index] = message[index]; index++; } if(index >= len) { FURI_LOG_T(TAG, "Message too long, ignoring. >%s<", message); return; } else if(message[index] != ':') { FURI_LOG_T(TAG, "Message missing ':' character, ignoring. >%s<", message); return; } sender_name[index] = 0; // subghz chat sends escape message. Ignore it. if(index > 10 && message[0] == 0x1B && message[1] == '[' && message[3] == ';' && message[6] == 'm') { index = 7; while(index < len && message[index] != ':' && message[index] != 0x1B) { sender_name[index] = message[index]; index++; } if(index < len && message[index] == 0x1B) { // Skip over the "ESC [ 0 m", which is the reset color code. // We should be at the ':' character. index += 4; } } // Skip the ':' character & check for a space. if(++index < len) { if(message[index++] != ' ') { FURI_LOG_T(TAG, "Message missing ' ' after name, ignoring. >%s<", message); return; } } // Check for the game name. int game_name_len = (int)strlen(RPS_GAME_NAME); for(int i = 0; i < game_name_len; i++) { if((index >= len) || (message[index++] != RPS_GAME_NAME[i])) { FURI_LOG_T( TAG, "Message missing game name '%s', ignoring. >%s<", RPS_GAME_NAME, message); return; } } if(index < len) { if(message[index++] != ':') { FURI_LOG_T(TAG, "Message missing ':' after game name, ignoring. >%s<", message); return; } } FURI_LOG_D(TAG, "Got message >%s<", message); // The purpose immediately follows the game name. if(index >= len) { FURI_LOG_W(TAG, "Message missing purpose, ignoring. >%s<", message); return; } purpose = message[index++]; // The version follows the purpose. if(index >= len) { FURI_LOG_W(TAG, "Message missing version, ignoring. >%s<", message); return; } version = message[index++]; FURI_LOG_T(TAG, "Purpose is %c and version is %c", purpose, version); // Game number is 3 digits. if(sscanf((const char*)message + index, "%03u", &game_number) != 1) { FURI_LOG_W(TAG, "Message missing game number, ignoring. >%s<", message); return; } index += 3; switch(purpose) { case GameRfPurposeMove: // We expect this mesage to the game number, move and sender name. if(index >= len) { FURI_LOG_W(TAG, "Failed to parse move message. >%s<", message); return; } else { move = (Move)message[index]; // IMPORTANT: The code processing the event needs to furi_string_free the senderName! FuriString* name = furi_string_alloc(); furi_string_set(name, sender_name); GameEvent event = { .type = GameEventRemoteMove, .move = move, .tick = tick, .sender_name = name, .game_number = game_number}; furi_message_queue_put(game_context->queue, &event, FuriWaitForever); } break; case GameRfPurposeBeacon: { // IMPORTANT: The code processing the event needs to furi_string_free the senderName! FuriString* name = furi_string_alloc(); furi_string_set(name, sender_name); GameEvent event = { .type = GameEventRemoteBeacon, .sender_name = name, .game_number = game_number}; furi_message_queue_put(game_context->queue, &event, FuriWaitForever); break; } case GameRfPurposeNotBeacon: { // IMPORTANT: The code processing the event needs to furi_string_free the senderName! FuriString* name = furi_string_alloc(); furi_string_set(name, sender_name); GameEvent event = { .type = GameEventRemoteNotBeacon, .sender_name = name, .game_number = game_number}; furi_message_queue_put(game_context->queue, &event, FuriWaitForever); break; } case GameRfPurposePlayAgain: { // IMPORTANT: The code processing the event needs to furi_string_free the senderName! FuriString* name = furi_string_alloc(); furi_string_set(name, sender_name); GameEvent event = { .type = GameEventRemotePlayAgain, .sender_name = name, .game_number = game_number}; furi_message_queue_put(game_context->queue, &event, FuriWaitForever); break; } case GameRfPurposeQuit: { // IMPORTANT: The code processing the event needs to furi_string_free the senderName! FuriString* name = furi_string_alloc(); furi_string_set(name, sender_name); GameEvent event = { .type = GameEventRemoteQuit, .sender_name = name, .game_number = game_number}; furi_message_queue_put(game_context->queue, &event, FuriWaitForever); break; } case GameRfPurposeJoin: if(index >= len) { FURI_LOG_W(TAG, "Failed to parse join message. >%s<", message); return; } else { sender_contact = (char*)message + index; while(index < len) { if(message[index] == '\n' || message[index] == '\r') { message[index] = 0; break; } index++; } FURI_LOG_T(TAG, "Join had contact of >%s<", sender_contact); // IMPORTANT: The code processing the event needs to furi_string_free the senderName! FuriString* name = furi_string_alloc(); furi_string_set(name, sender_name); FuriString* contact = furi_string_alloc(); furi_string_set(contact, sender_contact); GameEvent event = { .type = GameEventRemoteJoined, .sender_name = name, .sender_contact = contact, .game_number = game_number}; furi_message_queue_put(game_context->queue, &event, FuriWaitForever); } break; case GameRfPurposeJoinAcknowledge: if(index >= len) { FURI_LOG_W(TAG, "Failed to parse join acknowledge message. >%s<", message); return; } else { sender_contact = (char*)message + index; while(index < len) { if(message[index] == '\n' || message[index] == '\r') { message[index] = 0; break; } index++; } FURI_LOG_T(TAG, "Join acknowledge for game %d.", game_number); FURI_LOG_T(TAG, "Join ack had contact of >%s<", sender_contact); FuriString* name = furi_string_alloc(); furi_string_set(name, sender_name); FuriString* contact = furi_string_alloc(); furi_string_set(contact, sender_contact); GameEvent event = { .type = GameEventRemoteJoinAcknowledged, .sender_name = name, .sender_contact = contact, .game_number = game_number}; furi_message_queue_put(game_context->queue, &event, FuriWaitForever); } break; default: if(version <= MAJOR_VERSION) { // The version is same or less than ours, so we should know about the message purpose. FURI_LOG_E(TAG, "Message purpose not handled for known version. >%s<", message); } else { // The version is newer, so it's not surprising we don't know about the purpose. FURI_LOG_T(TAG, "Message purpose not handled. >%s<", message); } break; } }
// This gets invoked when we process a GameEventDataDetected event. // Read the message using subghz_tx_rx_worker_read & determine if valid format. // If valid, we queue a message for further processing. // @param game_context pointer to a GameContext // @param time (furi_get_tick) when event was initially made
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L134-L364
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rps_input_callback
static void rps_input_callback(InputEvent* input_event, void* ctx_q) { furi_assert(ctx_q); FuriMessageQueue* queue = ctx_q; GameEvent event = {.type = GameEventTypeKey, .tick = furi_get_tick(), .input = *input_event}; furi_message_queue_put(queue, &event, FuriWaitForever); }
// This gets invoked when input (button press) is detected. // We queue a GameEventTypeKey message with the input event data. // @param input_event event information, such as key that was pressed. // @param ctx_q message queue.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L370-L375
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rps_render_host_game
static void rps_render_host_game(Canvas* canvas, void* ctx) { furi_assert(ctx); GameContext* game_context = ctx; // Attempt to aquire context, so we can read the data. if(furi_mutex_acquire(game_context->mutex, 200) != FuriStatusOk) { return; } GameData* data = game_context->data; unsigned int gameNumber = data->game_number; canvas_draw_icon(canvas, 0, 0, images[DolphinLocalLooking]); canvas_set_font(canvas, FontPrimary); if(data->local_player == StateHostingSetFrequency || data->local_player == StateHostingBadFrequency) { canvas_draw_box(canvas, 48, 3, 128 - 50, 14); canvas_invert_color(canvas); } uint32_t freq = frequency_list[game_context->data->frequency_index]; uint16_t freq_mhz = freq / 1000000; uint16_t freq_mod = (freq % 1000000) / 10000; furi_string_printf(data->buffer, "freq < %03d.%02d >", freq_mhz, freq_mod); canvas_draw_str_aligned( canvas, 50, 5, AlignLeft, AlignTop, furi_string_get_cstr(data->buffer)); if(data->local_player == StateHostingSetFrequency || data->local_player == StateHostingBadFrequency) { canvas_invert_color(canvas); } if(data->local_player == StateHostingSetGameNumber) { canvas_draw_box(canvas, 48, 18, 128 - 50, 14); canvas_invert_color(canvas); } furi_string_printf(data->buffer, "game < %03d >", gameNumber); canvas_draw_str_aligned( canvas, 50, 20, AlignLeft, AlignTop, furi_string_get_cstr(data->buffer)); if(data->local_player == StateHostingSetGameNumber) { canvas_invert_color(canvas); } if(data->local_player == StateHostingLookingForPlayer) { canvas_set_font(canvas, FontSecondary); furi_string_printf(data->buffer, "Waiting for player, game %03d.", gameNumber); canvas_draw_str_aligned( canvas, 0, 53, AlignLeft, AlignTop, furi_string_get_cstr(data->buffer)); } else if(data->local_player == StateHostingBadFrequency) { canvas_draw_str_aligned(canvas, 0, 53, AlignLeft, AlignTop, "Frequency not avail."); } else { canvas_draw_str_aligned(canvas, 0, 53, AlignLeft, AlignTop, "Press OK to start game."); } furi_mutex_release(game_context->mutex); }
// Render UI when we are hosting the game. // @param canvas rendering surface of the Flipper Zero. // @param ctx pointer to a GameContext.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L380-L435
89716a9b00eacce7055a75fddb5a3adde93f265f
flipper-zero-tutorials
github_2023
jamisonderek
c
rps_render_join_game
static void rps_render_join_game(Canvas* canvas, void* ctx) { furi_assert(ctx); GameContext* game_context = ctx; // Attempt to aquire context, so we can read the data. if(furi_mutex_acquire(game_context->mutex, 200) != FuriStatusOk) { return; } GameData* data = game_context->data; canvas_draw_icon(canvas, 0, 0, images[DolphinLocalLooking]); canvas_set_font(canvas, FontPrimary); if(data->local_player == StateJoiningSetFrequency || data->local_player == StateJoiningBadFrequency) { canvas_draw_box(canvas, 48, 3, 128 - 50, 14); canvas_invert_color(canvas); } uint32_t freq = frequency_list[game_context->data->frequency_index]; uint16_t freq_mhz = freq / 1000000; uint16_t freq_mod = (freq % 1000000) / 10000; furi_string_printf(data->buffer, "freq < %03d.%02d >", freq_mhz, freq_mod); canvas_draw_str_aligned( canvas, 50, 5, AlignLeft, AlignTop, furi_string_get_cstr(data->buffer)); if(data->local_player == StateJoiningSetFrequency || data->local_player == StateJoiningBadFrequency) { canvas_invert_color(canvas); } if(data->local_player == StateJoiningSetGameNumber) { canvas_draw_box(canvas, 48, 18, 128 - 50, 14); canvas_invert_color(canvas); } GameInfo* game = remote_games_current(game_context); if(game) { char prev = remote_games_has_previous(game_context) ? '<' : ' '; char next = remote_games_has_next(game_context) ? '>' : ' '; furi_string_printf(data->buffer, "game %c %03d %c", prev, game->game_number, next); } else { furi_string_printf(data->buffer, "game none"); } canvas_draw_str_aligned( canvas, 50, 20, AlignLeft, AlignTop, furi_string_get_cstr(data->buffer)); if(data->local_player == StateJoiningSetGameNumber) { canvas_invert_color(canvas); } if(game && game->sender_name) { canvas_draw_str_aligned( canvas, 50, 35, AlignLeft, AlignTop, furi_string_get_cstr(game->sender_name)); } if(data->local_player == StateJoiningBadFrequency) { canvas_draw_str_aligned(canvas, 0, 53, AlignLeft, AlignTop, "Frequency not avail."); } else if(game) { canvas_draw_str_aligned(canvas, 0, 53, AlignLeft, AlignTop, "Press OK to join game."); } else { canvas_draw_str_aligned(canvas, 0, 53, AlignLeft, AlignTop, "No games available."); } furi_mutex_release(game_context->mutex); }
// Render UI when we are joining a game. // @param canvas rendering surface of the Flipper Zero. // @param ctx pointer to a GameContext.
https://github.com/jamisonderek/flipper-zero-tutorials/blob/89716a9b00eacce7055a75fddb5a3adde93f265f/subghz/plugins/rock_paper_scissors/rock_paper_scissors.c#L440-L503
89716a9b00eacce7055a75fddb5a3adde93f265f