Tuesday, March 17, 2020

Making custom function for spreadsheet


  1. Open a new spreadsheet, on the menu tab, choose Tools > Script editor
  2. Now you can write your own function in javascript
  3. This is a simple function that wraps HTML input tags

/**
* Generate a string for input tag.
*
* @param {"text"} type - type can be checkbox, text,....
* @param {"region"} name - name is required for php form.
* @param {"parent_form"} id -  .
* @param {"table01"} clas -  .
* @param {"country-of-birth"} placeholder - .
* @return the tring wraped with input tags.
* @customfunction
*/

function htmlInput(type,name,id,clas,placeholder){
  var outputString = '<input type=';
  var qm = '\"';//qm: quotation mark
  var space = ' ';
  outputString = outputString.concat(qm,type,qm,space);
  if(name){
    outputString = outputString.concat('name=',qm,name,qm,space);
  }
  if (id) {
    outputString = outputString.concat('id=',qm,id,qm,space);
  }
  if (clas) {
    outputString = outputString.concat('class=',qm,clas,qm,space);
  }
  if (placeholder) {
    outputString = outputString.concat('id=',qm,placeholder,qm,space);
  }
  outputString = outputString.concat('>')
  return (outputString);
}



Documentation style using JSDoc
  1. Now, you can use it in your spreadsheet

Friday, December 27, 2019

If của Rudyard Kipling

If you can keep your head when all about you  
    Are losing theirs and blaming it on you,  
If you can trust yourself when all men doubt you,
    But make allowance for their doubting too;  
If you can wait and not be tired by waiting,
    Or being lied about, don’t deal in lies,
Or being hated, don’t give way to hating,
    And yet don’t look too good, nor talk too wise:

If you can dream—and not make dreams your master;  
    If you can think—and not make thoughts your aim;  
If you can meet with Triumph and Disaster
    And treat those two impostors just the same;  
If you can bear to hear the truth you’ve spoken
    Twisted by knaves to make a trap for fools,
Or watch the things you gave your life to, broken,
    And stoop and build ’em up with worn-out tools:

If you can make one heap of all your winnings
    And risk it on one turn of pitch-and-toss,
And lose, and start again at your beginnings
    And never breathe a word about your loss;
If you can force your heart and nerve and sinew
    To serve your turn long after they are gone,  
And so hold on when there is nothing in you
    Except the Will which says to them: ‘Hold on!’

If you can talk with crowds and keep your virtue,  
    Or walk with Kings—nor lose the common touch,
If neither foes nor loving friends can hurt you,
    If all men count with you, but none too much;
If you can fill the unforgiving minute
    With sixty seconds’ worth of distance run,  
Yours is the Earth and everything that’s in it,  
    And—which is more—you’ll be a Man, my son!
Nếu con có thể ngẩn cao đầu khi mọi người
    Rối trí và đỗ lỗi cho con,
Nếu con tin vào bản thân dù tất cả nghi ngờ,
    Nhưng chấp nhận những điều nghi kị đó;
Nếu con chờ và không hề mỏi mệt,
    Hoặc bị lừa dối, nhưng không đáp trả,
Hay bị ghét bỏ, nhưng không để câm ghét cản trở,
    Và đừng tỏ ra quá tốt, hoặc nói lời khôn ngoan:

Nếu con có thể mơ—và không để giấc mơ làm chủ;
    Nếu con có thể nghĩ—và không để suy nghĩ làm mục tiêu của mình;
Nếu con gặp Thành Công, và Thất Bại
    Và đối xử 2 kẻ mạo danh đó như nhau;
Nếu con dám nghe lời nói của mình
    Bị thay đổi bởi những kẻ gian manh để bẫy những tên dại khờ,
Hoặc nhìn những thứ con xây nên, sụp đổ,
    Và con đứng lên, gầy dựng lại với những thứ đã mòn:

Nếu con có thể đặt cược những thứ con có
    Thử trong một ván cờ
Và thất bại, phải bắt đầu lại từ đầu
    Và không nói một lời về những mất mát;
Nếu con có thể bắt con tim, lý trí và cơ bắp
    Làm việc sau khi đã rã rời,
Và vững tin khi không còn gì trong con
    Trừ Ý Chí thúc giục: “Hãy cố lên!”

Nếu con có thể nói với đám đông nhưng giữ phẩm giá,
    Hoặc đi với nhà vua—và không mất đi sự chan hòa,
Nếu kẻ thù và bạn tốt không thể làm đau con,
    Nếu mọi người dựa vào con, nhưng con không dựa vào họ quá nhiều;
Nếu con có thể tận dùng từng phút không khoan nhượng
    Với 60 giây đáng giá,
Thế giới này, và mọi thứ trong đó là của con,
    Và—điều quan trọng hơn—Con là đàn ông, con trai!


Thursday, December 12, 2019

How VU meter works?

I meet a cool classmate name Tri who major in CS and wants to program a microcontroller. I'm just a noob in a microcontroller world but I love to explore new things.
We come up with an idea, making a VU with Christmas light.

After looking at other projects, I realize that a microcontroller will read AC signal from an output source, then it will separate the voltage into each level and display them accordingly.

Let's look at a snippet from Instructable(1):
else
    {
      for (i = 0; i < input; i++)
      {
        digitalWrite(led[i], HIGH);
        delay(4);
      }
      for (i = i; i < 11; i++)
      {
        digitalWrite(led[i], LOW);
      }
    } 

In the above code, maxInput is 12. Whenever there is a signal, LEDs that are less than the threshold will turn ON. Then, they will be set back to OFF.

Another example from Instructable(2):

GreatScott uses an analog approach.
  1. Audio (AC) is the main input source (1.3V).
  2. Create threshold values for 7 rows.
  3. So we use LM324 opamp to amplify the signal from 1.3V to 8V.
  4. Then, we build 7 comparators using the same type of opamp.
  5. Then we use MOSFET as a switch
  6. Control by using V_GS

Thursday, August 8, 2019

Generate Signal with Aduino

So I have a school project that requires sending square waves to a piezo controller. We avoid using Arduino Toolbox in Matlab because we have other peripherals that being controlled.
Our solution is to use serial communication in Matlab to control the Arduino board. Here is our code

Arduino:

unsigned long halfPeriod = 5000UL; // 5mS = 1/2 100 Hz
// don't use a floating # for delay, delayMicroseconds

void funcGen(){
  digitalWrite(11, LOW);
  delay(30);
  for(int i = 0; i< 8; i++){
      PINB = PINB | 0b00101000; // x-x-13-12-11-10-9-8 on Uno,
                            // toggle output by writing 1 to input register
      delayMicroseconds(halfPeriod);
    }
}

void setup()
{
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite (13, HIGH); // known starting level
pinMode(11, OUTPUT);
digitalWrite (11, LOW); // known starting level
}

void loop(){
if (Serial.read() > 0){
  funcGen();
  Serial.flush();
  }
}

Matlab:

arduino=serial('COM3','BaudRate',9600);
fopen(arduino)
fprintf(arduino,'1');
fclose(arduino);

Sunday, May 19, 2019

Calculate the length of monopole antenna

We have the formula for wavelength

in which c is the speed of light = 3*10^8 m/s

Practical Example:
A student has a receiver (MX-RM-5V) and a transmitter (FS1000A) in the 433MHz band. When he/she run the basic code sending "hello world", the distance between them is less than 10 cm. In order to increase the transmission distance, what is the length of the antenna should he/she add?

3*10^8 / 433 * 10^6  =  0.6928 meter

so for half-wavelength antenna: 0.5 * 0.6928 = 0.3464 meter
and for quater-wavelength antenna: 0.25 * 0.6928 = 0.1732 meter or 17.32 cm

That is the reason we want to add the carrier frequency to reduce the length of the antenna.

The range is improved significantly after the antennas are inserted



Wednesday, January 16, 2019

Fun scripts in cmd

In this post, I’ll represent temporary parameter by using %name of parameter%, you need to change the whole thing with your parameter. Please take a look at the example below. The reason I use %name of parameter% because it represent a variable in batch file.

simpleBatch.bat (run as administrator)

@echo off
setlocal
set username=olaf
REM create new user
net user %username% /add
net user
pause
Result:
Administrator DefaultAccount Guest
xxx olaf xxx

Start or Stop or enable type of service in cmd

sc config %service name% start=auto
net start %service name%
Ex (remember to change %service name% to service name you want to enable):
sc config RemoteRegistry start=auto
net start RemoteRegistry

Find particular service in cmd

sc query type=service state=all | findstr /i “remote”
/i: case insensitive

Add key or value to registry

reg add “%directory%” /v %value% /t %type_of_value% /d %data%
You can read more here
/t:
REG_BINARY // Free form binary
REG_DWORD // 32-bit number
REG_DWORD_LITTLE_ENDIAN // 32-bit number (same as REG_DWORD)
REG_DWORD_BIG_ENDIAN // 32-bit number
REG_LINK // Symbolic Link (unicode)
REG_MULTI_SZ // Multiple Unicode strings
REG_RESOURCE_LIST // Resource list in the resource map
REG_FULL_RESOURCE_DESCRIPTOR // Resource list in the hardware description
REG_QWORD // 64-bit number

Shutdown a computer

shutdown /m %name_of_the_network_machine% /s /f
/s: shutdown
/f: forced shutdown

Hide user in log in screen

We need to add “SpecialAccounts\UserList” sub-key under Winlogon
In cmd:
reg add “\%computer_name%”\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList" /v %user_name_to_be_hidden% /t REG_DWORD /d 00000000

Disable UAC (User Account Control) restrictions in order to remote control computer

reg add “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System” /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 00000001

Create a new user with admin privilege

net user %your user name% /add
net localgroup administrators %username% /add

Delete a user

net user username /delete

Change user password

net use %username% %password%

Login to the network

net use \name_of_computer_in_nw

Extend the maximum login

net accounts /maxpwage:%duration%

Thursday, January 10, 2019

How to exit Status: Disconnected and flash firmware for MOD-t Newmatter

The reason MOD-t printer ultility show Disconnected is because it's in DFU mode.
To exit, we can reflash the firmware


1. First, if you just get the printer, we need to install newest software and firmware for it.

https://github.com/tripflex/MOD-t You can find both software and firmware at the link above
I'll use the newest one because the developer said that it will improve the printer speed
https://github.com/tripflex/MOD-t/raw/master/firmware/1.0.0/firmware.dfu

2. After downloading the file, you should rename it to "firmware_modt_override.dfu" and move to C:/

We also need to download flashing tool: https://github.com/tripflex/MOD-t/blob/master/firmware/dfu-util.exe And put that in C:/ directory

3. Before flashing the firmware, we need to put MOD-t into DFU mode

if you use Windows, you might need to install libusb-win32 here: https://sourceforge.net/projects/libusb-win32/
after installing the software, click Install a Device Filter and Choose MOD-t
Run the enter_dfu.py script (it can be found in tripflex repo)


4. Now we can flash the firmware

press: Window + R > type cmd
type: cd C:/
type: dfu-util.exe -d 2b75:0003 -a 0 -s 0x0:leave -D firmware_modt_override.dfu
Done!
You can try your first print now.
Good luck!