]> mj.ucw.cz Git - home-hw.git/blobdiff - nucleo-test/Src/test.c
Morseovkový dekodér
[home-hw.git] / nucleo-test / Src / test.c
index 717771dbdee783a314a8686df1901fc1e5b763f5..570f89d54e110353309f7c53f1fd165a806ee6fd 100644 (file)
@@ -11,6 +11,8 @@ typedef int16_t s16;
 
 void debug_putc(int c)
 {
+  if (c == '\n')
+    debug_putc('\r');
   while (!LL_USART_IsActiveFlag_TXE(USART2))
     ;
   LL_USART_TransmitData8(USART2, c);
@@ -135,384 +137,79 @@ void debug_printf(const char *fmt, ...)
   va_end(args);
 }
 
-#if 0
-
-static uint bmp_read(uint reg, uint bytes)
-{
-  LL_I2C_ClearFlag_STOP(I2C1);
-  LL_I2C_ClearFlag_BERR(I2C1);
-  LL_I2C_HandleTransfer(I2C1, 0xee, LL_I2C_ADDRSLAVE_7BIT, 1, LL_I2C_MODE_SOFTEND, LL_I2C_GENERATE_START_WRITE);
-  while (!LL_I2C_IsActiveFlag_TXE(I2C1))
-    ;
-  LL_I2C_TransmitData8(I2C1, reg);
-  while (!LL_I2C_IsActiveFlag_TC(I2C1))
-    ;
-  LL_I2C_HandleTransfer(I2C1, 0xef, LL_I2C_ADDRSLAVE_7BIT, bytes, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_RESTART_7BIT_READ);
-
-  uint d = 0;
-  for (uint i=0; i<bytes; i++)
-    {
-      while (!LL_I2C_IsActiveFlag_RXNE(I2C1))
-       ;
-      d = (d << 8) | LL_I2C_ReceiveData8(I2C1);
+void decide_set(int n){
+    switch (n){
+      case 1:
+       LL_GPIO_SetOutputPin(MEGGY1_GPIO_Port, MEGGY1_Pin);
+       break;
+      case 2:
+       LL_GPIO_SetOutputPin(MEGGY2_GPIO_Port, MEGGY2_Pin);
+       break;
+      case 3:
+       LL_GPIO_SetOutputPin(MEGGY3_GPIO_Port, MEGGY3_Pin);
+       break;
+      case 0:
+       LL_GPIO_SetOutputPin(MEGGY4_GPIO_Port, MEGGY4_Pin);
+       break;
     }
-
-  return d;
 }
 
-static uint bmp_measure(uint type, uint bytes)
-{
-  LL_I2C_HandleTransfer(I2C1, 0xee, LL_I2C_ADDRSLAVE_7BIT, 2, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
-  while (!LL_I2C_IsActiveFlag_TXE(I2C1))
-    ;
-  LL_I2C_TransmitData8(I2C1, 0xf4);
-  while (!LL_I2C_IsActiveFlag_TXE(I2C1))
-    ;
-  LL_I2C_TransmitData8(I2C1, type);
-  while (!LL_I2C_IsActiveFlag_STOP(I2C1))
-    ;
-
-  while (!LL_GPIO_IsInputPinSet(BMP_DONE_GPIO_Port, BMP_DONE_Pin))
-    ;
-
-  return bmp_read(0xf6, bytes);
-}
-
-// Formulae from BMP085 specs
-void bmp_recalc(uint UT, uint UP, uint oss, u16 cc[11], int *tt, int *pp)
-{
-  s16 AC1 = cc[0];
-  s16 AC2 = cc[1];
-  s16 AC3 = cc[2];
-  u16 AC4 = cc[3];
-  u16 AC5 = cc[4];
-  u16 AC6 = cc[5];
-  s16 B1 = cc[6];
-  s16 B2 = cc[7];
-  s16 MB = cc[8];
-  s16 MC = cc[9];
-  s16 MD = cc[10];
-  UP >>= (8-oss);
-
-  int X1 = (UT-AC6)*AC5 / (1<<15);
-  int X2 = MC*(1<<11) / (X1+MD);
-  int B5 = X1 + X2;
-  int T = (B5+8) / (1<<4);
-  *tt = T;
-
-  int B6 = B5 - 4000;
-  X1 = (B2*(B6*B6/(1<<12))) / (1<<11);
-  X2 = AC2 * B6 / (1<<11);
-  int X3 = X1 + X2;
-  int B3 = (((AC1*4 + X3) << oss) + 2) / 4;
-  X1 = AC3 * B6 / (1<<13);
-  X2 = (B1*(B6*B6/(1<<12))) / (1<<16);
-  X3 = ((X1+X2) + 2) / (1<<2);
-  uint B4 = (uint)(AC4 * (X3 + 32768)) / (1U<<15);
-  uint B7 = (uint)(UP-B3) * (uint)(50000>>oss);
-  int p;
-  if (B7 < 0x80000000)
-    p = (B7*2) / B4;
-  else
-    p = B7 / B4 * 2;
-  X1 = (p/(1<<8)) * (p/(1<<8));
-  X1 = (X1*3038) / (1<<16);
-  X2 = (-7357*p) / (1<<16);
-  p = p + (X1 + X2 + 3791) / (1<<4);
-  *pp = p;
-}
-
-void run_test(void)
-{
-  for (;;)
-    {
-      LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
-
-      debug_puts("Constants:");
-      u16 cc[11];
-      for (uint i=0; i<11; i++)
-       {
-         cc[i] = bmp_read(0xaa + 2*i, 2);
-         debug_printf(" %04x", cc[i]);
-       }
-      debug_puts("\r\n");
-
-      uint raw_temp = bmp_measure(0x2e, 2);
-      debug_printf("Raw temperature: %04x\r\n", raw_temp);
-
-      uint oss = 3;    // Over-sampling setting
-      uint raw_press = bmp_measure(0xf4 | (oss<<6), 3);
-      debug_printf("Raw pressure: %06x\r\n", raw_press);
-
-      int temp, press;
-      bmp_recalc(raw_temp, raw_press, oss, cc, &temp, &press);
-      debug_printf("Temperature: %d ddegC\r\n", temp);
-      debug_printf("Pressure: %d Pa\r\n", press);
-
-      LL_mDelay(1000);
+void decide_reset(int n){
+    switch (n){
+      case 1:
+       LL_GPIO_ResetOutputPin(MEGGY1_GPIO_Port, MEGGY1_Pin);
+       break;
+      case 2:
+       LL_GPIO_ResetOutputPin(MEGGY2_GPIO_Port, MEGGY2_Pin);
+       break;
+      case 3:
+       LL_GPIO_ResetOutputPin(MEGGY3_GPIO_Port, MEGGY3_Pin);
+       break;
+      case 0:
+       LL_GPIO_ResetOutputPin(MEGGY4_GPIO_Port, MEGGY4_Pin);
+       break;
     }
 }
 
-#elif 0
-
-static const byte Gentium23x32[] = {
-        0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF8, 0x1F, 0x00, 0x80, 0xFF, 0xFF, 0x00, 0xE0, 0xFF, 0xFF, 0x03, 0xF0, 0xFF, 0xFF, 0x0F, 0xF8, 0xFF, 0xFF, 0x1F, 0xFC, 0xFF, 0xFF, 0x3F, 0xFE, 0x01, 0xE0, 0x3F, 0x3E, 0x00, 0x00, 0x7F, 0x1F, 0x00, 0x00, 0x7C, 0x0F, 0x00, 0x00, 0x78, 0x0F, 0x00, 0x00, 0x78, 0x1F, 0x00, 0x00, 0x78, 0x3F, 0x00, 0x00, 0x78, 0x7F, 0x00, 0x00, 0x3E, 0xFE, 0x07, 0xC0, 0x3F, 0xFE, 0xFF, 0xFF, 0x1F, 0xFC, 0xFF, 0xFF, 0x0F, 0xF8, 0xFF, 0xFF, 0x07, 0xE0, 0xFF, 0xFF, 0x03, 0x80, 0xFF, 0xFF, 0x00, 0x00, 0xFC, 0x0F, 0x00,  // Code for char 0
-        0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x70, 0x78, 0x00, 0x00, 0x70, 0x78, 0x00, 0x00, 0x78, 0x78, 0x00, 0x00, 0x78, 0x7C, 0x00, 0x00, 0x78, 0x3C, 0x00, 0x00, 0x78, 0xFE, 0xFF, 0xFF, 0x7F, 0xFE, 0xFF, 0xFF, 0x7F, 0xFE, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F, 0xFE, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00,  // Code for char 1
-        0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x80, 0x01, 0x00, 0x78, 0xE0, 0x03, 0x00, 0x7C, 0xF0, 0x03, 0x00, 0x7E, 0xF8, 0x03, 0x80, 0x7F, 0xFC, 0x01, 0xC0, 0x7F, 0xFE, 0x01, 0xE0, 0x7F, 0x3E, 0x00, 0xF0, 0x7F, 0x1E, 0x00, 0xFC, 0x7F, 0x0F, 0x00, 0xFE, 0x79, 0x0F, 0x00, 0xFF, 0x78, 0x0F, 0xC0, 0x7F, 0x78, 0x1F, 0xE0, 0x3F, 0x78, 0x3F, 0xF8, 0x0F, 0x78, 0xFF, 0xFF, 0x07, 0x78, 0xFE, 0xFF, 0x03, 0x78, 0xFE, 0xFF, 0x00, 0x7C, 0xFC, 0x7F, 0x80, 0x7F, 0xF8, 0x1F, 0x80, 0x7F, 0xE0, 0x07, 0x80, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // Code for char 2
-        0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0xC0, 0x00, 0x00, 0x1F, 0xF0, 0x01, 0x00, 0x1E, 0xF8, 0x01, 0x00, 0x3E, 0xFC, 0x01, 0x00, 0x3C, 0xFE, 0x00, 0x00, 0x7C, 0xFE, 0x00, 0x00, 0x78, 0x1E, 0xE0, 0x00, 0x78, 0x0F, 0xF0, 0x00, 0x78, 0x0F, 0xF0, 0x00, 0x78, 0x0F, 0xF0, 0x00, 0x78, 0x1F, 0xF8, 0x01, 0x7C, 0x3F, 0xFC, 0x01, 0x7C, 0xFF, 0xFF, 0x07, 0x3F, 0xFE, 0xFF, 0xFF, 0x3F, 0xFE, 0xFF, 0xFF, 0x1F, 0xFC, 0xCF, 0xFF, 0x0F, 0xF8, 0xC7, 0xFF, 0x07, 0xE0, 0x83, 0xFF, 0x03, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // Code for char 3
-        0x17, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x3E, 0x00, 0x00, 0x80, 0x3F, 0x00, 0x00, 0xC0, 0x3F, 0x00, 0x00, 0xF0, 0x3F, 0x00, 0x00, 0xF8, 0x3F, 0x00, 0x00, 0xFE, 0x3D, 0x00, 0x00, 0xFF, 0x3C, 0x70, 0xC0, 0x3F, 0x3C, 0x70, 0xE0, 0x1F, 0x3C, 0x70, 0xF8, 0x07, 0x3C, 0x78, 0xFC, 0x03, 0x3C, 0x78, 0xFE, 0xFF, 0xFF, 0x7F, 0xFE, 0xFF, 0xFF, 0x7F, 0xFE, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0x7F, 0xFE, 0xFF, 0xFF, 0x7F, 0x00, 0x00, 0x3C, 0x78, 0x00, 0x00, 0x3C, 0x70, 0x00, 0x00, 0x1C, 0x70, 0x00, 0x00, 0x08, 0x00,  // Code for char 4
-        0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0xC0, 0x00, 0x1F, 0x00, 0xFE, 0x01, 0x1E, 0xFC, 0xFF, 0x01, 0x3E, 0xFC, 0xFF, 0x00, 0x3C, 0xFC, 0xFF, 0x00, 0x3C, 0xFC, 0x7F, 0x00, 0x78, 0x3C, 0x78, 0x00, 0x78, 0x3C, 0x78, 0x00, 0x78, 0x3C, 0x78, 0x00, 0x78, 0x3C, 0x78, 0x00, 0x78, 0x3C, 0xF8, 0x00, 0x7C, 0x3C, 0xF8, 0x00, 0x7E, 0x3C, 0xF0, 0x03, 0x3F, 0x3C, 0xF0, 0xFF, 0x3F, 0x3C, 0xF0, 0xFF, 0x1F, 0x1E, 0xE0, 0xFF, 0x0F, 0x0F, 0xC0, 0xFF, 0x07, 0x06, 0x80, 0xFF, 0x03, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00,  // Code for char 5
-        0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x7F, 0x00, 0x00, 0xF8, 0xFF, 0x01, 0x00, 0xFE, 0xFF, 0x07, 0x80, 0xFF, 0xFF, 0x0F, 0xC0, 0xFF, 0xFF, 0x1F, 0xE0, 0xFF, 0xFF, 0x3F, 0xF0, 0xFF, 0xC1, 0x3F, 0xF8, 0xF7, 0x00, 0x7E, 0xF8, 0x71, 0x00, 0x7C, 0xFC, 0x78, 0x00, 0x78, 0x7C, 0x78, 0x00, 0x78, 0x3E, 0x78, 0x00, 0x78, 0x1E, 0xF8, 0x00, 0x7C, 0x1E, 0xF8, 0x03, 0x3E, 0x0F, 0xF0, 0xFF, 0x3F, 0x0F, 0xF0, 0xFF, 0x1F, 0x0F, 0xE0, 0xFF, 0x0F, 0x06, 0xE0, 0xFF, 0x07, 0x00, 0x80, 0xFF, 0x03, 0x00, 0x00, 0x7E, 0x00,  // Code for char 6
-        0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0x00, 0x00, 0xF8, 0x01, 0x00, 0x00, 0xFC, 0x01, 0x00, 0x00, 0xFC, 0x01, 0x00, 0x20, 0x7C, 0x00, 0x00, 0x78, 0x3C, 0x00, 0x00, 0x7E, 0x3C, 0x00, 0x80, 0x7F, 0x3C, 0x00, 0xE0, 0x3F, 0x3C, 0x00, 0xF8, 0x3F, 0x3C, 0x00, 0xFE, 0x1F, 0x3C, 0x80, 0xFF, 0x0F, 0x3C, 0xE0, 0xFF, 0x01, 0x3C, 0xF8, 0x7F, 0x00, 0x3C, 0xFE, 0x0F, 0x00, 0xFC, 0xFF, 0x03, 0x00, 0xFC, 0xFF, 0x00, 0x00, 0xFC, 0x1F, 0x00, 0x00, 0xFC, 0x07, 0x00, 0x00, 0xFC, 0x01, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00,  // Code for char 7
-        0x16, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x03, 0xC0, 0x07, 0xFC, 0x07, 0xF0, 0x1F, 0xFE, 0x1F, 0xF8, 0x3F, 0xFF, 0x1F, 0xFC, 0xBF, 0xFF, 0x3F, 0xFE, 0xFF, 0xFF, 0x3F, 0xFE, 0xFF, 0x0F, 0x7E, 0x1F, 0xFE, 0x03, 0x7C, 0x0F, 0xFC, 0x01, 0x78, 0x0F, 0xF8, 0x01, 0x78, 0x0F, 0xF8, 0x01, 0x78, 0x0F, 0xF8, 0x03, 0x78, 0x1F, 0xFC, 0x07, 0x7C, 0xFF, 0xFF, 0x0F, 0x3E, 0xFE, 0xFF, 0xFF, 0x3F, 0xFE, 0xDF, 0xFF, 0x1F, 0xFC, 0xCF, 0xFF, 0x0F, 0xF8, 0x87, 0xFF, 0x07, 0xF0, 0x01, 0xFF, 0x03, 0x00, 0x00, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00,  // Code for char 8
-        0x17, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0xC0, 0xFF, 0x00, 0x00, 0xF0, 0xFF, 0x01, 0x60, 0xF8, 0xFF, 0x03, 0xE0, 0xFC, 0xFF, 0x07, 0xF0, 0xFC, 0xFF, 0x07, 0xF0, 0x7E, 0xE0, 0x0F, 0xF8, 0x1E, 0x80, 0x0F, 0x78, 0x0F, 0x00, 0x0F, 0x7C, 0x0F, 0x00, 0x0F, 0x7C, 0x0F, 0x00, 0x0F, 0x3E, 0x0F, 0x00, 0x0F, 0x3F, 0x1F, 0x00, 0x87, 0x1F, 0x3F, 0x80, 0xE7, 0x1F, 0xFE, 0xC1, 0xFB, 0x0F, 0xFE, 0xFF, 0xFF, 0x07, 0xFC, 0xFF, 0xFF, 0x03, 0xF8, 0xFF, 0xFF, 0x01, 0xF0, 0xFF, 0x7F, 0x00, 0xC0, 0xFF, 0x1F, 0x00, 0x00, 0xFF, 0x03, 0x00,  // Code for char 9
-        0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x03, 0xC0, 0x07, 0xF0, 0x07, 0xE0, 0x0F, 0xF8, 0x07, 0xF0, 0x0F, 0xF8, 0x07, 0xF0, 0x0F, 0xF8, 0x07, 0xF0, 0x0F, 0xF8, 0x07, 0xF0, 0x0F, 0xF8, 0x03, 0xF0, 0x07, 0xF0, 0x01, 0xE0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00   // Code for char :
-};
-
-// Based on https://github.com/adafruit/Adafruit_SSD1306
-
-#define SSD1306_SETLOWCOLUMN 0x00
-#define SSD1306_SETHIGHCOLUMN 0x10
-#define SSD1306_MEMORYMODE 0x20
-#define SSD1306_SETSTARTLINE 0x40
-#define SSD1306_SETCONTRAST 0x81
-#define SSD1306_CHARGEPUMP 0x8D
-#define SSD1306_SEGREMAP 0xA0
-#define SSD1306_DISPLAYALLON_RESUME 0xA4
-#define SSD1306_DISPLAYALLON 0xA5
-#define SSD1306_NORMALDISPLAY 0xA6
-#define SSD1306_INVERTDISPLAY 0xA7
-#define SSD1306_SETMULTIPLEX 0xA8
-#define SSD1306_DISPLAYOFF 0xAE
-#define SSD1306_DISPLAYON 0xAF
-#define SSD1306_SETSTARTPAGE 0xB0
-#define SSD1306_COMSCANINC 0xC0
-#define SSD1306_COMSCANDEC 0xC8
-#define SSD1306_SETDISPLAYOFFSET 0xD3
-#define SSD1306_SETCOMPINS 0xDA
-#define SSD1306_SETVCOMDETECT 0xDB
-#define SSD1306_SETDISPLAYCLOCKDIV 0xD5
-#define SSD1306_SETPRECHARGE 0xD9
-#define SSD1306_NOP 0xE3
-
-static const byte display_init_cmds[] = {
-  SSD1306_DISPLAYOFF,
-  SSD1306_SETDISPLAYCLOCKDIV, 0x80,  // the suggested ratio 0x80
-  SSD1306_SETMULTIPLEX, 0x1F,        // ratio 32
-  SSD1306_SETDISPLAYOFFSET,0x0,      // no offset
-  SSD1306_SETSTARTLINE | 0x0,        // line #0
-  SSD1306_CHARGEPUMP, 0x14,          // internal vcc
-  SSD1306_MEMORYMODE, 0x02,          // page mode
-  SSD1306_SEGREMAP | 0x0,            // column 0 mapped to SEG0
-  SSD1306_COMSCANINC,                // column scan direction not reversed
-  SSD1306_SETCOMPINS, 0x02,          // sequential COM pins, disable remap
-  SSD1306_SETCONTRAST, 0x7F,         // contrast level 127
-  SSD1306_SETPRECHARGE, 0xF1,        // pre-charge period (1, 15)
-  SSD1306_SETVCOMDETECT, 0x40,       // vcomh regulator level-
-  SSD1306_DISPLAYALLON_RESUME,
-  SSD1306_NORMALDISPLAY,
-  SSD1306_DISPLAYON,
-};
-
-static void display_cmd(byte cmd)
-{
-  LL_I2C_ClearFlag_STOP(I2C1);
-  LL_I2C_HandleTransfer(I2C1, 0x79, LL_I2C_ADDRSLAVE_7BIT, 2, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
-  while (!LL_I2C_IsActiveFlag_TXE(I2C1))
-    ;
-  LL_I2C_TransmitData8(I2C1, 0x00);            // Will send a command
-  while (!LL_I2C_IsActiveFlag_TXE(I2C1))
-    ;
-  LL_I2C_TransmitData8(I2C1, cmd);
-  while (!LL_I2C_IsActiveFlag_STOP(I2C1))
-    ;
-}
-
-static void display_data_start(uint cnt)
-{
-  LL_I2C_ClearFlag_STOP(I2C1);
-  LL_I2C_HandleTransfer(I2C1, 0x79, LL_I2C_ADDRSLAVE_7BIT, cnt + 1, LL_I2C_MODE_AUTOEND, LL_I2C_GENERATE_START_WRITE);
-  while (!LL_I2C_IsActiveFlag_TXE(I2C1))
-    ;
-  LL_I2C_TransmitData8(I2C1, 0x40);            // Will send data
-}
-
-static void display_data(byte d)
-{
-  while (!LL_I2C_IsActiveFlag_TXE(I2C1))
-    ;
-  LL_I2C_TransmitData8(I2C1, d);
-}
-
-static void display_data_end(void)
-{
-  while (!LL_I2C_IsActiveFlag_STOP(I2C1))
-    ;
-}
-
-static void display_init(void)
-{
-  for (uint i=0; i < sizeof(display_init_cmds); i++)
-    display_cmd(display_init_cmds[i]);
-
-  for (uint p=0; p<4; p++)
-    {
-      display_cmd(SSD1306_SETSTARTPAGE + p);
-      display_cmd(SSD1306_SETHIGHCOLUMN);
-      display_cmd(SSD1306_SETLOWCOLUMN);
-      display_data_start(128);
-      for (uint i=0; i<128; i++)
-       {
-         byte x = 0;
-         // x = Gentium23x32[(23*4+1)*(i/23) + 1 + 4*(i%23) + p];
-         display_data(x);
-       }
-      display_data_end();
-    }
-}
-
-static void counter(uint cnt)
-{
-  byte d[5];
-  for (uint i=0; i<5; i++)
-    {
-      d[4-i] = cnt % 10;
-      cnt /= 10;
-    }
-
-  for (uint p=0; p<4; p++)
-    {
-      display_cmd(SSD1306_SETSTARTPAGE + p);
-      display_cmd(SSD1306_SETHIGHCOLUMN);
-      display_cmd(SSD1306_SETLOWCOLUMN);
-      display_data_start(5*24);
-      for (uint i=0; i<5; i++)
-       {
-         for (uint j=0; j<23; j++)
-           {
-             byte x = Gentium23x32[(23*4+1)*d[i] + 1 + 4*j + p];
-             display_data(x);
-           }
-         display_data(0);
-       }
-      display_data_end();
-    }
-}
-
-void run_test(void)
-{
-  uint cnt = 0;
-
-  debug_puts("Init\r\n");
-  display_init();
-
-  for (;;)
-    {
-      LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
-
-      debug_printf("Tick tock: %d\r\n", cnt);
-
-      counter(cnt);
-
-      LL_mDelay(1000);
-      cnt++;
-    }
-}
-
-#else
-
-static volatile uint32_t dma_buffer;
-
-static void ds18b20_reset(void)
-{
-  debug_puts("DS18B20: Reset\r\n");
-  LL_TIM_DisableCounter(TIM3);
-  LL_TIM_SetOnePulseMode(TIM3, LL_TIM_ONEPULSEMODE_SINGLE);
-  LL_TIM_CC_SetDMAReqTrigger(TIM3, LL_TIM_CCDMAREQUEST_CC);
-
-  dma_buffer = 0xdeadbeef;
-  LL_DMA_SetMemoryAddress(DMA1, LL_DMA_CHANNEL_4, (uint32_t) &dma_buffer);
-  LL_DMA_SetPeriphAddress(DMA1, LL_DMA_CHANNEL_4, (uint32_t) &THERMO_GPIO_Port->IDR);
-  LL_DMA_SetDataLength(DMA1, LL_DMA_CHANNEL_4, 1);
-  LL_DMA_EnableChannel(DMA1, LL_DMA_CHANNEL_4);
-
-  LL_TIM_OC_InitTypeDef oc;
-
-  LL_TIM_OC_StructInit(&oc);
-  oc.OCMode = LL_TIM_OCMODE_FROZEN;
-  oc.CompareValue = 560;
-  LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH1, &oc);
-  LL_TIM_EnableDMAReq_CC1(TIM3);
-
-  LL_TIM_OC_StructInit(&oc);
-  oc.OCMode = LL_TIM_OCMODE_FORCED_ACTIVE;
-  oc.OCState = LL_TIM_OCSTATE_ENABLE;
-  oc.CompareValue = 480;
-  oc.OCPolarity = LL_TIM_OCPOLARITY_LOW;
-  LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH2, &oc);
-
-  LL_TIM_SetAutoReload(TIM3, 999);
-  LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_INACTIVE);
-
-  LL_TIM_EnableCounter(TIM3);
-
-  while (LL_TIM_IsEnabledCounter(TIM3))
-    ;
-  // Counter is automatically disabled at the end of cycle
-
-  LL_TIM_DisableDMAReq_CC1(TIM3);
-  LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_4);
-
-  debug_printf("DMA = %08x [%u] (%u remains)\r\n", dma_buffer, !!(dma_buffer & THERMO_Pin), LL_DMA_GetDataLength(DMA1, LL_DMA_CHANNEL_4));
-}
-
-static void ds18b20_send_byte(byte b)
-{
-  LL_TIM_SetAutoReload(TIM3, 99);
-  for (uint m=0x80; m; m >>= 1)
-    {
-      LL_TIM_OC_SetCompareCH2(TIM3, ((b & m) ? 1 : 89));
-      LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_FORCED_ACTIVE);
-      LL_TIM_OC_SetMode(TIM3, LL_TIM_CHANNEL_CH2, LL_TIM_OCMODE_INACTIVE);
-      LL_TIM_EnableCounter(TIM3);
-      while (LL_TIM_IsEnabledCounter(TIM3))
-       ;
-    }
+byte recognize(int n){
+    if (n<150) return 0;
+    else return 1;
 }
 
 void run_test(void)
 {
   uint cnt = 0;
-
-  debug_puts("Init\r\n");
-  NVIC_DisableIRQ(TIM3_IRQn);
-
-#if 0
-  LL_GPIO_InitTypeDef GPIO_InitStruct;
-  GPIO_InitStruct.Pin = THERMO_Pin;
-  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
-  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
-  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
-  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
-  LL_GPIO_Init(THERMO_GPIO_Port, &GPIO_InitStruct);
-  LL_GPIO_SetOutputPin(THERMO_GPIO_Port, THERMO_Pin);
-#endif
-
-  ds18b20_reset();
+  uint cnt2 = 0;
+  static const char mtree[]="**ETIANMSURWDKGOHVF*L*PJBXCYZQ**";
+  int letter=1;
 
   for (;;)
     {
-      LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
-
-      debug_printf("Tick tock: %d\r\n", cnt);
-      // ds18b20_send_byte(0xcc);
-      ds18b20_reset();
-
-      LL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
-
-      LL_mDelay(100);
-      cnt++;
+      LL_GPIO_SetOutputPin(LD2_GPIO_Port, LD2_Pin);
+      cnt=0;
+      while (!LL_GPIO_IsInputPinSet(BLUE_BUTTON_GPIO_Port, BLUE_BUTTON_Pin)){
+         cnt++;
+         LL_mDelay(1);
+      }
+      LL_GPIO_ResetOutputPin(LD2_GPIO_Port, LD2_Pin);
+      // debug_printf(">>> %d\n", cnt);
+      letter=(letter<<1)+recognize(cnt);
+      cnt=0;
+      while (LL_GPIO_IsInputPinSet(BLUE_BUTTON_GPIO_Port, BLUE_BUTTON_Pin)){
+         cnt++;
+         LL_mDelay(1);
+         if (cnt==150) {
+             debug_putc(mtree[letter]);
+             letter=1;
+         }
+         if (cnt==1500) debug_putc(' ');
+         if (cnt==4000) debug_putc('\n');
+      }
+
+      cnt2++;
+      for (int i=0;i<4;i++){
+         if (cnt2&(1<<i)) decide_set((i+1)%4);
+         else decide_reset((i+1)%4);
+      }
     }
 }
-
-#endif