~ruther/uni-mam-arm

bb6a58a376bbc65e6c168ecc698421164363bb38 — Rutherther 3 months ago 92d0958
feat(arm07): implement few commands
4 files changed, 86 insertions(+), 20 deletions(-)

M arm07/include/spi_matrix.h
M arm07/src/main.c
M arm07/src/spi.c
M arm07/src/spi_matrix.c
M arm07/include/spi_matrix.h => arm07/include/spi_matrix.h +5 -0
@@ 40,6 40,8 @@ typedef struct {

  uint32_t state_cycle;
  uint32_t cycle;

  uint8_t intensity;
} matrix_t;

void matrix_init(matrix_t* matrix, buffered_transceiver_t* transceiver, uint32_t shift_period);


@@ 47,6 49,9 @@ void matrix_init(matrix_t* matrix, buffered_transceiver_t* transceiver, uint32_t
void matrix_setup(matrix_t* matrix);
void matrix_enable(matrix_t* matrix, bool enable);

void matrix_increase_intensity(matrix_t *matrix);
void matrix_decrease_intensity(matrix_t *matrix);

void matrix_send(matrix_t* matrix, matrix_register_t reg, uint8_t value);
void matrix_set_buffer(matrix_t* matrix, matrix_slot_t slot, uint8_t* buffer);


M arm07/src/main.c => arm07/src/main.c +61 -18
@@ 98,6 98,9 @@ buffered_transceiver_t uart_rx;
#define AUTO_TOGGLE_CYCLES 1000000
#define MAX_IMAGES 10

#define COLS 8
#define ROWS 8

uint8_t images_count = 4;
uint8_t current_image = 0;
uint8_t images[MAX_IMAGES][8] = {


@@ 113,6 116,13 @@ bool toggle_next = false;

bool animation = false;

void num_to_string(uint16_t num, char* res, uint16_t digits) {
  for (uint16_t i = 0; i < digits; i++) {
    res[digits - 1 - i] = num % 10 + '0';
    num /= 10;
  }
}

void handle_command(char* cmd, uint16_t len) {
  if (len == 0) {
    return;


@@ 121,16 131,14 @@ void handle_command(char* cmd, uint16_t len) {
  bool handled = false;

  switch (cmd[0]) {
  case 'n':
    // next
  case 'n': // next
    if (len == 1) {
      handled = true;
      toggle_next = true;
      buffered_transceiver_transmit(&uart_rx, "Switching to next image.\r\n", 0);
    }
    break;
  case 'N':
    // auto toggle
  case 'N': // auto toggle
    if (len == 1) {
      handled = true;
      auto_toggle = !auto_toggle;


@@ 142,8 150,7 @@ void handle_command(char* cmd, uint16_t len) {
      }
    }
    break;
  case 'a':
    // animate
  case 'a': // animate
    if (len == 1) {
      handled = true;
      animation = !animation;


@@ 155,15 162,47 @@ void handle_command(char* cmd, uint16_t len) {
      }
    }
    break;
  case 'u':
  case 'u': // upload
    // upload
    // TODO
    break;
  case 's':
    // number of slots is...
    // TODO
    break;
  case 's': // number of slots is...
    if (len == 1) {
      handled = true;

      buffered_transceiver_transmit(&uart_rx, "Used slots: ", 0);

      char str[5] = { '\0', '\0', '\r', '\n', '\0' };
      num_to_string(images_count, str, 2);
      buffered_transceiver_transmit(&uart_rx, str, 0);
    }
    break;
  case 'I': // increase intensity
    if (len == 1) {
      matrix_increase_intensity(&matrix);
      buffered_transceiver_transmit(&uart_rx, "Increasing intensity.\r\n", 0);
      handled = true;
    }
    break;
  case 'i': // decrease intensity
    if (len == 1) {
      matrix_decrease_intensity(&matrix);
      buffered_transceiver_transmit(&uart_rx, "Decreasing intensity.\r\n", 0);
      handled = true;
    }
    break;
  case 'h':
    buffered_transceiver_transmit(&uart_rx, "Help:\r\n", 0);
    buffered_transceiver_transmit(&uart_rx, " h - show this help messages\r\n", 0);
    buffered_transceiver_transmit(&uart_rx, " s - print used slots\r\n", 0);
    buffered_transceiver_transmit(&uart_rx, " a - toggle animating\r\n", 0);
    buffered_transceiver_transmit(&uart_rx, " n - next image\r\n", 0);
    buffered_transceiver_transmit(&uart_rx, " N - automatically switch images\r\n", 0);
    buffered_transceiver_transmit(&uart_rx, " I - increase intensity\r\n", 0);
    buffered_transceiver_transmit(&uart_rx, " i - decrease intensity\r\n", 0);
    buffered_transceiver_transmit(&uart_rx, " uIMG - upload new image, IMG has \"X\" for 1s and \"-\" for 0s. It should be 64 chars long\r\n", 0);
    handled = true;
    break;
  default:
    break;
  }


@@ 207,6 246,10 @@ char* receive_command(uint16_t* length)

void next_image()
{
  if (matrix.state != MATRIX_STABLE) {
    return;
  }

  current_image++;
  current_image %= images_count;



@@ 261,8 304,8 @@ void main()
    pin_t pin_rx;
    pin_t pin_tx;

    #define UART_RX_BUFFER_SIZE 32
    #define UART_TX_BUFFER_SIZE 32
    #define UART_RX_BUFFER_SIZE 64
    #define UART_TX_BUFFER_SIZE 300

    static queue_t uart_rx_queue, uart_tx_queue;
    static uint16_t uart_rx_buffer[UART_RX_BUFFER_SIZE], uart_tx_buffer[UART_TX_BUFFER_SIZE];


@@ 305,7 348,7 @@ void main()
    pin_t pin_csn;
    pin_t pin_clk;

    #define SPI_RX_BUFFER_SIZE 1
    #define SPI_RX_BUFFER_SIZE 0
    #define SPI_TX_BUFFER_SIZE 10

    static queue_t spi_rx_queue, spi_tx_queue;


@@ 439,9 482,9 @@ void TIM3_handler(void) {
bool spi_can_transmit_flag = true;

void SPI1_handler(void) {
  if (spi_can_receive(&matrix_spi)) {
    buffered_transceiver_trigger_receive(&matrix_tx, 1);
  }
  /* if (spi_can_receive(&matrix_spi)) { */
  /*   buffered_transceiver_trigger_receive(&matrix_tx, 1); */
  /* } */
  if (spi_can_transmit(&matrix_spi)) {
    //
    spi_can_transmit_flag = false;


@@ 457,7 500,7 @@ void TIM4_handler(void) {
  timer_clear_update_interrupt(&spi_csn_timer);

  if (!(matrix_spi.periph->SR & SPI_SR_BSY)) {
    // disable and enable spi so that CSN is pulsed.
    // puse CSN.
    spi_pulse_csn(&matrix_spi);

    // Allow sending...

M arm07/src/spi.c => arm07/src/spi.c +1 -1
@@ 29,7 29,6 @@ void spi_master_enable(spi_t *spi, bool enable) {
}

uint16_t spi_transmit(spi_t *spi, uint16_t *data, uint16_t size) {
  pin_reset(&spi->csn);
  for (int16_t i = 0; i < size; i++) {
    while (!(spi->periph->SR & SPI_SR_TXE));
    uint16_t word = *(data++);


@@ 51,6 50,7 @@ uint16_t spi_receive(spi_t *spi, uint16_t *buffer, uint16_t max_size) {

void spi_pulse_csn(spi_t *spi) {
  pin_set(&spi->csn);
  pin_reset(&spi->csn);
}

bool spi_can_transmit(spi_t *spi) {

M arm07/src/spi_matrix.c => arm07/src/spi_matrix.c +19 -1
@@ 11,6 11,8 @@ void matrix_init(matrix_t *matrix, buffered_transceiver_t *transceiver,

  matrix->state_cycle = 0;
  matrix->cycle = 0;

  matrix->intensity = 1;
}

void matrix_setup(matrix_t *matrix) {


@@ 24,10 26,26 @@ void matrix_enable(matrix_t *matrix, bool enable) {

  matrix_send(matrix, MATRIX_REG_SHUTDOWN, enable ? 1 : 0);
  matrix_send(matrix, MATRIX_REG_DECODE_MODE, 0);
  matrix_send(matrix, MATRIX_REG_INTENSITY, 0x01);
  matrix_send(matrix, MATRIX_REG_INTENSITY, matrix->intensity);
  matrix_send(matrix, MATRIX_REG_SCAN_LIMIT, 0x7);
}

void matrix_increase_intensity(matrix_t *matrix) {
  if (matrix->intensity < 0xF) {
    matrix->intensity++;
  }

  matrix_send(matrix, MATRIX_REG_INTENSITY, matrix->intensity);
}

void matrix_decrease_intensity(matrix_t *matrix) {
  if (matrix->intensity > 0x0) {
    matrix->intensity--;
  }

  matrix_send(matrix, MATRIX_REG_INTENSITY, matrix->intensity);
}

void matrix_send(matrix_t *matrix, matrix_register_t reg, uint8_t value) {
  uint16_t data = (((uint16_t)reg) << 8) | (uint16_t)value;
  buffered_transceiver_transmit(matrix->transceiver, (void*)&data, 1);

Do not follow this link